How to replace the first and last character which is pipe symbol in all files within a folder?

with in my files i have the data like this, starting with a pipe and ending the line with a pipe.

all i want is to replace the first and last pipe , remove those
trying to use following sed command, but it is only showing on the screen the entire data of the file as if it removed, but when i open the file i again see the pipes at the beginning of each line and ending.

sed 's/^.\(.*\).$/\1/' /usr/local/bin/stage_process/tgfiles/20180301_svy_analysis.csv
|1273098536|A2|5|
|1273098536|A28|5|
|1273098536|A3|5|
|1273098536|A4|5|
|1273098536|A5|5|
|1273098536|B1|5|

that way they appear like this

1273098536|A2|5
1273098536|A28|5
1273098536|A3|5
1273098536|A4|5
1273098536|A5|5

Thank you very much for the helpful info, i am planning on using the commands within the shell files.

By default sed prints to stdout (that you can redirect to a new file).
Some sed versions have a -i option to replace the input file by the output.

sed -i sedcode file_to_be_modified
sed -i 's/^.\(.*\).$/\1/' usr/local/bin/stage_process/tgfiles/20180301_svy_analysis.csv

As MadeInGermany says, some versions of sed have the -i option. If your version of sed does not, you will have to write something like:

file=/usr/local/bin/stage_process/tgfiles/20180301_svy_analysis.csv 
mv ${file} ${file}-bak
sed 's/^.\(.*\).$/\1/' ${file}-bak > ${file}

Don't try to write directly to the file without using an intermediate; the shell will open it for writing before it is opened for reading and the file contents will be lost.

Please include your Operating System and shell version in future queries; the solution can be tailored to your system.

Andrew

1 Like

Hello cplusplus1,

Could you please try following and let me know if this helps you.

awk '{gsub(/^\||\|$/,"")} 1' Input_file > temp_file  &&  mv  temp_file  Input_file

Thanks,
R. Singh