LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 02-23-2007, 03:27 PM   #1
mr_scary
Member
 
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:

6001 172.17.1.146
6002 172.17.1.113
6003 172.17.1.135

How can I add a column so I get this:

1 6001 172.17.1.146
2 6002 172.17.1.113
3 6003 172.17.1.135

Thanks for your interest in this important matter.

PM
 
Old 02-23-2007, 04:29 PM   #2
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora, Lubuntu, FreeBSD
Posts: 3,930
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
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,151

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
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
ygloo
Member
 
Registered: Aug 2006
Distribution: slack
Posts: 323

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


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 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 08:20 AM.

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