求shell脚本,删除一个txt文档中包含另外一个txt文档的行。
2个回答
展开全部
#!/usr/bin/ksh
###[局部方法]########################
SedFileCont()
{
_fileName=$1
_fromContent=$2
_toContent=$3
vi $_fileName <<! >/dev/null
:1,%s/$_fromContent/$_toContent/g
:x
!
}
###[局部方法]########################
#需要删除行的txt:src.txt
#执行匹配行的txt:std.txt
###[执行修改]########################
cat std.txt|while read strLine
do
cnt=0
cat src.txt|grep ${strLine}|wc -l|read cnt
[ $cnt -eq 0 ] && continue
[ $cnt -gt 0 ] && SedFileCont src.txt ${strLine} "\c"
done
###[执行修改]########################
###[局部方法]########################
SedFileCont()
{
_fileName=$1
_fromContent=$2
_toContent=$3
vi $_fileName <<! >/dev/null
:1,%s/$_fromContent/$_toContent/g
:x
!
}
###[局部方法]########################
#需要删除行的txt:src.txt
#执行匹配行的txt:std.txt
###[执行修改]########################
cat std.txt|while read strLine
do
cnt=0
cat src.txt|grep ${strLine}|wc -l|read cnt
[ $cnt -eq 0 ] && continue
[ $cnt -gt 0 ] && SedFileCont src.txt ${strLine} "\c"
done
###[执行修改]########################
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#!/bin/bash
#
# assume /tmp/a.txt is the file for keywords
# /tmp/b.txt is the file to be operated
awk '
BEGIN{
i=0
while(getline < "/tmp/a.txt" > 0) {
keyword[i++] = $0
}
keysize=i
}
{
flag=0
for(i=0;i<keysize;i++)
if($0 ~ keyword[i])
flag=1
if(flag==0)
print
}' /tmp/b.txt > /tmp/b.txt.tmp
# replace /tmp/b.txt with modified /tmp/b.txt.tmp
mv /tmp/b.txt.tmp /tmp/b.txt
把上述代码保存为脚本。调试:
debian:~/prog/shell# cat /tmp/a.txt
abc def
def
kllmn
delete
debian:~/prog/shell# cat /tmp/b.txt
#!/bin/bash
delete
kllmn
执行脚本后:
debian:~/prog/shell# cat /tmp/b.txt
#!/bin/bash
#
# assume /tmp/a.txt is the file for keywords
# /tmp/b.txt is the file to be operated
awk '
BEGIN{
i=0
while(getline < "/tmp/a.txt" > 0) {
keyword[i++] = $0
}
keysize=i
}
{
flag=0
for(i=0;i<keysize;i++)
if($0 ~ keyword[i])
flag=1
if(flag==0)
}' /tmp/b.txt > /tmp/b.txt.tmp
# replace /tmp/b.txt with modified /tmp/b.txt.tmp
mv /tmp/b.txt.tmp /tmp/b.txt
把上述代码保存为脚本。调试:
debian:~/prog/shell# cat /tmp/a.txt
abc def
def
kllmn
delete
debian:~/prog/shell# cat /tmp/b.txt
#!/bin/bash
delete
kllmn
执行脚本后:
debian:~/prog/shell# cat /tmp/b.txt
#!/bin/bash
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询