LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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 02-21-2013, 06:13 AM   #1
dazdaz
Member
 
Registered: Aug 2003
Location: Europe
Distribution: RHEL 6.x, 5.x, Fedora 20, Kubuntu 12.04, Solaris 10,8
Posts: 321

Rep: Reputation: 17
awk - from field n to end of line


How do I print from field 3 to the end of the line, when there may be more than 5 fields.


Therefore printing $3, $4, $5 is not a realistic method, because there might be more than 5 fields.

This is as far as I can get..

Code:
echo 66 77 88 99 | awk '{print $3}'
 
Old 02-21-2013, 06:23 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Have a look at this:
Code:
 echo 1 2 3 4 5 | awk '{ for (i=3; i<=NF; i++) print $i }'
NF is special in awk, it holds the number of fields for the line that is worked on.
 
2 members found this post helpful.
Old 02-21-2013, 06:28 AM   #3
dazdaz
Member
 
Registered: Aug 2003
Location: Europe
Distribution: RHEL 6.x, 5.x, Fedora 20, Kubuntu 12.04, Solaris 10,8
Posts: 321

Original Poster
Rep: Reputation: 17
Thanks, that's very useful to know.

How can I print the output without awk adding a new CR/LF each time.
 
Old 02-21-2013, 06:31 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Here is one way;
Code:
echo 1 2 3 4 5 | awk '{ for (i=3; i<=NF; i++) printf("%s ",$i) }END{ print"" }'
The END{ print"" } part is needed to move the cursor to the next line (try running it without that part).
 
1 members found this post helpful.
Old 02-21-2013, 07:10 AM   #5
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,093

Rep: Reputation: 288Reputation: 288Reputation: 288
Quote:
Originally Posted by dazdaz View Post
How do I print from field 3 to the end of the line, when there may be more than 5 fields.
I like to use cut for this purpose.

InFile ...
Code:
one two three four five 
one two three four five six seven 
one two three four
one two three four five six seven eight
one two three four five six
This code ...
Code:
cut -d" " -f3- $InFile > $OutFile
... produces this result ...
Code:
three four five 
three four five six seven 
three four
three four five six seven eight
three four five six
Daniel B. Martin
 
2 members found this post helpful.
Old 02-21-2013, 10:56 AM   #6
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,093

Rep: Reputation: 288Reputation: 288Reputation: 288
This is a revision of the awk posted by druuna.

InFile ...
Code:
one two three four five 
one two three four five six seven 
one two three four
one two three four five six seven eight
one two three four five six
This code ...
Code:
awk '{for (i=3;i<=NF;i++) printf("%s ",$i)} {print ""}' $InFile > $OutFile
... produced this result ...
Code:
three four five 
three four five six seven 
three four 
three four five six seven eight 
three four five six
Daniel B. Martin
 
1 members found this post helpful.
Old 02-24-2013, 04:14 PM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
I second daniel's cut suggestion, with the caveat that it can only be used for simple column extractions. You'll still need to use awk for any fancier line filtering, or if the delimiters are mixed/uneven.

(As an aside, I've often said to myself that awk really needs to add a few functions for manipulating ranges of fields and array elements. It would make this kind of thing so much easier.)

For another option, how about using a sed regex to remove the columns from the front that you don't want?

Code:
sed -r 's/^([^[:space:]]+[[:space:]]+){2}//' infile.txt
 
1 members found this post helpful.
Old 02-24-2013, 09:11 PM   #8
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,093

Rep: Reputation: 288Reputation: 288Reputation: 288
InFile ...
Code:
one two three four five 
one two three four five six seven 
one two three four
one two three four five six seven eight
one two three four five six
This code ...
Code:
awk '{$1=$2="";sub(/  /,"")}1' $InFile >$OutFile
... produced this result ...
Code:
three four five 
three four five six seven 
three four 
three four five six seven eight 
three four five six
As a matter of personal style I like to write code which does not use an explicit loop. Technical intuition suggests that "loopless" code runs faster but I have no evidence to support this hypothesis.

Daniel B. Martin
 
Old 02-24-2013, 09:49 PM   #9
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,503

Rep: Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893
Another alternative:
Code:
ruby -ane 'puts $F[2..-1].join(" ")' file
 
  


Reply


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
awk to remove the end considering the last field? patrick295767 Programming 18 08-01-2012 10:38 AM
awk error awk: line 2: missing } near end of file boscop Linux - Networking 2 04-08-2012 10:49 AM
awk command line: blank line record sep, new line field sep robertmarkbram Programming 4 02-21-2010 05:25 AM
Replace a field for a whole line in the same file, with awk. amwink Programming 12 11-13-2009 06:51 AM
AWK: print field to end, and character count? ridertech Linux - Newbie 1 05-07-2004 05:07 PM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration