淡泊以明志,寜靜以致遠

保留两个关键词之间的内容–单行或者多行

#!/bin/bash
# 能不能先并行(使用一个没有出现过特殊字符串作为结束标记),再分行?
# file1.txt
#"abcxdefgaxcdyeafgxyxabefght..."
#"fewewretgjkjk543yeafgxy4534ght..."
# 处理 删除第一个x前的内容和最后一个y后面的内容
# 预期结果 
#defgaxcdyeafgxyxabefght..."
#"fewewretgjkjk543yeafgx         
# 合并行 
# "公平"就是传说中的标记
sed 's/$/公平/g' tex1.txt | tr -d '\n' | sed 's/公平$//'>file1.txt
i=`sed 's/x.*$/x/' file1.txt`
j=`sed 's/^.*y/y/' file1.txt`
echo $i $j | sed -e "s/$i//" -e "s/$j$//" file1.txt | sed 's/公平/\n/g' 

几经修改,最后得到是代码是下面几行。算法不变:先并行,再行分,使用贪婪匹配 x.*y
#!/bin/bash
for i in text*.txt
do
cat $i | tr '\n' '\034' | grep -oP 'x.*y' | sed -e 's/^x//;s/y$//' | tr '\034' '\n' | tee out-$i
done
exit 0

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Connecting to %s

加关注

Get every new post delivered to your Inbox.