Bash 从行中提取文本
我需要帮助从文件中提取行的某些部分 以下是我的文件的外观:Bash 从行中提取文本,bash,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脚本: #!/b
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
还请建议我做这件事的最佳工具
提前感谢。如果这只是您要删除的最后一个字段,您可以使用
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'文件>新建文件
使现代化 根据你的评论
在我的例子中,它并不总是最后一个字段,它也可能是最后一个字段 但在预定义位置的其他字段之间(始终固定 位置) 然后可以使用以下
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 $#%#
如果它只是要删除的最后一个字段,则可以使用
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'文件>新建文件
使现代化 根据你的评论
在我的例子中,它并不总是最后一个字段,它也可能是最后一个字段 但在预定义位置的其他字段之间(始终固定 位置) 然后可以使用以下
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 $#%#
说:
FilterString=${line:22:26}
您选择打印该行的部分内容
你可以说:
FilterString=${line:0:21}
sed 's/ $#.*//g' testfile.txt
打印行的所需部分。或者,你可以说:
FilterString=${line//\$#%#/}
(请注意,$
符号需要转义)
使用
sed
,您可以说:
FilterString=${line:0:21}
sed 's/ $#.*//g' testfile.txt
将-i
选项提供给sed
将使更改到位:
根据您的建议,如果要从文件中的固定位置删除文本,使用
cut
可能会简化操作。说:
cut -b1-21,27- testfile.txt
将从文件testfile.txt
中的所有行中删除字节22-26
(包括),方法是:
FilterString=${line:22:26}
您选择打印该行的部分内容
你可以说:
FilterString=${line:0:21}
sed 's/ $#.*//g' testfile.txt
打印行的所需部分。或者,你可以说:
FilterString=${line//\$#%#/}
(请注意,$
符号需要转义)
使用
sed
,您可以说:
FilterString=${line:0:21}
sed 's/ $#.*//g' testfile.txt
将-i
选项提供给sed
将使更改到位:
根据您的建议,如果要从文件中的固定位置删除文本,使用
cut
可能会简化操作。说:
cut -b1-21,27- testfile.txt
将从文件testfile.txt
中的所有行中删除字节22-26
(包括)
而不是将字符串“$#%#”写入文件。我希望将除字符串“$#%#”之外的所有内容写入文件。
可以使用sed内联完成:
sed -i.bak 's/ *\$#%#//g' testfile.txt
而不是将字符串“$#%#”写入文件。我希望将除字符串“$#%#”之外的所有内容写入文件。
可以使用sed内联完成:
sed -i.bak 's/ *\$#%#//g' testfile.txt
你非常接近:
FilterString=${line:0:22}
或者只是过滤垃圾:
FilterString=${line% \$#%#}
你非常接近:
FilterString=${line:0:22}
或者只是过滤垃圾:
FilterString=${line% \$#%#}
试试看:
#!/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
试试看:
#!/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
感谢你的回答,伙计们: 将使用基于@devnull答案的脚本:
#!/bin/bash
while read line
do
#echo $line
#FilterString=${line:22:26}
echo $line | cut -b1-20,27- >>testfile2.txt
done<testfile
然后输出为:
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
这正是我想要的感谢各位的回答: 将使用基于@devnull答案的脚本:
#!/bin/bash
while read line
do
#echo $line
#FilterString=${line:22:26}
echo $line | cut -b1-20,27- >>testfile2.txt
done<testfile
然后输出为:
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
这正是我想要的在我的情况下,它并不总是最后一个字段,它也可能位于其他字段之间,但是在预定义位置(始终固定位置)OK@Rockwire,然后您可以使用列名。我将在5分钟内进行更新,类似于
awk-vcol=$col'$(col)=''file
的文件应该是bash变量。在我的情况下,它并不总是最后一个字段,也可能位于其他字段之间,但在预定义位置(始终固定位置)OK@Rockwire,然后您可以使用列名。我将在5分钟内更新,类似于awk-vcol=$col'$(col)=''file
的文件,作为一个bash变量,应该可以了。+1表示一个简洁的问题+1表示简洁的问题谢谢你的回答,我将使用“cut”表示感谢你的回答,我将使用“cut”