LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux > Linux - Newbie
User Name
Password
Linux - Newbie This forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Tags used in this thread
Popular LQ Tags , , , ,

Reply
 
Thread Tools
Old 12-14-2006, 01:54 PM   #1
armandino101
LQ Newbie
 
Registered: Oct 2006
Distribution: Ubuntu
Posts: 14
Thanked: 0
Parsing text file line by line


[Log in to get rid of this advertisement]
Hi,

I have a file with cvs output:


Code:
Checking in src/com/blah/Foo.java;
/home/cvs/cvsroot/project/src/com/blah/Foo.java,v  <--  Foo.java
new revision: 1.2; previous revision: 1.1
done

Checking in src/com/blah/Bar.java;
/home/cvs/cvsroot/project/src/com/blah/Bar.java,v  <--  Bar.java
new revision: 1.7; previous revision: 1.6
done
I want to parse this file line by line:

Code:
LINES="$( cat cvsout.txt )"
for i in $(<LINES); do
  echo $i
done
but this converts each white space into a new line, so I get

Code:
Checking
in
src/com/blah/Foo.java;
...
What am I doing wrong here? Any suggestions?

Thanks in advance.

PS: if it helps, my end goal is to format the text into a tidier output that looks like this:

Code:
project/src/com/blah/Foo.java:1.2
project/src/com/blah/Bar.java:1.7
armandino101 is offline  
Tag This Post , , , ,
Reply With Quote
Old 12-14-2006, 02:23 PM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu (x86), Debian (PPC)
Posts: 3,495
Thanked: 6
You want something like this:

Code:
cat cvsout.txt | while read line; do
    echo "got line: $line"
done
...although I think perl or awk is a better bet for this sort of task. For sure you can do it with a shell script, but it's really not ideal for this sort of thing, whereas perl is. For example:
Code:
#!/usr/bin/perl -w

use strict;

my $file = "";
my $ver = 0.0;


while (<>) {
        if ( m|cvsroot/(.*),v\s| ) {
                $file = $1;
        }
        elsif ( m|^new revision: ([^;]+);| ) {
                $ver = $1;
        }
        elsif ( /^\s*done\s*$/ ) {
                print "$file:$ver\n";
        }
}
matthewg42 is offline     Reply With Quote
Old 12-14-2006, 03:23 PM   #3
armandino101
LQ Newbie
 
Registered: Oct 2006
Distribution: Ubuntu
Posts: 14
Thanked: 0

Original Poster
That does exactly what i need.

Cheers Matt!
armandino101 is offline     Reply With Quote
Old 12-14-2006, 03:43 PM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu (x86), Debian (PPC)
Posts: 3,495
Thanked: 6
No problem.
matthewg42 is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

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
C++ text file line by line/each line to string/array Dimitris Programming 15 03-11-2008 09:22 AM
read line by line form text file in java. spank Programming 1 10-18-2006 03:46 PM
how to change some text of a certain line of a text file with bash and *nix scripting alred Programming 6 07-10-2006 12:55 PM
Parsing a string line-by-line in PHP enigma_0Z Programming 3 04-21-2006 09:07 AM
Getting a text file line after line in C++ cdog Programming 4 12-31-2005 03:36 PM


All times are GMT -5. The time now is 07:13 AM.

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
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration