- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- awk - print range of fields
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-28-2007 03:17 AM
тАО06-28-2007 03:17 AM
I'm trying to rearrange a file where rows contain a variable number of fields.
How can I print "field 6 to end of line"?
example:
awk '{print $4 " " $2 " " $3 " " $6through$NF :'
Thanks
RayB
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-28-2007 03:28 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-28-2007 03:32 AM
тАО06-28-2007 03:32 AM
Re: awk - print range of fields
# awk '{printf "%s %s %s ",$4,$2,$3;for (i=7;i
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-28-2007 03:33 AM
тАО06-28-2007 03:33 AM
Re: awk - print range of fields
Ooops! Initialize i=6 not i=7...
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-28-2007 03:44 AM
тАО06-28-2007 03:44 AM
Re: awk - print range of fields
for(i=2;i<=NF;++i) {
if (i==2)
str=$i
else if (i==3)
str=str" "$i
else if(i==4)
str=$i" "str
else if (i==5)
printf("%s ",str)
else
printf(i
}' infile
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-28-2007 04:13 AM
тАО06-28-2007 04:13 AM
Re: awk - print range of fields
The elements start at 0
A little to my surprise i coudl not use [5..-1] to indicate a slice from the 6th fields thru the end.
Anyway... a perl solution:
perl -alpe '$_ = join " ",@F[3,1,2,5..@F-1]' x.tmp
-a = autosplit into array @F
-l = add newline to print
-p = print $_ before looping
-e = here comes the program
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-28-2007 05:09 AM
тАО06-28-2007 05:09 AM
Re: awk - print range of fields
I had never thought of the loop.
I'll have to get a book or at least a quick ref.
Thanks again.
Rayb
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-28-2007 05:16 AM
тАО06-28-2007 05:16 AM
Re: awk - print range of fields
> Hein: A little to my surprise i coudl not use [5..-1] to indicate a slice from the 6th fields thru the end.
Yes, but this would work:
# perl -alpe '$_ = join " ",@F[3,1,2,-4..-1]' x.tmp
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-28-2007 05:25 AM
тАО06-28-2007 05:25 AM
Re: awk - print range of fields
Sorry for this side line
And btw...
why not just TRY the perl solution.
It's just an other language/tool.
JRF,
>> # perl -alpe '$_ = join " ",@F[3,1,2,-4..-1]' x.tmp
Yes, the -n..-m does work but the start end end of the slice would both be relative to the end. So for example for 20 fields it would return 16 .. 20, not the desired 6 .. 20.
fwiw,
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-28-2007 05:32 AM
тАО06-28-2007 05:32 AM
Re: awk - print range of fields
Hein: > Yes, the -n..-m does work but the start end end of the slice would both be relative to the end. So for example for 20 fields it would return 16 .. 20, not the desired 6 .. 20.
Yes, I now see why you chose the approach you did.
Sandman: BTW, I had not thought of using conditional operators in 'awk'. That's very nice!
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-28-2007 05:47 AM
тАО06-28-2007 05:47 AM
Re: awk - print range of fields
Sometimes I'd like to try Perl but We don't use it except in some OS scripts and noboby in my shop is proficient.
It would become a maintenance problem.
Is there a -quick- way to learn Perl?
Which manual would you recommend?
Rayb
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-28-2007 06:03 AM
тАО06-28-2007 06:03 AM
Re: awk - print range of fields
I'd probably start with "Learning Perl" from the O'Reilly publishers. A compendium of links to good stuff is:
http://perlmonks.org/index.pl?node_id=284175
Like any language, the best way to really learn it is to use it.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-28-2007 06:59 AM
тАО06-28-2007 06:59 AM
Re: awk - print range of fields
Perhaps the worst quality of Perl is also its best quality: the ability to pack an enormous amount of processing into one line. This characteristic makes it very easy to produce Perl code that is extremely cryptic especially for beginners. It's really quite easy to write Perl in a style that is verbose and clear; it's just not done that often. I suppose that it's one of those "suffering (or at least confusion) is good for the soul" things.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-28-2007 07:23 AM
тАО06-28-2007 07:23 AM
Re: awk - print range of fields
Btw... if the contents of field $6 van not match fields $1 - $5, then a simple awk solution could be:
$ awk '{print $4,$2,$3,substr($0,index($0,$6))}' x.tmp
If $6 is a 'short' field then you can increase its uniqueness by pre end-or post pending a knwo separator:
$ awk '{print $4,$2,$3,substr($0,index($0," " $6 " "))}' x.tmp
Cheers,
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-28-2007 09:04 AM
тАО06-28-2007 09:04 AM
Re: awk - print range of fields
cut -f 4,2,3,6- inputfile >outputfile
cut assumes tab for the delimiter, but you can override with the -d option.
HTH
Rod Hills
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-28-2007 03:36 PM
тАО06-28-2007 03:36 PM
Re: awk - print range of fields
cut(1) is usually not a better tool than awk.
It has rigorous ideas of delimiters, if you have more than one between fields.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-29-2007 12:44 AM
тАО06-29-2007 12:44 AM
Re: awk - print range of fields
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО06-29-2007 12:48 AM
тАО06-29-2007 12:48 AM