LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

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

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


Hi,

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
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Distribution: UBUNTU 5.10 since Jul-18,2006 on Intel 820 DC
Posts: 607

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

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

Original Poster
Rep: Reputation: 56
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
 
  


Reply


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


All times are GMT -5. The time now is 04:26 PM.

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