Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 10-24-2008, 11:51 AM   #1
Registered: Oct 2002
Posts: 496

Rep: Reputation: 62
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, 12:13 PM   #2
Senior Member
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Perhaps cut will work?
... | 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, 12:22 PM   #3
Senior Member
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810

Rep: Reputation: 231Reputation: 231Reputation: 231
Just concatenate the last two together :
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 :

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

Last edited by bgeddy; 10-24-2008 at 12:24 PM.
Old 10-24-2008, 12:33 PM   #4
Registered: Oct 2002
Posts: 496

Original Poster
Rep: Reputation: 62
Originally Posted by bgeddy View Post
Just concatenate the last two together :
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 :

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.

Old 10-24-2008, 02:29 PM   #5
Registered: Jun 2008
Posts: 235

Rep: Reputation: 46

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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 02:41 AM
gawk/awk/tcsh - how to convert large number to human readable (Mb,Gb, etc)? BrianK Programming 23 10-30-2010 05:20 AM
Help with pattern matching, sorting data with awk/gawk or perl placem Programming 2 09-11-2008 03:26 PM
LXer: Get started coding with GAWK and AWK LXer Syndicated Linux News 0 09-26-2006 03:54 PM
Deleting a line with gawk/awk caps_phisto Linux - General 4 11-06-2004 03:31 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:10 PM.

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