chan go
chan go

Reputation: 137

Regex to replace a character if is unique

I need please help with a script with a regex to fix a big text file under linux (with sed for example). My records looks like:

1373350|Doe, John|John|Doe|||B|Acme corp|...
1323350|Simpson, Homer|Homer|Simpson|||3|Moe corp|...

I need to validate if the 7th column has a unique character (maybe a letter or number) and if true, add the second column without the comma, i mean:

1373350|Doe, John|John|Doe|||B Doe John|Acme corp|...
1323350|Simpson, Homer|Homer|Simpson|||3 Simpson Homer|Moe corp|...

Any help? Thanks!

Upvotes: 0

Views: 55

Answers (1)

Wintermute
Wintermute

Reputation: 44043

Awk is better suited for this job:

awk -F '|' 'BEGIN { OFS = FS } length($7) == 1 { x = $2; sub(/,/, "", x); $7 = $7 " " x } 1' filename

That is:

BEGIN { OFS = FS }   # output separated the same way as the input
length($7) == 1 {    # if the 7th field is one character long
  x = $2             # make a copy of the second field
  sub(/,/, "", x)    # remove comma from it
  $7 = $7 " " x      # append it to seventh field
}
1                    # print line

Upvotes: 1

Related Questions