多个for循环中的sed命令用法

时间:2015-12-17 11:50:15

标签: linux bash

我正在编写一个shell脚本,如下所示:

#!/bin/bash
try="7200005781
8110892533
9585055818
9942609990
9943985228"
filename="TAMILNADU,917373342777,POSTPAID
TAMILNADU,917639403547,POSTPAID
TAMILNADU,917639989185,POSTPAID
TAMILNADU,917667882223,POSTPAID
TAMILNADU,918012888899,POSTPAID
TAMILNADU,918098909992,POSTPAID
TAMILNADU,917200005781,POSTPAID
TAMILNADU,918110892533,POSTPAID
TAMILNADU,919585055818,POSTPAID
TAMILNADU,919942609990,POSTPAID
TAMILNADU,919944040565,POSTPAID
TAMILNADU,919943985228,POSTPAID"

#echo -e "\n${try[@]}" -> works
#echo -e "\n"           -> works
#echo -e "\n${filename[@]}" -> works

for j in "${try[@]}"
do
    for k in "${filename[@]}"
    do     
        sed -i 's/$j//g' $k > master.txt
    done
done

我正在尝试匹配'尝试'和'文件名'

如果有任何数量的比赛来自'尝试'在'文件名'

我需要从' filename'

中删除该条目

请帮助我。

2 个答案:

答案 0 :(得分:0)

如果你在文件中有记录,这很简单:

{
  "updaters": [
      {
        "type": "real-time-alerts",
        "sourceType": "gtfs-http",
        "frequencySec": 10,
        "url": "http://my-rt-fee-publisher.com/feeds",
        "defaultAgencyId": "Agency name"
      }
  ]
}

答案 1 :(得分:0)

这是在您拥有的脚本中执行此操作的一种方法,但需要编写一个文件。

#!/bin/bash
try="7200005781
8110892533
9585055818
9942609990
9943985228"
filename="TAMILNADU,917373342777,POSTPAID
TAMILNADU,917639403547,POSTPAID
TAMILNADU,917639989185,POSTPAID
TAMILNADU,917667882223,POSTPAID
TAMILNADU,918012888899,POSTPAID
TAMILNADU,918098909992,POSTPAID
TAMILNADU,917200005781,POSTPAID
TAMILNADU,918110892533,POSTPAID
TAMILNADU,919585055818,POSTPAID
TAMILNADU,919942609990,POSTPAID
TAMILNADU,919944040565,POSTPAID
TAMILNADU,919943985228,POSTPAID"

echo "$filename" > /tmp/filename_zz.txt
echo "$try" | xargs -L 1 -I{} grep {} /tmp/filename_zz.txt 
rm /tmp/filename_zz.txt

编辑:没有意识到我们想删除这些项目,而不是匹配它们......用这些行代替上面脚本的最后3行......归功于@Florin

echo "$filename" > /tmp/filename_zz.txt
echo "$try" > /tmp/try_zz.txt
grep -F -vf /tmp/try_zz.txt /tmp/filename_zz.txt
rm /tmp/try_zz.txt
rm /tmp/filename_zz.txt