LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux 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


Reply
  Search this Thread
Old 12-14-2006, 12:54 PM   #1
armandino101
LQ Newbie
 
Registered: Oct 2006
Distribution: Ubuntu
Posts: 14

Rep: Reputation: 0
Parsing text file line by line


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
 
Old 12-14-2006, 01:23 PM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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";
        }
}
 
Old 12-14-2006, 02:23 PM   #3
armandino101
LQ Newbie
 
Registered: Oct 2006
Distribution: Ubuntu
Posts: 14

Original Poster
Rep: Reputation: 0
That does exactly what i need.

Cheers Matt!
 
Old 12-14-2006, 02:43 PM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
No problem.
 
  


Reply

Tags
bash, file, parse, script, text



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 08:22 AM
read line by line form text file in java. spank Programming 1 10-18-2006 02: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 11:55 AM
Parsing a string line-by-line in PHP enigma_0Z Programming 3 04-21-2006 08:07 AM
Getting a text file line after line in C++ cdog Programming 4 12-31-2005 02:36 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:33 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