从行中提取文本

时间:2013-10-28 09:13:39

标签: bash

我需要帮助从文件中提取行的某些部分。

以下是我的文件的样子:

testfile.txt
This is a test line 1 $#%#
This is a test line 2 $#%#
This is a test line 3 $#%#
This is a test line 4 $#%#
This is a test line 5 $#%#
This is a test line 6 $#%#
This is a test line 7 $#%#

这是我的bash脚本:

#!/bin/bash

while read line
do
#echo $line
FilterString=${line:22:26}
echo $FilterString>>testfile2.txt
done<testfile.txt

上面的脚本获取字符串$#%#并写入临时文件

我的问题:

我希望除了字符串$#%#之外的所有内容都写入文件,而不是编写字符串$#%#。 所以我希望我的最终输出文件看起来像:

testfile.txt
This is a test line 1 
This is a test line 2 
This is a test line 3 
This is a test line 4 
This is a test line 5 
This is a test line 6 
This is a test line 7 

还请建议我使用它的最佳工具

提前致谢。

6 个答案:

答案 0 :(得分:5)

如果它只是您要删除的最后一个字段,则可以使用awk

$ awk 'NF=NF-1' file
This is a test line 1
This is a test line 2
This is a test line 3
This is a test line 4
This is a test line 5
This is a test line 6
This is a test line 7

它减少了一个字段数,因此不考虑最后一个字段。

然后,它会执行awk {print $0}的默认操作。

要重定向到文件,请使用awk 'NF=NF-1' file > new_file


更新

根据你的评论

  

在我的情况下,它并不总是最后一个字段,也可能是   然而,在其他领域之间的预定义位置(始终固定   位置)。

然后您可以使用以下awk语法:

awk -v c=col_num '{$(c)=""}1' file

其中col_num可以手动设置,如:

$ awk -v c=3 '{$(c)=""}1' file
This is  test line 1 $#%#
This is  test line 2 $#%#
This is  test line 3 $#%#
This is  test line 4 $#%#
This is  test line 5 $#%#
This is  test line 6 $#%#
This is  test line 7 $#%#
$ awk -v c=5 '{$(c)=""}1' file
This is a test  1 $#%#
This is a test  2 $#%#
This is a test  3 $#%#
This is a test  4 $#%#
This is a test  5 $#%#
This is a test  6 $#%#
This is a test  7 $#%#

你也可以像这样使用cut,省略你想要跳过的字段:

$ cut -d' ' -f1,2,3,4,5,6 file
This is a test line 1
This is a test line 2
This is a test line 3
This is a test line 4
This is a test line 5
This is a test line 6
This is a test line 7

$ cut -d' ' -f1,2,3,5,6,7 file
This is a line 1 $#%#
This is a line 2 $#%#
This is a line 3 $#%#
This is a line 4 $#%#
This is a line 5 $#%#
This is a line 6 $#%#
This is a line 7 $#%#

答案 1 :(得分:2)

说:

FilterString=${line:22:26}

选择来打印该行的$#%#部分。

你可以说:

FilterString=${line:0:21}

打印该行的所需部分。或者,您可以说:

FilterString=${line//\$#%#/}

(请注意$符号需要转义)


使用sed,您可以说:

sed 's/ $#.*//g' testfile.txt

-i选项提供给sed会使更改就地

sed -i 's/ $#.*//g' testfile.txt

根据您的comment,如果您要从文件中的固定位置中删除文字,使用cut可能会简化操作。话说:

cut -b1-21,27- testfile.txt

会从文件22-26中的所有行中删除字节testfile.txt(包括)。

答案 2 :(得分:1)

  

Instead of writing the string "$#%#" i want everything except string "$#%#" written to file.

可以使用sed inline完成:

sed -i.bak 's/ *\$#%#//g' testfile.txt

答案 3 :(得分:1)

你非常接近:

FilterString=${line:0:22}

或者只是过滤垃圾:

FilterString=${line% \$#%#}

答案 4 :(得分:1)

试一试:

#!/bin/sh

while read line
do
#echo $line
FilterString=`python -c "s='$line';print s[:s.find('$')]"`
echo $FilterString>>testfile2.txt`

此样本可以使用不同的长度。例如,使用文件上下文:

...
This is a test line 6 $#%#
This is a test line 1024 $#%#
...

您将获得下一个结果:

This is a test line 6
This is a test line 1024

答案 5 :(得分:0)

感谢所有答案:

将使用基于@ devnull答案的脚本:

#!/bin/bash
while read line
do
#echo $line
#FilterString=${line:22:26}
echo $line | cut -b1-20,27- >>testfile2.txt
done<testfile

因此,如果文件看起来像

testfile.txt
This is a test line 1 $#%# more text
This is a test line 2 $#%# more text
This is a test line 3 $#%# more text
This is a test line 4 $#%# more text
This is a test line 5 $#%# more text
This is a test line 6 $#%# more text
This is a test line 7 $#%# more text

然后输出将是:

testfile2.txt
This is a test line  more text
This is a test line  more text
This is a test line  more text
This is a test line  more text
This is a test line  more text
This is a test line  more text
This is a test line  more text

这正是我想要的