Reputation: 1434
I have a list of lists, like:
125,99,1]
109,124,70,1]
71,62,1]
108,90,1]
and I want use grep along a file with each of them. Problem is that in the file those lists are like
125 ,99, 1]
109, 124, 70, 1]
71, 62, 1]
108, 90, 1]
i.e. there is an space after each comma. I wanted to change my list and then use the grep.
for i in $strs
do echo $i
k=\"
echo $i | awk '{gsub(",",", ");print}' > tmp
less report150.txt | grep $k$tmp$k
echo $k$i$k
done
But, the output of tmp
is just nothing.
I appreciate any help.
Thanks
Afshin
Here is also a sample of the report150.txt
0.95 4.20385997064e-06 0.75 0.000744232007872 0.9 9000 [43, 125, 99, 1] 64
2016-09-09 07:58:53; Iter= 0; lr= 0.000004; l2= 0.000744; str= [43, 125, 99, 1]; Train_loss= 2456.124; Test_loss= 3192.166; best_tets= 3192.166; ||W|| 5470.262= lmbd*||W||= 4.071; seconds= 0.60
2016-09-09 07:59:05; Iter= 2000; lr= 0.000004; l2= 0.000744; str= [43, 125, 99, 1]; Train_loss= 2440.910; Test_loss= 3197.328; best_tets= 3192.166; ||W|| 5481.576= lmbd*||W||= 4.080; seconds= 12.73
2016-09-09 07:59:17; Iter= 4000; lr= 0.000004; l2= 0.000744; str= [43, 125, 99, 1]; Train_loss= 2453.606; Test_loss= 3209.651; best_tets= 3192.166; ||W|| 5496.604= lmbd*||W||= 4.091; seconds= 24.77
2016-09-09 07:59:29; Iter= 6000; lr= 0.000004; l2= 0.000744; str= [43, 125, 99, 1]; Train_loss= 2446.254; Test_loss= 3189.405; best_tets=
3189.405; ||W|| 5516.128= lmbd*||W||= 4.105; seconds= 36.74
2016-09-09 07:59:41; Iter= 8000; lr= 0.000004; l2= 0.000744; str= [43, 125, 99, 1]; Train_loss= 2439.585; Test_loss= 3209.031; best_tets= 3189.405; ||W|| 5532.284= lmbd*||W||= 4.117; seconds= 48.67
final; 2016-09-09 07:59:53; Iter= 10000; lr= 0.000004; l2= 0.000744; str= [43, 125, 99, 1]; Train_loss= 2446.341; Test_loss= 3215.552; best_tets= 3189.405; ||W|| 5557.950= lmbd*||W||= 4.136; seconds= 60.45
0.95 5.71780298736e-05 0.75 1.44677845571e-05 0.9 9000 [43, 109, 124, 70, 1] 64
2016-09-08 09:39:06; Iter= 0; lr= 0.000057; l2= 0.000014; str= [43, 109, 124, 70, 1]; Train_loss= 16697.264; Test_loss= 17143.036; best_tets= 17143.036; ||W|| 55.256= lmbd*||W||= 0.001; seconds= 0.60
2016-09-08 09:39:19; Iter= 2000; lr= 0.000057; l2= 0.000014; str= [43, 109, 124, 70, 1]; Train_loss= 6630.918; Test_loss= 6804.459; best_tets= 6804.459; ||W|| 249.912= lmbd*||W||= 0.004; seconds= 13.89
2016-09-08 09:39:33; Iter= 4000; lr= 0.000057; l2= 0.000014; str= [43, 109, 124, 70, 1]; Train_loss= 6636.286; Test_loss= 6804.976; best_tets= 6804.459; ||W|| 252.936= lmbd*||W||= 0.004; seconds= 27.23
2016-09-08 09:39:46; Iter= 6000; lr= 0.000057; l2= 0.000014; str= [43, 109, 124, 70, 1]; Train_loss= 3051.755; Test_loss= 3021.825; best_tets= 3021.825; ||W|| 1588.203= lmbd*||W||= 0.023; seconds= 40.49
2016-09-08 09:39:59; Iter= 8000; lr= 0.000057; l2= 0.000014; str= [43, 109, 124, 70, 1]; Train_loss= 3103.843; Test_loss= 3062.770; best_tets= 3021.825; ||W|| 1993.378= lmbd*||W||= 0.029; seconds= 53.90
final; 2016-09-08 09:40:13; Iter= 10000; lr= 0.000054; l2= 0.000014; str= [43, 109, 124, 70, 1]; Train_loss= 3018.172; Test_loss= 2970.957; best_tets= 2970.957; ||W|| 2207.357= lmbd*||W||= 0.032; seconds= 67.35
0.95 3.993666922e-06 0.75 0.000744232007872 0.9 9000 [43, 125, 99, 1] 64
2016-09-09 08:20:28; Iter= 0; lr= 0.000004; l2= 0.000744; str= [43, 125, 99, 1]; Train_loss= 2445.847; Test_loss= 3215.179; best_tets= 3215.179; ||W|| 5557.956= lmbd*||W||= 4.136; seconds= 0.52
2016-09-09 08:20:40; Iter= 2000; lr= 0.000004; l2= 0.000744; str= [43, 125, 99, 1]; Train_loss= 2439.789; Test_loss= 3212.580; best_tets= 3212.580; ||W|| 5572.128= lmbd*||W||= 4.147; seconds= 12.75
2016-09-09 08:20:52; Iter= 4000; lr= 0.000004; l2= 0.000744; str= [43, 125, 99, 1]; Train_loss= 2443.565; Test_loss= 3230.781; best_tets= 3212.580; ||W|| 5590.523= lmbd*||W||= 4.161; seconds= 25.00
2016-09-09 08:21:05; Iter= 6000; lr= 0.000004; l2= 0.000744; str= [43, 125, 99, 1]; Train_loss= 2434.253; Test_loss= 3204.755; best_tets= 3204.755; ||W|| 5602.024= lmbd*||W||= 4.169; seconds= 37.14
2016-09-09 08:21:17; Iter= 8000; lr= 0.000004; l2= 0.000744; str= [43, 125, 99, 1]; Train_loss= 2438.778; Test_loss= 3222.805; best_tets= 3204.755; ||W|| 5617.882= lmbd*||W||= 4.181; seconds= 49.28
final; 2016-09-09 08:21:29; Iter= 10000; lr= 0.000004; l2= 0.000744; str= [43, 125, 99, 1]; Train_loss= 2434.325; Test_loss= 3205.160; best_tets= 3204.755; ||W|| 5626.633= lmbd*||W||= 4.188; seconds= 61.40
0.95 3.79398352829e-06 0.75 0.000744232007872 0.9 9000 [43, 125, 99, 1] 64
2016-09-09 08:40:11; Iter= 0; lr= 0.000004; l2= 0.000744; str= [43, 125, 99, 1]; Train_loss= 2434.336; Test_loss= 3205.251; best_tets= 3205.251; ||W|| 5626.640= lmbd*||W||= 4.188; seconds= 0.48
2016-09-09 08:40:22; Iter= 2000; lr= 0.000004; l2= 0.000744; str= [43, 125, 99, 1]; Train_loss= 2431.928; Test_loss= 3190.885; best_tets= 3190.885; ||W|| 5639.988= lmbd*||W||= 4.197; seconds= 11.52
2
And the output should be like:
0.95 4.20385997064e-06 0.75 0.000744232007872 0.9 9000 [43, 125, 99, 1] 64
2016-09-09 07:58:53; Iter= 0; lr= 0.000004; l2= 0.000744; str= [43, 125, 99, 1]; Train_loss= 2456.124; Test_loss= 3192.166; best_tets= 3192.166; ||W|| 5470.262= lmbd*||W||= 4.071; seconds= 0.60
2016-09-09 07:59:05; Iter= 2000; lr= 0.000004; l2= 0.000744; str= [43, 125, 99, 1]; Train_loss= 2440.910; Test_loss= 3197.328; best_tets= 3192.166; ||W|| 5481.576= lmbd*||W||= 4.080; seconds= 12.73
2016-09-09 07:59:17; Iter= 4000; lr= 0.000004; l2= 0.000744; str= [43, 125, 99, 1]; Train_loss= 2453.606; Test_loss= 3209.651; best_tets= 3192.166; ||W|| 5496.604= lmbd*||W||= 4.091; seconds= 24.77
2016-09-09 07:59:29; Iter= 6000; lr= 0.000004; l2= 0.000744; str= [43, 125, 99, 1]; Train_loss= 2446.254; Test_loss= 3189.405; best_tets=
2016-09-09 07:59:41; Iter= 8000; lr= 0.000004; l2= 0.000744; str= [43, 125, 99, 1]; Train_loss= 2439.585; Test_loss= 3209.031; best_tets= 3189.405; ||W|| 5532.284= lmbd*||W||= 4.117; seconds= 48.67
final; 2016-09-09 07:59:53; Iter= 10000; lr= 0.000004; l2= 0.000744; str= [43, 125, 99, 1]; Train_loss= 2446.341; Test_loss= 3215.552; best_tets= 3189.405; ||W|| 5557.950= lmbd*||W||= 4.136; seconds= 60.45
2016-09-09 08:20:28; Iter= 0; lr= 0.000004; l2= 0.000744; str= [43, 125, 99, 1]; Train_loss= 2445.847; Test_loss= 3215.179; best_tets= 3215.179; ||W|| 5557.956= lmbd*||W||= 4.136; seconds= 0.52
2016-09-09 08:20:40; Iter= 2000; lr= 0.000004; l2= 0.000744; str= [43, 125, 99, 1]; Train_loss= 2439.789; Test_loss= 3212.580; best_tets= 3212.580; ||W|| 5572.128= lmbd*||W||= 4.147; seconds= 12.75
2016-09-09 08:20:52; Iter= 4000; lr= 0.000004; l2= 0.000744; str= [43, 125, 99, 1]; Train_loss= 2443.565; Test_loss= 3230.781; best_tets= 3212.580; ||W|| 5590.523= lmbd*||W||= 4.161; seconds= 25.00
2016-09-09 08:21:05; Iter= 6000; lr= 0.000004; l2= 0.000744; str= [43, 125, 99, 1]; Train_loss= 2434.253; Test_loss= 3204.755; best_tets= 3204.755; ||W|| 5602.024= lmbd*||W||= 4.169; seconds= 37.14
2016-09-09 08:21:17; Iter= 8000; lr= 0.000004; l2= 0.000744; str= [43, 125, 99, 1]; Train_loss= 2438.778; Test_loss= 3222.805; best_tets= 3204.755; ||W|| 5617.882= lmbd*||W||= 4.181; seconds= 49.28
final; 2016-09-09 08:21:29; Iter= 10000; lr= 0.000004; l2= 0.000744; str= [43, 125, 99, 1]; Train_loss= 2434.325; Test_loss= 3205.160; best_tets= 3204.755; ||W|| 5626.633= lmbd*||W||= 4.188; seconds= 61.40
0.95 3.79398352829e-06 0.75 0.000744232007872 0.9 9000 [43, 125, 99, 1] 64
2016-09-09 08:40:11; Iter= 0; lr= 0.000004; l2= 0.000744; str= [43, 125, 99, 1]; Train_loss= 2434.336; Test_loss= 3205.251; best_tets= 3205.251; ||W|| 5626.640= lmbd*||W||= 4.188; seconds= 0.48
2016-09-09 08:40:22; Iter= 2000; lr= 0.000004; l2= 0.000744; str= [43, 125, 99, 1]; Train_loss= 2431.928; Test_loss= 3190.885; best_tets= 3190.885; ||W|| 5639.988= lmbd*||W||= 4.197; seconds= 11.52
2
Upvotes: 0
Views: 155
Reputation: 20002
Your code has some mistakes. First see what you are doing before showing alternatives.
# How is $strs filled? Perhaps it is filled with `cat searchlist`.
# when you have spaces in your searchlist (not in this example), the
# line will be split in words.
# Perhaps you want `while IFS= read -r i; do ... done < searchlist`.
for i in $strs
do echo $i
# assigning the double quote to a var is overkill and of no use
k=\"
# Use `echo "$i"` avoiding strange things when i has wildcards.
# You are redirecting to tmp, that will be a file.
# You wanted to assign it to a var, you should have used
# tmp=$(echo ....print}')
echo $i | awk '{gsub(",",", ");print}' > tmp
# When you want to grep from a file, give the file as an argument:
# grep xxx file
# When tmp has spaces, you want "$tmp" (or "${tmp}"). This is
# not the same as $k$tmp$k, where the shell will expand the 3 vars.
less report150.txt | grep $k$tmp$k
# again: Use "$i" and not $k$i$k
echo $k$i$k
done
You can fix some errors for making this loop work, but you can do completely without loop using the solution of @karakfa or using awk
in one run:
awk '{gsub(",",", ");print}' searchlist
will give you the manipulated strings.
You can store them in a temp file and use grep -f
for finding it, or skip the temp file with the construction <(some_command_whoes_output_will_behave_as_a_file)
.
Together:
grep -f <(awk '{gsub(",",", ");print}' searchlist) report150.txt
Upvotes: 1
Reputation: 67477
pre process your search file with sed
, add optional space after comma and use grep
$ grep -Ef <(sed 's/,/& ?/g' search_list) file
Upvotes: 0