Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a number of log files that look like this:

/*
header
arbitrary number of lines
*/
blah blah
blah blah

Using simple Bash commands (preferably sed, not awk), how would I retrieve only the header lines (ideally including the comment markers)?

I've RTFM and tried googling, also found some hints, but not enough to get me started.

Thanks!

share|improve this question
    
Another related link: cyberciti.biz/faq/sed-howto-remove-lines-paragraphs That does the opposite of what I need though. –  AnC Sep 6 '09 at 15:32

4 Answers 4

up vote 2 down vote accepted

If you confirm the following things, the script in this answer will work for you.

  1. Files start with the "/*" characters
  2. There may be multi-line C-Syntex comment block that is not nested
  3. The comment block ends with no additional C-Statements on the same line
sed -n '/^\/\*/,/\*\// p' file.c
#        - - -    - - 

This will match all lines from the start of the file to end of the comment block.
The second line (with a "#" in the start) highlights the match being searched for.
The "-n" at the start and the "p" at the end tell sed to print only the matching part.

share|improve this answer
    
Thanks, much appreciate the detailed response! –  AnC Sep 6 '09 at 16:49
    
@AnC, I think there is a problem in my solution too. It does not stop like Richard's command after the first match! So, you'll get all the comments starting at new lines (requirement 1 is not limited to the start of the file) in a file. –  nik Sep 6 '09 at 16:54
    
Just realized that - so I'm now combining both of your solutions: cat $LOGFILE | sed -e '/*\//q' | sed -n '/^\/*/,/*\// p' Sadly, I can't properly credit you both. –  AnC Sep 6 '09 at 17:09
    
@AnC, you could do: sed -n '/^\/*/,/*\// p' $LOGFILE | sed -e '/*\//q' and skip the cat. But, I am not yet happy about it. –  nik Sep 6 '09 at 17:32

This look at every file with an extension .log and if the first line is "/*" only, print everything until a line with "*/" only.

for file in *.log; do
      head -n1 $file | grep  -q '^/*' &&
      sed '/^\*\/$/q' $file; 
done
share|improve this answer
1  
That does the job - thanks! One caveat: It won't work if there's anything above the header, or if there a multiple comment blocks. However, I shouldn't worry about that until I actually need it. –  AnC Sep 6 '09 at 16:21
    
And if there is no header, it will print the whole file! There are a lot of failure modes. Perhaps you can ask the question "How can I print comments in a file?" (And please let us use awk.) –  Richard Hoskins Sep 6 '09 at 16:28
    
You're right - nik's solution seems a little safer in that regard, so I'll use that for now. Thanks again! –  AnC Sep 6 '09 at 16:48
    
I have updated my answer to get rid of most of its worst problems. –  Richard Hoskins Sep 6 '09 at 19:32
    
Interesting approach. Now I'm torn between this and the combination of both of your solutions (see comments above)... –  AnC Sep 7 '09 at 12:07
cat *.log | sed -e 's/*\/\*//g' -e 's/\*\///g' >> smt.log

That might be it

share|improve this answer
    
I'm afraid that only removes the "*/" line. I've experimented with similar expressions, but didn't get anywhere - I think it's because sed works on a line-by-line basis. –  AnC Sep 6 '09 at 16:06

In case, if like me you stumbled across this question while trying to retrieve Doxygen style comments, the sed command is:

sed -n '/^\/\*/p; /^ \*/p' < file

For a more detailed explanation, I blogged about it.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.