Removing pipe and space in a file

Hi ,

I have a file which is like below. The file record is separated by | as delimiter. I want to remove the first and last | in the file and also I want to remove the spaces in the columns.

Original File
|3464114      |10     | REPORT|CA| 1|0|4825400|0|213167|  939396524|

Expected:
3464114|10|REPORT|CA|1|0|4825400|0|213167|939396524

I tried below sed and getting the | delimited removed from first and last . But I need to remove space too. For some reason I am not getting expected out on big file with the below command:-

#sed -n '/^| */{s///;s/ *| */|/g;p;}'  file.out > format.out
sed 's/^ *[|] *//; s/ *[|] */|/g; s/ *[|] *$//' file.out > format.out
1 Like

This works with my GNU sed 4.4:

sed -r 's/(^\|| |\|$)//g' file
3464114|10|REPORT|CA|1|0|4825400|0|213167|939396524
1 Like

Put the global substitution first:

sed 's/ *| */|/g; s/^|//; s/|$//'
1 Like