For Programmers: Free Programming Magazines  


Home > Archive > AWK > May 2006 > print from a field to EOL









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author print from a field to EOL
kpcamp

2006-05-23, 6:57 pm

Ok I am trying to create a file from output field "3" and "9 to EOL"

while read line;do
ls -ltr "${line}" |awk {'print$3":"$9'} #Need to have $9 to EOL
done < $file


Each line getting sent into awk looks like

-rw-rw-rw- 1 userid groupid 4497952 May 16 09:31
/Volumes/Neuro/Neurogenomics/Neurodegenerative Unit/Staff/tom jones/Red
Hot Chili Peppers/Stadium Arcadium [Disc 1]/1-08 Torture Me.mp3


Any ideas? Thanks in advance

Harlan Grove

2006-05-23, 6:57 pm

kpcamp wrote...
>Ok I am trying to create a file from output field "3" and "9 to EOL"
>
>while read line;do
> ls -ltr "${line}" |awk {'print$3":"$9'} #Need to have $9 to EOL
>done < $file

....

Since you're dealing with ls output, your fields are fixed length. Use
substr to get the substring of $0 beginning at the first character of
$9 and print that rather than $9.

kpcamp

2006-05-23, 6:57 pm

what does that syntax look like? Thanks

Klaus Alexander Seistrup

2006-05-23, 6:57 pm

kpcamp wrote:

> Ok I am trying to create a file from output field "3" and "9 to EOL"


#v+

while read f1 f2 f3 f4 f5 f6 f7 f8 rest
do
echo "${f3} ${rest}"
done

#v-

Cheers,

--
Klaus Alexander Seistrup
SubZeroNet, Copenhagen, Denmark
http://magnetic-ink.dk/
Eric Pement

2006-05-23, 6:57 pm

kpcamp wrote:
> Ok I am trying to create a file from output field "3" and "9 to EOL"
>
> while read line;do
> ls -ltr "${line}" |awk {'print$3":"$9'} #Need to have $9 to EOL
> done < $file
>
>
> Each line getting sent into awk looks like
>
> -rw-rw-rw- 1 userid groupid 4497952 May 16 09:31
> /Volumes/Neuro/Neurogenomics/Neurodegenerative Unit/Staff/tom jones/Red
> Hot Chili Peppers/Stadium Arcadium [Disc 1]/1-08 Torture Me.mp3
>
>
> Any ideas? Thanks in advance


In the 'ls' utility, the fields are actually fixed-length fields, so
you can use the substr() function instead of the space-separated
parameters ($1, $2, $3 ...) to select your fields. Try something like
this on the awk side:

... | awk '{print substr($0,15,8), substr($0,52)}'

If the position or length values don't match your settings, alter them
as needed. HTH.

--
Eric

Harlan Grove

2006-05-23, 6:57 pm

kpcamp wrote...
>what does that syntax look like? Thanks


Always quote relevant context.

Hard to be precise since your sample ls output wrapped to multiple
lines, but you could tweak

awk '{ print $3 ":" substr($0, 54) }'

kpcamp

2006-05-23, 6:57 pm

Thanks
awk '{ print $3 ":" substr($0, 56) }'

did the trick

Chris F.A. Johnson

2006-05-23, 6:57 pm

On 2006-05-23, Harlan Grove wrote:
> kpcamp wrote...

This really belongs in comp.unix.shell. Followup set.

while read line;do
ls -ltr "${line}"
done < "$file" | tr -s ' ' | cut -d ' ' -f3,9-

Or:

IFS='
'
ls -ltr $( cat "$file" ) | tr -s ' ' | cut -d ' ' -f3,9-
[color=darkred]
> ...
>
> Since you're dealing with ls output, your fields are fixed length.


Modern versions of ls do not produce fixed-length fields; they
produce whitespace-separated fields. The actual width of any field
(except the last) is determined by the longest entry in the field.

> Use substr to get the substring of $0 beginning at the first
> character of $9 and print that rather than $9.




--
Chris F.A. Johnson, author <http://cfaj.freeshell.org>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com