I have a psv file having records something like this:
Apple | Banana | I want to eat banana | Carrot
As you can see the all the columns contain leading and trailing spaces I used this command:
sed -i -r 's/\s //g;/^$/d' fileName
to remove the blank spaces as well as blank lines. After which i got the result as:
Apple|Banana|Iwanttoeatbanana|Carrot
What my expected Result is somewhat like this:
Apple|Banana|I want to eat banana|Carrot
Please help me getting this desired result using shell script.
Thank you in advance!
CodePudding user response:
You may use this sed:
sed -E 's/^[[:blank:]] |[[:blank:]]*(\|)[[:blank:]]*|[[:blank:]] $/\1/g' file
Apple|Banana|I want to eat banana|Carrot
Explanation:
^[[:blank:]]: Match 1 or more whitespaces after start|: OR[[:blank:]]*(\|)[[:blank:]]*: Match 0 or more whitespaces before and after a|and capture|in group #1|: OR[[:blank:]] $: Match 1 or more whitespaces before endReplacement is a\1` that puts captured value of group #1 in replacement
An awk solution:
awk -F '[[:blank:]]*\\|[[:blank:]]*' -v OFS='|' '{
gsub(/^[[:blank:]] |[[:blank:]] $/, ""); $1=$1} 1' file
Apple|Banana|I want to eat banana|Carrot
