Jump to content
 English      
HP.com Home Products and Services Support and Drivers Solutions How to Buy
» Contact HP
     Forums advanced search
HP.com Home
IT Resource Center Forums > HP-UX > general

Awk print question

» 

IT Resource Center

» Login
» Register
» My profile
» Search knowledge base
» Forums
» Patch database
» Download drivers, software and firmware
» Warranty check
» Support Case Manager
» Software Update Manager
» Training and Education
» More maintenance and support options
» Online help
» Site map

Member icons
 
 HP moderator  HP moderator
 Expert in this area  Expert in this area
Member status
ITRC Pro ITRC Pro
250 points
ITRC Graduate ITRC Graduate
500 points
ITRC Wizard ITRC Wizard
1000 points
ITRC Royalty ITRC Royalty
2500 points
ITRC Pharaoh ITRC Pharaoh
7500 points
Olympian Olympian
20000 points
1-Star Olympian 1-Star Olympian
40000 points
2-Star Olympian 2-Star Olympian
80000 points
»  How to earn points
»  Support forums FAQs
Question status
Magical answer Magical answer
Message with a response that solved the author's question
Favorites status
Add to my favorites Add to my favorites
Delete from my favorites Delete from my favorites
This thread has been closed Thread closed
 

Content starts here
   Create a new message    Receive e-mail notification if a new reply is posted  Reply to this message
Author Subject: Awk print question      Add to my favorites
Belinda Dermody
Nov 20, 2001 22:00:44 GMT   

I have a file that the first 4 fields are variable length and each only have 1 data item/word
FileName Tape#REC# RptName Date and the last field is comments variable length with words. The file is not delimited. I know how to use the printf sequence in awk to format the output for it to look nice, but the last field is comments and could be any number of words or stuff. Is there a way in awk to say use $5, etc ... till the end of the record and print it as one field.
Note: If you are the author of this question and wish to assign points to any of the answers, please login first.For more information on assigning points ,click here


Sort Answers By: Date or Points
Craig Rants This member has accumulated 2500 or more points
Nov 20, 2001 22:25:35 GMT  3 pts

This may not be exactly what you are looking for but I think it will work. The output will need a little work.

awk '{for (i = NF; i > 4; i--) print $i}'

Good Luck,
C
Marco Paganini Expert in this area This member has accumulated 500 or more points
Nov 20, 2001 22:38:00 GMT  10 pts

Hello,

This "one-liner" awk program should do the trick for you:

awk '{ rest=""; for (ix = 5; ix <= NF; ix++) { rest = rest " " $ix } printf("%-10.10s %-10.10s %-10.10s %-10.10s %s\n",$1,$2,$3,$4,rest) }' <your_file

You may change the %-10.10s format to something that gives you a better aesthetic result.

Hope it helps,
Paga
Sridhar Bhaskarla This member has accumulated 20000 or more points
Nov 20, 2001 22:42:49 GMT  3 pts

James,

Try this way.

If your file is called my_text and the first four fields are fixed (say by "space") then you would do like this

awk '{print substr($0, index($0,$4)) }' my_text

-Sri
Sridhar Bhaskarla This member has accumulated 20000 or more points
Nov 20, 2001 23:05:04 GMT    Unassigned

Hi (again),

A small correction. Make it $5 in the above awk statement otherwise it will print $4 + the comments.

-Sri
Belinda Dermody
Nov 21, 2001 16:41:46 GMT    N/A: Question Author

Thanks for the response, Craigs and Sridhar did not work or provide the output that I was looking for, Marco works great, a long one liner though and I have to change the formating parameters.

Thanks
Sridhar Bhaskarla This member has accumulated 20000 or more points
Nov 21, 2001 16:52:48 GMT    Unassigned

James,

Probably it would have helped if you provided a sample input file.

If your input file is like this

testfile 23#1212 test.report 11/12/01 this is ofcourse my comment

Then

awk '{print substr($0, index($0,$5)) }' input_file

will print out

this is ofcourse my comment

It's a very simple logic that we are printing the substring starting from $5 (this) no matter what spaces or characters are there later. You don't have to deal with the formatting.

-Sri
 
Create a new message    Receive e-mail notification if a new reply is posted   Reply to this message
 
 
Printable version
Privacy statement Using this site means you accept its terms
© 2010 Hewlett-Packard Development Company, L.P.