Skip to main content
added 3 characters in body
Source Link
glenn jackman
  • 248.7k
  • 42
  • 233
  • 362

Using awk with custom field separator:

First non-empty field:

awk -F ',+' '{print $2}' file VALUE1 VALUE3 VALUE6 VALUE8 

Last non-empty field:

awk -F ',+' '{print $(NF-1)}' file VALUE2 VALUE5 VALUE7 VALUE8 

Regex pattern ',+' will make 1 or more commas as a field separator.

Using awk with custom field separator:

First non-empty field:

awk -F ',+' '{print $2}' file VALUE1 VALUE3 VALUE6 VALUE8 

Last non-empty field:

awk -F ',+' '{print $(NF-1)}' file VALUE2 VALUE5 VALUE7 VALUE8 

Regex pattern ',+' will make 1 more commas as a field separator.

Using awk with custom field separator:

First non-empty field:

awk -F ',+' '{print $2}' file VALUE1 VALUE3 VALUE6 VALUE8 

Last non-empty field:

awk -F ',+' '{print $(NF-1)}' file VALUE2 VALUE5 VALUE7 VALUE8 

Regex pattern ',+' will make 1 or more commas as a field separator.

Source Link
anubhava
  • 790.3k
  • 67
  • 603
  • 671

Using awk with custom field separator:

First non-empty field:

awk -F ',+' '{print $2}' file VALUE1 VALUE3 VALUE6 VALUE8 

Last non-empty field:

awk -F ',+' '{print $(NF-1)}' file VALUE2 VALUE5 VALUE7 VALUE8 

Regex pattern ',+' will make 1 more commas as a field separator.