antimuon
antimuon

Reputation: 262

awk not parsing the first line of file

I am piping awk on to a set of file paths that I want to parse into comma separated output but for some reason the first line is getting ignored. I can't see what I am doing wrong.

File path example is in test file...

/home/user/folder1
/home/user/folder2
/home/user/folder3
/home/user/folder4
/home/user/folder5

awk command is...

awk ' BEGIN { FS="/" } { print $1,$2,$3,$4 } { OFS="," } ' test

But my output is this...

 home user folder1
,home,user,folder2
,home,user,folder3
,home,user,folder4
,home,user,folder5

It seems to ignore the first line...I have even tried if(NR>0) print but it didn't work either.

Upvotes: 0

Views: 504

Answers (1)

antimuon
antimuon

Reputation: 262

I see what I did wrong. The OFS was being applied after the statement was executed.

Changed to...

awk ' BEGIN { FS="/"; OFS="," } { print $1,$2,$3,$4 } ' test


,home,user,folder1
,home,user,folder2
,home,user,folder3
,home,user,folder4
,home,user,folder5

Upvotes: 1

Related Questions