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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loop through fields in tex file

Status
Not open for further replies.

netherxx

Programmer
Oct 30, 2009
1
NL
Hi All,

I need some help with looping through fields in a text file.
The text file is in the following format:

{field 1}{field 2}{field 3}{field 4}{field 5}{field 6}{field 7}{field 8}

I know that in awk there is the field separator options (FS), but i have tried it with "{" and "}{" but i cannot get it to work.

I need to search the text file to find a specific field and then do something with that field (compare the value).

can anybody help me with this?
 
Hi

netherxx said:
i have tried it with "{" and "}{" but i cannot get it to work.
Well, I can. Of course, the field values needs some cleanup after the splitting.

But I would set [tt]FS[/tt] to [tt][{}]+[/tt] and use it with another compromise : ignore the extra fields at both ends of the record. ( They will appear because the record starts and ends with separator. )
Code:
[blue]master #[/blue] echo '{field 1}{field 2}{field 3}{field 4}{field 5}{field 6}{field 7}{field 8}' | mawk -F '[{}]+' '{for(i=1;i<=NF;i++)print"field #"i" is "$i}'
field #1 is 
field #2 is field 1
field #3 is field 2
field #4 is field 3
field #5 is field 4
field #6 is field 5
field #7 is field 6
field #8 is field 7
field #9 is field 8
field #10 is
Tested with [tt]mawk[/tt] and [tt]gawk[/tt].

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top