LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-05-2011, 08:20 AM   #1
tia_chofi
LQ Newbie
 
Registered: Dec 2011
Posts: 2

Rep: Reputation: Disabled
How to read an external file and print it with a definite format within AWK script


Hallo!
I'm brand new in programming and wanted to ask a question about the following. I have a file which looks like this:

*first_set
xx xx xx xx xx xx
xx xx xx xx xx xx
*second_set
yy yy yy yy yy yy
yy yy yy yy yy yy
yy yy yy yy yy yy
...
...
...
*last_set
zz zz zz zz zz zz
zz zz zz zz zz zz
zz zz zz zz zz zz

I want to separate each of this blocks. Then, printout the title of each of them , and sort the rest of the block and print it in the same format as the original file.
I have done the most with a simple code in AWK, writing the contains of the blocks in temporary files. The problem I have is precisely to read back this temp.files and print them with the same format as before.
I know that it can be done with getfile, but it simply does not work.
My code so far which does not work:
while ((getline <"sorted_"match".tempo" ) > 0)
{nr++
line[nr] = $0
}close("sorted_"match".tempo");
printf("10%s", line[nr])
}\


"match" is a counter which labels each of the temp.files with the block encounter in the file.
I appreciate very much some suggestions.
 
Old 12-05-2011, 10:09 AM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: CentOS 6 (pre-systemd)
Posts: 2,667

Rep: Reputation: 710Reputation: 710Reputation: 710Reputation: 710Reputation: 710Reputation: 710Reputation: 710
awk is an interesting choice for learning programming. The more I think about it, the more I like it.

Code:
nr = 0
fname = "sorted_"match".tempo"
while ((getline < fname ) > 0)
{
  nr++
  line[nr] = $0
  printf("10%s", line[nr])
}
close(fname);
Use code tags as above to make your posts more readable.
I added a line to set nr to 0 before the loop. Otherwise it will keep incrementing from the previous file.
Rather than compute the filename each time, lets use a variable and do it once.
If you don't print the line inside the loop, then it will only print the last line.
If you aren't using the array "line" later, then you can just print $0 and get rid of line.
 
Old 12-13-2011, 05:26 AM   #3
tia_chofi
LQ Newbie
 
Registered: Dec 2011
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thanks! it was very useful. Although at the ende I decided for a more elegant solution just using pipes instead that making temporary files.
 
  


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 print an apostrophe (') in a shell script using awk? skuz_ball Programming 11 03-10-2012 09:26 AM
[SOLVED] Awk script to look for string, show value in field 2, if not present print zero Perseus Programming 12 10-06-2011 04:40 AM
[SOLVED] Using AWK to print out the first few lines of a txt file mskalak Linux - Newbie 3 07-27-2011 03:58 PM
Awk+print, create file if not exixt takayama Programming 1 05-15-2011 02:31 PM
[SOLVED] awk print to file question takayama Programming 3 02-05-2011 05:21 PM


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