LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 03-30-2010, 06:26 PM   #1
jssidhu4
LQ Newbie
 
Registered: Mar 2010
Posts: 4

Rep: Reputation: 0
Unhappy for loop in awk command


how do I use the for loop in awk command to read the file records and then use the for loop to display (hence number) each line like

1. sometext here
2. sometext here


please help
 
Old 03-30-2010, 07:35 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
I don't quite follow ...
If you just want a record number for each line just print it like so (no loops at all):
Code:
awk '{print NR". "$0}' file


Cheers,
Tink
 
Old 03-30-2010, 07:40 PM   #3
jssidhu4
LQ Newbie
 
Registered: Mar 2010
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks,

I know this but it is a requirement to have a for loop doing this so, if you can help me by showing with a for loop your help would be very great
once again thank you so much
 
Old 03-30-2010, 07:43 PM   #4
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
http://www.ibm.com/developerworks/library/l-awk2.html

Exactly halfway down the page I linked above (which I found by Googling for "awk for loop") is a nice little example.

Cheers!

Sasha
 
Old 03-30-2010, 07:45 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Let me see what google has to say:

http://www.lmgtfy.com/?q=awk+for+loop
 
Old 03-30-2010, 08:59 PM   #6
jssidhu4
LQ Newbie
 
Registered: Mar 2010
Posts: 4

Original Poster
Rep: Reputation: 0
okay but how do I put number in front of each record?
 
Old 03-30-2010, 09:34 PM   #7
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Have you actually LOOKED at the link Sasha suggested?
 
Old 03-31-2010, 04:19 AM   #8
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
LQ has a very agile search engine built in. I got really good results, and a lot of examples of using awk, like this:

I went to the LQ Search Page;
Typed "awk for loop" into the 'search by keyword' text field;
I selected 'search entire posts' in the drop-down thingy just below that;
I selected the 'Programming' forum as the forum to search...

223 items returned!

Last edited by GrapefruiTgirl; 03-31-2010 at 04:21 AM.
 
  


Reply



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
cat command space problem with awk in for loop. shan_nathan Programming 3 06-18-2009 05:51 AM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 04:11 PM
awk loop related kalyanofb Programming 3 02-16-2007 02:31 AM
awk in loop How to Nkunzis Linux - Newbie 3 12-10-2006 01:34 PM
awk - simple 'for' loop doing my nut... davee Programming 2 06-30-2004 08:54 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

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
Open Source Consulting | Domain Registration