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!

Arrays and Fieldwidths 2

Status
Not open for further replies.

pdalke

Technical User
Aug 14, 2007
2
DE
Hi there,
following problem makes me some headaches:
I have a fixed field length file (SAP IDoc) and needs to extract data. For that I need to define the FIELDWIDTHS variable dynamically. As I had to learn you cannot define the FIELDWIDTHS variable within the actual record
awk ' /E1EDK02/ {FIELDWIDTH="10 5 ..."; print ...}' don't work.

Next idea I had was to define an array to extract the FNR and the belonging record. Then I can refer to array[FNR+1] and set the FIELDWIDTHS variable within the previous record.
Unfortunately, this doesn't work too, because within the array is filled up to the actual record only.

Does anyone has any idea. I'm open to everything. Ideas can be textual.

Many thanks,
Paul.
 
Hi

Paul said:
awk ' /E1EDK02/ {FIELDWIDTH="10 5 ..."; print ...}' don't work.
Well, it works, but may not do what you are expecting...

Try this :
Code:
awk ' /E1EDK02/ {FIELDWIDTH[red]S[/red]="10 5 ..."; [red]$0=$0;[/red] print ...}'

Feherke.
 
Hi feherke,

Thanks very much. It works perfectly. If you still have time, would you pls explain what "$0=$0" (entire line=entire line) does?!

Again thanks, you save me a lot of time.

Paul.
 
Hi

Well, that [tt]$0=$0[/tt] indeed looks meaningless.

But in [tt]awk[/tt] triggers the reevaluation of the current record.

When [tt]awk[/tt] reads a record it, it parses it based on the [tt]FS[/tt] or [tt]FIELDWIDTHS[/tt] variable's value. That happens before the script is evaluated. So your setting of [tt]FIELDWIDTHS[/tt] happened after the record was parsed into fields and had no effect. But when you set a velue tu the record, it is parsed again. That is [tt]$0=$0[/tt] good for.

In the below example you can see that [tt]awk[/tt] parses again the record when [tt]$0=$0[/tt] is executed :
Code:
[blue]master #[/blue] echo hello world | awk '{print NF " : " $0}'
2 : hello world

[blue]master #[/blue] echo hello world | awk '{$2="cruel " $2; print NF " : " $0}'
2 : hello cruel world

[blue]master #[/blue] echo hello world | awk '{$2="cruel " $2; [red]$0=$0;[/red] print NF " : " $0}'
3 : hello cruel world

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top