获取带有echo的列表并在grep中使用它

时间:2016-09-16 18:56:41

标签: bash grep echo

我有一个列表列表,例如:

125,99,1]
109,124,70,1]
71,62,1]
108,90,1]

我想在每个文件中使用grep。 问题是在文件中这些列表就像

125 ,99, 1]
109, 124, 70, 1]
71, 62, 1]
108, 90, 1]

即。每个逗号后面都有一个空格。我想更改我的列表然后使用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

但是,tmp的输出只是一无所获。 我感谢任何帮助。

由于

阿夫欣

这里也是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

输出应该是:

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

2 个答案:

答案 0 :(得分:1)

您的代码有一些错误。首先看看你在做什么之前做了什么。

# 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

您可以修复一些错误以使此循环工作,但您可以使用@karakfa的解决方案或在一次运行中使用awk完全无需循环:

awk '{gsub(",",", ");print}' searchlist

会给你操纵的字符串。

您可以将它们存储在临时文件中并使用grep -f进行查找,或者使用构造<(some_command_whoes_output_will_behave_as_a_file)跳过临时文件。 一起:

grep -f <(awk '{gsub(",",", ");print}' searchlist) report150.txt

答案 1 :(得分:0)

使用sed预处理您的搜索文件,在逗号后添加可选空格并使用grep

$ grep -Ef <(sed 's/,/& ?/g' search_list) file