I have pipe delimited file that looks like this:
col1|col2|col3|col4
a |b | some value | hello world
a |b | some value | hello world
a |b | some value | hello world
a |b | some value | hello world
The values in each column, including the last, have trailing white spaces. How do I remove trailing white spaces using sed so that my output looks like this?
col1|col2|col3|col4
a|b|some value|hello world
a|b|some value|hello world
a|b|some value|hello world
a|b|some value|hello world
This is what I have tried which removes spaces between the delimiters, but not at the end of last column:
sed -i -e "s/\s*|/|/g" somefile.txt
CodePudding user response:
You may use this sed:
sed -E 's/[[:blank:]]*(\|)[[:blank:]]*|[[:blank:]] $/\1/g' file
col1|col2|col3|col4
a|b|some value|hello world
a|b|some value|hello world
a|b|some value|hello world
a|b|some value|hello world
Here:
[[:blank:]] (\|)[[:blank:]]*: Match whitespaces before and after||: OR[[:blank:]] $: Match whitespaces before end
Alternative solution in awk:
awk -F ' *\\| *' -v OFS='|' '{sub(/ $/, "", $NF)} 1' file
CodePudding user response:
You can use this sedcommand:
sed 's/[[:blank:]]*$//; s/[[:blank:]]*|[[:blank:]]*/|/g' file
If you want to remove leading blanks as well, then just add s/^[[:blank:]]*// to the sed expression above.
CodePudding user response:
1st solution: Here is one more awk variant, written and tested with your shown samples; written and tested in GNU awk.
awk -F'[[:space:]]*\\|[[:space:]]*' -v OFS="|" '
{
sub(/[[:space:]] $/,"")
$1=$1
}
1
' Input_file
2nd solution: Making field separator in awk and using match function of GNU awk please try following.
awk -F '[[:space:]]*\\|[[:space:]]*' -v OFS='|' '
($1=$1) && match($0,/[[:space:]] $/){
print substr($0,1,RSTART-1)
}
' Input_file
