Not the same result for the same command in script and from script

When I pass this command from my script:

transmission-remote -n 'transmission:transmission' -l | awk '{ s = ""; for (i = 10; i <= NF; i++) s = s $i " "; print s }' | grep "\b\b" | tail -1

Result:

Micro Pratique N°247 Avril 2017.pdf

But when I pass this command in my script and put the result in a variable like this:

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`

The result is not the same! Here is the script with "set -x":

+++ 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 '

Why does it shorten my first word "Micro"?

Here is the result 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
+4
source share
1 answer

Your file has a fixed-width format, not a delimited one. You can use a variable gawk FIELDWIDTHS. Since others awkdo not support it, here is another approach, assuming you know the title of the field you are trying to extract

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

awk 'NR==1        {p=match($0,"Name")} 
     length($0)>p {last=substr($0,p)} 
     END          {print last}' file

Micro Pratique N°247 Avril 2017.pdf
+2

Source: https://habr.com/ru/post/1673497/


All Articles