Register
It is currently Wed Apr 16, 2014 3:54 pm

awk column to end of line


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Sat Sep 04, 2010 2:50 pm   

Joined: Sat Sep 04, 2010 2:45 pm
Posts: 1
Code:
#!/bin/bash
ls -lhGg | while read line; do echo "$line"; done | awk ' { print $3" "$6 } '


I want to spit out only column 3 and columns greater then or equal to 6.

filenames could have any number of words in them, so I can't stick to just 1 column.

as the code stands now, it works unless the filename has blank space in it, so files with 1 word work, the rest don't.

I read somewhere of something like $6,EOF but that just spits out column 6 then $0 (the whole line) after it.


Top
 Profile  
 PostPosted: Sat Sep 04, 2010 3:48 pm   
User avatar

Joined: Sun Jun 27, 2010 12:57 am
Posts: 192
There is no easy way to specify a range in awk without using some for-loop.
I suggest the following:

Code:
ls -lhGg | awk '{ printf $3; for (i=6; i<NF; i++) printf " "$i; print " "$NF; }'


Top
 Profile  
 PostPosted: Sat Jun 11, 2011 9:04 am   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
Also, you can use the ternary operator :

Code:
awk '{ printf("%s ", $3); for (i = 6; i <= NF; i++) printf((i != NF) ? "%s " : "%s\n", $i) }' < <(ls -lhGg)


(parsing ls(1) is bad)


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP