LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 10-27-2010, 08:57 PM   #1
cliffyao
LQ Newbie
 
Registered: Oct 2009
Posts: 27

Rep: Reputation: 15
How to use awk in a for loop?


I just wrote a code as

Code:
for((i=1; i<=5; i++))
do
	awk '$1=="$i"' input.txt > output_$i\.txt
done
I want to take out the lines in the input.txt in which their first columns are 1 or 2 or 3 or 4 or 5, to get 5 output files as

output_1.txt
output_2.txt
output_3.txt
output_4.txt
output_5.txt

This code doesn't work unfortunately..

Anyone knows how to make it work?

Thanks in advance
 
Old 10-27-2010, 09:56 PM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Wheezy (Fluxbox WM)
Posts: 1,363
Blog Entries: 52

Rep: Reputation: 353Reputation: 353Reputation: 353Reputation: 353
You just have a minor quoting problem. Because you are using single quotes, the '$i' is not being expanded out by the shell. Modifying the awk line to this will help:
Code:
awk "\$1==$i" input.txt >output_$i.txt
(notice that the '$' in $1 is being escaped, so that the shell does not attempt to expand it)

Last edited by neonsignal; 10-27-2010 at 09:58 PM.
 
Old 10-27-2010, 10:51 PM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,442

Rep: Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880
Not exactly ... the issue is actually the $i means nothing inside awk. You either have to quote accordingly or use an awk variable.
I prefer the second as the quoting option gets very messy if you have to use it more than once:
Code:
awk -vtest=$i '$1 == test' input.txt > output_${i}.txt
 
  


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
[SOLVED] awk for-loop babaqga Linux - Software 4 09-02-2010 03:57 AM
for loop in awk command jssidhu4 Programming 7 03-31-2010 04:19 AM
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: loop through ldap aliases tonedeaf1969 Programming 6 11-17-2006 11:56 AM


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