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

Simple AWK command 1

Status
Not open for further replies.

kamasoni

Systems Engineer
Sep 30, 2019
1
DE
Hello,

I've got following command - ls | awk 'FS= "." { print $2$1 }'
Result:
K900048.SMA
SMAK900051
SMAK900053
SMAK900055

why first line is not sorted to SMAK900048?

Best regards,
Damian
 
Hi

Because you used [tt][navy]FS[/navy][teal]=[/teal][green]"."[/green][/tt] as pattern. Reading and parsing the input line is performed before the regular pattern-action pairs are executed, so before your code sets the field separator.

Either set the field separator in the special [tt]BEGIN[/tt] rule :
Code:
ls | awk '[b]BEGIN[/b][teal]{[/teal][navy]FS[/navy][teal]=[/teal][i][green]"."[/green][/i][teal]}{[/teal] [b]print[/b] [navy]$2$1[/navy] [teal]}[/teal]'
or use the interpreter's [tt]-F[/tt] option to set the field separator :
Code:
ls | awk -F "." '[teal]{[/teal] [b]print[/b] [navy]$2$1[/navy] [teal]}[/teal]'


Feherke.
feherke.github.io
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top