For loop inside awk to read and print contents of files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting For loop inside awk to read and print contents of files
# 1  
Old 06-06-2013
For loop inside awk to read and print contents of files

Hello,
I have a set of files Xfile0001 - Xfile0021, and the content of this files (one at a time) needs to be printed between some line (lines start with word "Generated") that I am extracting from another file called file7.txt and all the output goes into output.txt. First I tried creating a for loop over the Xfiles, and inside the loop the awk code, but didn't work, I understand I have to use the for loop inside the awk command, but after trying many times, I am not getting the output I am looking for.
So far this is one of the several versions I have:
Code:
awk '/Generated/ {print;getline; for (i in Xfile????); print "$i"; next}' file7.txt > output.txt

So far this gives me an error, now I realized the question mark is not properly understood as 'interchangeable' character within awk, and also the print "$i" prints literally $i and not the content of the corresponding Xfile. I have recently started using more unix commands to try to do more efficiently my work, and its been very useful (whenever I get through this kind of things). I will really appreciate if someone can help me here.
By the way, the output I expect to have on output.txt should look like this:
Code:
<first line saying "Generated ....." from file7.txt>
<content of Xfile0001>
<second line saying "Generated ..." from file7.txt>
<content of Xfile0002>
...
...
...

Thank you very much,
# 2  
Old 06-06-2013
$ does not mean "variable" in awk -- it means column. If you did V=5; print $V you'd be printing the 5th column. Leave off the $, and you'd just be printing the value 5.

awk cannot do globbing like in shell, but it can read from arbitrary files. Perhaps this?

Code:
awk '/Generated/ {print;getline;  file++;
        while((getline <sprintf("Xfile%04d",file))>=0) print;
        close(sprintf("Xfile%04d", file)); }' inputfile

# 3  
Old 06-06-2013
Thanks for your reply, and for the hint on the use of $. I used the code you suggested, but what is doing is to get the first line that says "Generated ..." from inputfile, then pastes the content of Xfile0001, and then it prints the last line of Xfile0001 'ad infinitum' until I disk quota is exceeded, and doesn't print the next line from inputfile saying "Generated..." followed by Xfile0002, and so on. Thanks!
I tried adding a next statement, but doestn't work. Thanks for your advice.
# 4  
Old 06-06-2013
Quote:
Originally Posted by jaldo0805
Thanks for your reply, and for the hint on the use of $. I used the code you suggested, but what is doing is to get the first line that says "Generated ..." from inputfile, then pastes the content of Xfile0001, and then it prints the last line of Xfile0001 'ad infinitum' until I disk quota is exceeded, and doesn't print the next line from inputfile saying "Generated..." followed by Xfile0002, and so on. Thanks!
I tried adding a next statement, but doestn't work. Thanks for your advice.
Change below statement to be > 0 as opposed to >= 0...
Code:
while ((getline < sprintf("Xfile%04d", file)) > 0) print;

Because when end-of-file is reached the >= 0 test will always be true creating an ad nauseum loop until "output.txt" fills up your disk partition.
These 2 Users Gave Thanks to shamrock For This Post:
# 5  
Old 06-06-2013
Thank you so much, that does the trick Smilie Smilie
# 6  
Old 06-06-2013
Sorry about that, awk's getline always confuses me. It uses neither the C nor C++ convention for return value on these things, but some weird hybrid of both, causing the usual while(getline) most programmers expect to become an infinite loop as well.

Last edited by Corona688; 06-06-2013 at 05:37 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Unable to read user input inside a loop

Hi, This query is a part of a much more lengthy script. I wish to look for all the files in a folder named "data" which in this case has two files i.e. plan.war and agent.properties. For all the files found under data I wish to ask the user as to where they wish copy the files to. Below,... (14 Replies)
Discussion started by: mohtashims
14 Replies

2. Shell Programming and Scripting

Use while loop to read file and use ${file} for both filename input into awk and as string to print

I have files named with different prefixes. From each I want to extract the first line containing a specific string, and then print that line along with the prefix. I've tried to do this with a while loop, but instead of printing the prefix I print the first line of the file twice. Files:... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

3. Shell Programming and Scripting

Update file record inside read loop

Hi, I am reading file records inside a while loop, and want to update the record when certain condition is met. How can I update a file while being read? I want to avoid using temporary files, copy, rename, ... while IFS=',' read -r f1 f2 do function(f1,f2) if then <add... (1 Reply)
Discussion started by: ysrini
1 Replies

4. Shell Programming and Scripting

script to read the contents of a file and print

Hi, Need help in writing a script to read the contents of this file test Test 00a 00b 00c 00d 00e 00f where it need to read each line to give a display such as form meta from dev 00a , config=Striped; add dev 00b:00f to meta 00a Can any one help me in writing this script (2 Replies)
Discussion started by: praveen1516
2 Replies

5. Shell Programming and Scripting

Read contents of the file and print

AT ---------- 0 Elapsed: 00:00:00.02 SO ---------- 0 Elapsed: 00:00:00.01 SE ---------- 0 Elapsed: 00:00:00.01 CR ---------- (4 Replies)
Discussion started by: sandy1028
4 Replies

6. Shell Programming and Scripting

Rename contents inside multiple files

Hi, I have 100 files in a directory. Each file have the following format >CtbRe01234 fdfjdhfkdfkd >CtL2B0456 gjfgfkgjfkgjfk >CmdrE05768 fghdjskksllfkLike this I have many files in the directory. What I want is; rename the header content in each file such that the above file... (6 Replies)
Discussion started by: Lucky Ali
6 Replies

7. Shell Programming and Scripting

Print filename inside loop

Im want to print filename inside loop .. the code im using :- Filename_1=abc_20090623_2.csv.lk Filename_2=def_20090623_2.csv.lk i want to extract filename till .csv eg Filename_1=abc_20090623_2 Filename_2=def_20090623_2 How can i do this inside the for loop ... (3 Replies)
Discussion started by: r_t_1601
3 Replies

8. Shell Programming and Scripting

read command (input) inside the while loop

Hi, 'read' command is not working inside the while loop, How can I solve this? Rgds, Sharif. (2 Replies)
Discussion started by: sharif
2 Replies

9. Shell Programming and Scripting

input inside while read loop

Hi all Does anyone have a script that will allow me to stop inside a while read loop. I want to pause the loop until a enter is pressed. e.g. While read line do echo something if LINECOUNT > 40 then read ENTER?"PRESS ENTER TO CONT..." ... (3 Replies)
Discussion started by: jhansrod
3 Replies

10. UNIX for Dummies Questions & Answers

read inside a while loop

Hi all, In a while loop, like below... while read line do read choice case $choice in 1) echo "xxx" esac done < file why I can't run the read choice???? (3 Replies)
Discussion started by: dta4316
3 Replies
Login or Register to Ask a Question