LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-24-2008, 10:51 AM   #1
sharky
Member
 
Registered: Oct 2002
Posts: 569

Rep: Reputation: 84
awk or gawk question


I'm piping a file to awk in order to filter the required fields. The last column in the file will sometimes have spaces. In the example below consider the 'col4 -i' as the last column.

col1 col2 col3 col4 -i

There are no delimiters in the report except for spaces so having spaces in the last column is a dirty trick. )

Anyway, if I want to return from awk $2 and $4 then I will not get all of the last field. I've been looking in my trusty Sed & Awk O'reilly book for a way to return $2,$4 and the remainder of the line but haven't found it. Is there a way to do this with awk?
 
Old 10-24-2008, 11:13 AM   #2
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Perhaps cut will work?
Code:
... | cut -d" " -f 2,4-
That should print out the second and fourth fields, even if the 4th field contains spaces (or whatever character you specify with the -d flag).
 
Old 10-24-2008, 11:22 AM   #3
bgeddy
Senior Member
 
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810

Rep: Reputation: 232Reputation: 232Reputation: 232
Just concatenate the last two together :
Code:
echo "col1 col2 col3 col4 -i" | awk '{print $2,$4$5}'
If there's no $5 it's just ignored.

If you want to keep the space then just insert it :

Code:
echo "col1 col2 col3 col4 -i" | awk '{print $2,$4" "$5}'

Last edited by bgeddy; 10-24-2008 at 11:24 AM.
 
Old 10-24-2008, 11:33 AM   #4
sharky
Member
 
Registered: Oct 2002
Posts: 569

Original Poster
Rep: Reputation: 84
Quote:
Originally Posted by bgeddy View Post
Just concatenate the last two together :
Code:
echo "col1 col2 col3 col4 -i" | awk '{print $2,$4$5}'
If there's no $5 it's just ignored.

If you want to keep the space then just insert it :

Code:
echo "col1 col2 col3 col4 -i" | awk '{print $2,$4" "$5}'
I think this is it. Some times my last 'field' may have more than one space but I can test that with 'awk {print $#}' | sort | uniq' where I keep increasing # till I have no output. Then I know where to set the last value.

I love one liners.

Thanks,
 
Old 10-24-2008, 01:29 PM   #5
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 47
Moin,

why don't you do the concatenation completely in awk?
Code:
awk ' { last = "";
for (i=4; i<=NF; i++) last=last $i; 
print $2, last; } ' /path/to/your/file
Jan
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Static awk or gawk binary rbautch Linux - Software 5 06-14-2011 01:41 AM
gawk/awk/tcsh - how to convert large number to human readable (Mb,Gb, etc)? BrianK Programming 23 10-30-2010 04:20 AM
Help with pattern matching, sorting data with awk/gawk or perl placem Programming 2 09-11-2008 02:26 PM
LXer: Get started coding with GAWK and AWK LXer Syndicated Linux News 0 09-26-2006 02:54 PM
Deleting a line with gawk/awk caps_phisto Linux - General 4 11-06-2004 02:31 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:06 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration