LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Parsing text file line by line (https://www.linuxquestions.org/questions/linux-newbie-8/parsing-text-file-line-by-line-510461/)

armandino101 12-14-2006 12:54 PM

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


matthewg42 12-14-2006 01:23 PM

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";
        }
}


armandino101 12-14-2006 02:23 PM

That does exactly what i need.

Cheers Matt!

matthewg42 12-14-2006 02:43 PM

No problem.


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