Append pipe | at the end of all the rows except header n trailer for all the files under a directory

Hi Experts

Need help...
I am looking for a Unix script to append pipe | at the end of all the rows (except header and trailer)in all the files placed under the directory /interfaces/Temp

e.g.

Header
row1
row2
row3
Trailer

The script should read all the files under /interfaces/Temp and append | at the end of rows (except header and trailer).
Post execution of Script, all files under the folder should look like this:

Header
row1|
row2|
row3|
Trailer

Thanks
Phani

Any attempts / ideas / thoughts from your side?

I tried below command. But it works for only one file. I am trying to read all the files under a specified folder.

sed 's/$/|/' file.txt > new-file.txt

Try with a shell loop:

for FN in *; do sed -n '1 {p;b;}; ${p;b;}; s/$/|/p;' $FN > new_$FN; done

EDIT: or,

for FN in *; do sed  '1bL; $bL; s/$/|/; :L' $FN; done
Moderator comments were removed during original forum migration.

One of the good things about the occasional auto-bumped post is that I get to remember some of the excellent solutions by @RudiC.

I am reading them more carefully these days :slight_smile:

3 Likes

I wonder why the label is here?
Maybe it doesn’t work like this everywhere:

sed '1b;$b;s/$/|/' file
1 Like

It does. But a Unix sed wants a label to end with a new line or the end of document.

sed -e '1b' -e '$b' -e 's/$/|/'

Also it might want { and } on separate lines. The following avoids all multi-line requirement

sed '1p;1d; $p;$d; s/$/|/'
2 Likes

Or:

sed '1n; $n; s/$/|/'

or

sed '1n; $!s/$/|/'
2 Likes