user2586302
user2586302

Reputation: 71

Print the results of a grep to a csv file

I am trying to print the results of grep to a CSV file. When I run this command grep FILENAME * in my directory I get this result : B140509184-#-02Jun2015-#-11:00-#-12:00-#-LT4-UGW-MAJAZ-#-I-#-CMAP-#-P-45088966.lif: FILENAME A20150602.0835+0400-0840+0400_DXB-GGSN-V9-PGP-16.xml.

What I want is to print the FILENAME part to a csv. Below is what I have tried so far, I am a bit lost in the part of how I should go about printing the result

BASE_DIR=/appl/virtuo/var/loader/spool/umtsericssonggsn_2013a/2013A/good

cd ${BASE_DIR}

grep FILENAME *
#grep END_TIME *
#grep START_TIME *

#my $Filename = grep FILENAME *;
print $Filename;


#$ awk '{print;}' employee.txt

#echo "$Filename :"

File sample

measInfoId 140509184
GP 3600
START_DATE 2015-06-02



Output should be 140509184 3600 2015-06-02, in columns of a csv file

Upvotes: 2

Views: 15111

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 207465

Updated Answer

If you only want lines that start with GP, or START_DATE or measInfoId, you would modify the awk commands below to look like this:

awk '/^GP/ || /^START_DATE/ || /^measInfoId/ {print $2}' file

Original Answer

I am not sure what you are trying to do actually, but this may help...

If your input file is called file, and contains this:

measInfoId 140509184
GP 3600
START_DATE 2015-06-02

This command will print the second field on each line:

awk '{print $2}' file
140509184
3600
2015-06-02

Then, building on that, this command will put all those onto a single line:

awk '{print $2}' file | xargs
140509184 3600 2015-06-02

And then this one will translate the spaces into commas:

awk '{print $2}' file | xargs | tr " " ","
140509184,3600,2015-06-02

And if you want to do that for all the files in a directory, you can do this:

for f in *; do
   awk '{print $2}' "$f" | xargs | tr " " ","
done > result.cv

Upvotes: 2

Related Questions