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

using Begin

Status
Not open for further replies.

smirsistani

Programmer
Jul 27, 2015
3
0
0
IR
hi
awk '{FS=",";print $2}'
one,two,three

four,five,six
five

why FS="," in line one do not work?
 
if i using BEGIN
awk 'BEGIN{FS=","}{print $2}'
one,two,three
one
four,five,six
five
 
Hi

Because Awk first reads the next record, splits it into fields then execute the code. And you only set the [tt]FS[/tt] in the code. So after the splitting was performed.

You can force Awk to repeat the splitting by touching the record :
Code:
[teal]{[/teal][navy]FS[/navy][teal]=[/teal][i][green]","[/green][/i][teal];[/teal][highlight][navy]$0[/navy][teal]=[/teal][navy]$0[/navy][teal];[/teal][/highlight][b]print[/b] [navy]$2[/navy][teal]}[/teal]

Feherke.
feherke.ga
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top