LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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: 61
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
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
(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: 61
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



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:08 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration