bashcsvawksed

Remove last character from lines if it matches with bash/awk


I have a load of csv files, but they are not clean.

They use "|" as a seperator. Some of the files have a trailing "|" after line one, meaning there is N columns in line 1, and N+1 in line 2+.

I want to cycle through the files and remove the trailing separator on lines that have it.

What is the best way to do this for a single file using the shell with awk or a similar tool?

Input csv:

|some|data|here
|some|more|data|
|even|more|data|

Output:

|some|data|here
|some|more|data
|even|more|data

Solution

  • With sed inplace replacement:

    sed -i 's/|$//' test.txt