LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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-30-2012, 10:29 AM   #1
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,700

Rep: Reputation: 564Reputation: 564Reputation: 564Reputation: 564Reputation: 564Reputation: 564
awk newbie - simple loop


As a learning exercise I want to write a simple loop.

For each line in a file ...
- if the first character is a tilde (~) increment a counter
- if the first character is not a tilde print the counter value and the line

Sample input file:
Quote:
~ garbage garbage
Henry
Ed
Jason
Ron
~ more garbage
Judith
Paul
Desired output file:
Quote:
1Henry
1Ed
1Jason
1Ron
2Judith
2Paul
This is my newbie code which has syntax errors.
Code:
cat < $Work2      \
|awk '{
sum = 0
for (i=1; i<=NL; i++)
{ if substr($i,1,1) = '~' then sum ++
                          else printf(sum,$i) }
} \
> $Work5
Please offer corrections or a complete revision.
Thank you.

Daniel B. Martin
 
Old 01-30-2012, 10:36 AM   #2
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 243Reputation: 243Reputation: 243
You don't need a loop
Code:
awk '/^~/ {i++;next} {print i$0}' file

Last edited by Cedrik; 01-30-2012 at 10:37 AM.
 
1 members found this post helpful.
Old 01-30-2012, 11:09 AM   #3
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,700

Original Poster
Rep: Reputation: 564Reputation: 564Reputation: 564Reputation: 564Reputation: 564Reputation: 564
Quote:
Originally Posted by Cedrik View Post
Code:
awk '/^~/ {i++;next} {print i$0}' file
Short and sweet and loopless. I love it! Thank you, and this one is SOLVED!
 
  


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
How to use awk in a for loop? cliffyao Programming 2 10-27-2010 11:51 PM
[SOLVED] awk for-loop babaqga Linux - Software 4 09-02-2010 04:57 AM
Simple while loop problem (newbie question) Seventh Programming 3 09-07-2004 01:00 PM
awk - simple 'for' loop doing my nut... davee Programming 2 06-30-2004 09:54 AM

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

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