Spread
Spread

Reputation: 49

Not same result on the same command in script and out of the script

When i pass this command out of 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

The result is :

Micro Pratique N°247 Avril 2017.pdf

But when i pass this command in my script and stock 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 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 it cuts my first word "Micro" ?

Here the result of 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

Upvotes: 4

Views: 63

Answers (1)

karakfa
karakfa

Reputation: 67497

your file is fixed-width format, not properly delimited. You can utilize gawk's FIELDWIDTHS variable. Since the other awks don't support it, here is another approach, assuming you know the header of the field you're 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

to get the last entry record either tail or

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

Upvotes: 2

Related Questions