Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mostly formatted data? 1

Status
Not open for further replies.

huffgluons

Technical User
Aug 16, 2012
4
0
0
US
Hi everyone. I'm trying to write an awk script to read a "mostly" formatted file. A chunk of the input file is shown below:

Ca A 4861A -0.113 0.6733 He 2 1025A -2.425 0.0033 S II 1.037m -2.475 0.0029 Ar 3 517.5A -2.592 0.0022
Ca A 4340A -0.438 0.3190 He 2 992.4A -2.635 0.0020 S II 1.034m -2.178 0.0058 Ar 3 387.3A -2.867 0.0012
Ca A 4102A -0.692 0.1779 He 2 972.2A -2.810 0.0014 S 2 1256A -2.824 0.0013 Ar 3 517.4A -2.937 0.0010
Ca A 3970A -0.900 0.1100 He 2 4686A -2.088 0.0071 S 3 18.67m -0.371 0.3721 Ar 4 444.7A -2.124 0.0066
Ca A 3889A -1.078 0.0730 He 2 3203A -2.501 0.0028 S 3 33.47m -1.122 0.0661 Ar 4 392.0A -2.777 0.0015
Ca A 3835A -1.234 0.0510 He 2 2733A -2.791 0.0014 S 3 1720A -2.430 0.0033 Ar 4 859.3A -2.567 0.0024

I want a script that, given a name like "He 2 4686A" (second column, fourth row), will give the numbers -2.088 and 0.0071. The problem is I never know in which column this will appear. So I don't know what address the two numbers have.
I have no previous awk experience, but I think now is a great time to learn it.
Thanks.
 
Because the output format is
"emission line name" "wavelength" "flux" and "line ratio"
He 2 4686A flux lineratio

what happens is, depending on my model, I have a different number of lines, so a particular line appears in a different place every time. But I always want the two numbers following the line name and the wavelength. So, the two fields after "He 2 4686A".
 
Here is a link to what the output looks like. It was made to be readable by humans, not machines =/
 
Hi

Should I understand that you need the values from the two columns immediately following those containing the found values ?

A not so elegant solution, supposing the column separators are spaces :
Code:
awk -vsearch='He 2 4686A' '[teal]{[/teal][b]for[/b][teal]([/teal]i[teal]=[/teal][purple]1[/purple][teal];[/teal]i[teal]<[/teal][blue]NF[/blue][teal];[/teal]i[teal]++)[/teal][b]if[/b][teal]([/teal][navy]$i[/navy][green][i]" "[/i][/green][navy]$(i+1)[/navy][green][i]" "[/i][/green][navy]$(i+2)[/navy][teal]==[/teal]search[teal])[/teal][COLOR=chocolate]print[/color][navy]$(i+3)[/navy][teal],[/teal][navy]$(i+4)[/navy][teal]}[/teal]' /input/file
Tested with [tt]gawk[/tt] and [tt]mawk[/tt].

Feherke.
[link feherke.github.com/][/url]
 
Ah, I see what you did. Thanks, that's pretty straightforward.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top