LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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 01-08-2010, 09:53 AM   #1
sebelk
Member
 
Registered: Jan 2007
Posts: 66

Rep: Reputation: 15
awk printing from Nth field to last field


Hi,

How can print the, let's say 5nd field to the last field of every record (let's say we have 10 fields)?

I mean: I cant avoid to have to do:

print '{$5 $6 $7 $8 $9 $10}'

Thanks in advance!
 
Old 01-08-2010, 10:11 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Awk has not a syntax to specify a fields interval (ghostdog74, correct me if I'm wrong) but you can manage it using a for loop:
Code:
for ( i = 5 ; i <= NF; i++ ) print $i
On the other hand, if it is a one-liner better to use cut in place of awk:
Code:
$ cut -d' ' -f5- file

Last edited by colucix; 01-08-2010 at 10:13 AM.
 
Old 01-08-2010, 10:39 AM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
you can do it like colucix showed or you can set $1=$2=$3=$4="" (and if you don't mind an extra space , which you can gsub() away).
 
  


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 replace particular field vgr12386 Programming 9 06-05-2009 08:15 AM
php question, how do I get a return from a field within a field? cherrington Programming 11 04-29-2009 02:27 AM
deleting a field using awk jkeertir Linux - Newbie 5 04-13-2008 11:55 PM
My field separator changes when using awk Helene Programming 3 05-01-2004 09:10 AM
Two field seperators in awk?? Astro Programming 2 11-09-2003 11:12 AM


All times are GMT -5. The time now is 08:49 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration