当我从我的脚本中传出此命令时:
transmission-remote -n 'transmission:transmission' -l | awk '{ s = ""; for (i = 10; i <= NF; i++) s = s $i " "; print s }' | grep "\b\b" | tail -1
结果是:
Micro Pratique N°247 Avril 2017.pdf
但是当我在我的脚本中传递此命令并将结果存储在这样的变量中时:
test=`transmission-remote -n 'transmission:transmission' -l | awk '{ s = ""; for (i = 10; i <= NF; i++) s = s $i " "; print s }' | grep "\b\b" | tail -1`
结果不一样!这里的脚本是&#34; set -x&#34; :
+++ transmission-remote -n transmission:transmission -l
+++ awk '{ s = ""; for (i = 10; i <= NF; i++) s = s $i " "; print s }'
+++ grep '\b\b'
+++ tail -1
++ test='Pratique N°247 Avril 2017.pdf '
为什么它会削减我的第一个字&#34; Micro&#34; ?
此处为transmission-remote -n transmission:transmission -l
的结果:
ID Done Have ETA Up Down Ratio Status Name
1 100% 8.30 GB Done 1.0 0.0 2.8 Seeding Ghost In The Shell 2 Innocence 2003 MULTi VFF BluRay 1080p AC3 x264
65 100% 4.86 GB Done 0.0 0.0 2.9 Idle WWE.RAW.2017.03.20.720p.HDTV.x264-Ebi.mp4
71 100% 2.89 GB Done 0.0 0.0 2.9 Idle WWE.Smackdown.Live.2017.03.21.720p.HDTV.x264-Ebi.mp4
106 100% 4.80 GB Done 0.0 0.0 0.0 Seeding WWE.RAW.2017.03.27.720p.HDTV.x264-Ebi.mp4
107 100% 2.90 GB Done 0.0 0.0 0.9 Idle WWE.SmackDown.Live.2017.03.28.720p.HDTV.x264-NWCHD.mp4
113 0% None Unknown 0.0 0.0 None Idle Micro Pratique N°247 Avril 2017.pdf
Sum: 23.75 GB 1.0 0.0
答案 0 :(得分:2)
您的文件是固定宽度格式,没有正确分隔。您可以使用gawk
的{{1}}变量。由于其他FIELDWIDTHS
不支持,
这是另一种方法,假设您知道要提取的字段的标题
awk
获取最后一个条目记录awk 'NR==1 {p=match($0,"Name")}
length($0)>p {print substr($0,p)}' file
Name
Ghost In The Shell 2 Innocence 2003 MULTi VFF BluRay 1080p AC3 x264
WWE.RAW.2017.03.20.720p.HDTV.x264-Ebi.mp4
WWE.Smackdown.Live.2017.03.21.720p.HDTV.x264-Ebi.mp4
WWE.RAW.2017.03.27.720p.HDTV.x264-Ebi.mp4
WWE.SmackDown.Live.2017.03.28.720p.HDTV.x264-NWCHD.mp4
Micro Pratique N°247 Avril 2017.pdf
或
tail