Reputation: 133
I have a csv file like this :
Monday
Tuesday
Wednesday
Thursday
Friday
I would like to analyze an other csv file with the csv file with the days of the week. My script will take Monday, analyze the file and extract all the lines that contain the word Monday, then switch to Tuesday etc....
I use this command to do this :
grep -wf daysfile.csv myothercsv.csv > file.csv
This command works pretty well, but the result is :
Monday XXXX XXX XXX
Monday XXXX XXX XXX
Monday XXXX XXX XXX
Monday XXXX XXX XXX
Tuesday XXXX XXXX XXXX
Tuesday XXXX XXXX XXXX
Tuesday XXXX XXXX XXXX
Tuesday XXXX XXXX XXXX
Tuesday XXXX XXXX XXXX
Wednesday XXXX XXXX XXXX
Wednesday XXXX XXXX XXXX
Wednesday XXXX XXXX XXXX
Wednesday XXXX XXXX XXXX
But I would like to have spaces like this :
Monday XXXX XXX XXX
Monday XXXX XXX XXX
Monday XXXX XXX XXX
Monday XXXX XXX XXX
Tuesday XXXX XXXX XXXX
Tuesday XXXX XXXX XXXX
Tuesday XXXX XXXX XXXX
Tuesday XXXX XXXX XXXX
Can you help me ?
Upvotes: 1
Views: 430
Reputation: 556
I'm not sure how grep -wf daysfile.csv myothercsv.csv
is working for you as you described. The data in myothercsv.csv
must already be sorted by days of the week, since the grep command would simply print any line that has any matching word in the daysfile.csv
as they are ordered.
If you want to search for each word in the daysfile.csv
sequentially, then you probably want following:
cat daysfile.csv | while read line; do grep "$line" myothercsv.csv; done
Then you can apply the other answers here that refer to adding blank lines (when a different first word is detected) to the output.
Also, a different way to do this is to insert a blank line into myothercsv file, and insert ^$
lines to your daysfile. Here's an example, notice the first line in myothercsv file is a blank.
$ cat daysfile.csv
mon
^$
tue
^$
wed
^$
$ cat myothercsv.csv
mon,1
tue,2
wed,3
mon,4
tue,5
wed,6
$ cat daysfile.csv | while read line; do grep "$line" myothercsv.csv; done
mon,1
mon,4
tue,2
tue,5
wed,3
wed,6
Upvotes: 0
Reputation: 133428
In awk
could you please try following.
your_grep_command | awk 'prev!=$1 && prev{print ""} {prev=$1;print}'
Since OP has NOT provided samples so couldn't test it, could you please try to run directly this command? In spite of passing grep
output to another command once and let us know then.
awk 'FNR==NR{a[$0];next} prev!=$1{print ""} ($0 in a);{prev=$1}' daysfile.csv <(sort -k1 myothercsv.csv)
Upvotes: 0
Reputation: 3183
Use awk
grep -wf daysfile.csv myothercsv.csv |awk -F: '{if(f!=$1)print ""; f=$1; print $0;}'
Monday XXXX XXX XXX
Monday XXXX XXX XXX
Monday XXXX XXX XXX
Monday XXXX XXX XXX
Tuesday XXXX XXXX XXXX
Tuesday XXXX XXXX XXXX
Tuesday XXXX XXXX XXXX
Tuesday XXXX XXXX XXXX
Tuesday XXXX XXXX XXXX
Wednesday XXXX XXXX XXXX
Wednesday XXXX XXXX XXXX
Wednesday XXXX XXXX XXXX
Wednesday XXXX XXXX XXXX
Upvotes: 0
Reputation: 241738
Just pipe the output of grep to
perl -ape 'print "\n" if $previous and $previous ne $F[0]; $previous = $F[0]'
-p
reads the input line by line and prints each line after processing-a
splits each input line into the @F arrayUpvotes: 1