Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 10-31-2004, 09:09 AM   #1
Senior Member
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 59
awk text that is on several lines


I am using awk to show the comments in a file like this ....
this is a /* comment on */ one line and
this is /* also on one */ line.

#find the lines which start with "/* and end with "*/"
#remove the "/*" and "*/" from output
awk -F[\/][\*] '{print $2}' | awk -F[\*][\/] '{print $1}' file.txt

This works fine if the comment is on one line but what if the person is long winded and the comments go for several lines like this .....
this is a /* comment which
takes up several lines
in the file. */

How do you get awk or sed to continue on down past the \n ?
Old 10-31-2004, 09:21 AM   #2
Registered: Jul 2004
Location: Chennai, India
Distribution: UBUNTU 5.10 since Jul-18,2006 on Intel 820 DC
Posts: 843

Rep: Reputation: 201Reputation: 201Reputation: 201
Since the default for RS (the record separator is newline, is you change it to some non existent character ..

Possible pseude code for awk.
InCommentFlag = 0
For(Ix=0;Ix<Length($0);Ix++) {
If (InCommentFlag == 1) {
If (data is "*/") InCommentFlag = 0
Else {
If Data is "/*"
InCommentFlag = 1
Write the byte.
Old 10-31-2004, 09:27 AM   #3
Senior Member
Registered: Oct 2003
Posts: 3,057

Original Poster
Rep: Reputation: 59
Thanks AnanthaP

Unfortunately, the \n can't be changed to something else as the lines will be counted.

#find the lines which start with "/* and end with "*/"
#remove the "/*" and "*/" from output
cat /home/file.txt |grep -e '[\/][\*]\(.*\)[\*][\/]' | \
awk -F[\/][\*] '{print $2}' | awk -F[\*][\/] '{print $1}' > file1.txt

#count the number of lines which start with "/*" and end with "*/"
cat /home/file1.txt |wc -l > file2.txt

#count the number of words from those lines.
cat /home/file1.txt |wc -w >> file2.txt

#count the number of unique words from those lines.
cat /home/file1.txt | tr ' ' '\012' |sort | uniq |wc -w >> file2.txt


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Grab text lines in text file LULUSNATCH Programming 1 12-02-2005 10:55 AM
formating text thru awk slack66 Linux - Newbie 4 11-28-2005 11:20 AM
Extracting text with grep or awk? UrbanDruid Linux - Newbie 5 04-07-2005 02:57 PM
counting the commented lines using awk [ /* */] itsjvivek Linux - General 8 01-17-2003 08:30 AM
awk usage and color text ^BuGs^ Programming 2 09-24-2001 02:04 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:20 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration