A file is modified by a script using an input file-
141,141_1,BAR,HONDA,ps2_0,unassigned,ps3_0,Unassigned,ps4_0,Unassigned,ps5_0,Unassigned,ps6_0,Unassigned,ps7_3,TILL WILL,.....
Input file-
141,ps7,TILL WILL
Now I need to search whether to column ps7_3 is updated with the correct value.
So from the input file, I separated the columns-
while read -r line;
do
sub1=$(echo $line|cut -f 1 -d ',');
sub2=$(echo $line|cut -f 2 -d ',');
sub3=$(echo $line|cut -f 3 -d ',');
sub4=$(echo $sub2'.*,'$sub3|sed -e "s/\(.*\)\r/'\1'/");
echo $sub1;
echo $sub2;
echo $sub3;
echo $sub4;
grep $sub4 modded_file.csv.dat;
done<input.csv
The output being-
141
ps7
TILL WILL
'ps7.*,TILL WILL'
grep: WILL': No such file or directory
But when I run grep 'ps7.*,TILL WILL' modded_file.csv.dat
, it works. How can I grep a variable as shown above, in a file?
cut
,echo
andsed
, you could replace all by a simplesed -E "s/.*,(.*),(.*)/'\1.*,\2'/"
, but actually you don't want the single quotes to be part of the search pattern, so it's-E "s/.*,(.*),(.*)/\1.*,\2/"
. Escape the pattern variable with double quotes, like @Romeo indicated in his answer. – Philippos Apr 26 '17 at 07:39for pattern in "$(sed -E "s/.*,(.*),(.*)/\1.*,\2/" input.csv)"; do grep "$pattern" modded_file.csv.dat; done
to iterate through your input file. You can also do the whole thing in one pass, but this requires deepersed
orawk
experience – Philippos Apr 26 '17 at 08:40