View the Most Wanted LQ Wiki articles.
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 02-23-2007, 03:27 PM   #1
Registered: Aug 2006
Posts: 39

Rep: Reputation: 15
simple awk question

Hi gang. I have an awk one-liner:

grep $STRING $FILE | awk '{printf "%-6s %s\n", $11,$13}' | sort

Its output is:


How can I add a column so I get this:

1 6001
2 6002
3 6003

Thanks for your interest in this important matter.

Old 02-23-2007, 04:29 PM   #2
Senior Member
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
Presuming I understand what you're asking, you can use awk's special variable - NR. It contains the number of input records awk has processed, and if you print it you can use it as a "record count".

Might mess up your sort though. It all depends on your data and what you're trying to do exactly...
Old 02-23-2007, 04:42 PM   #3
Senior Member
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,170

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
Or you could pipe the sort output to another awk script, vis:

. . . | sort | gawk '{print NR ": " $0};'

Last edited by PTrenholme; 02-23-2007 at 04:43 PM.
Old 02-23-2007, 06:37 PM   #4
Registered: Aug 2006
Distribution: slack
Posts: 323

Rep: Reputation: 30
awk '/'$STRING'/ {printf "%-6s %s\n", $11,$13}' $FILE | sort | nl


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 On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple question about sed or awk setianusa Programming 2 09-16-2005 03:57 PM
awk question jonlake Linux - Software 2 05-04-2005 03:58 PM
Simple bash/awk/sed scripting question R00ts Programming 4 04-16-2005 02:55 AM
awk - simple 'for' loop doing my nut... davee Programming 2 06-30-2004 08:54 AM
awk question dilberim82 Programming 2 10-01-2003 07:12 PM

All times are GMT -5. The time now is 03:14 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