sed remove last character of file but not every last character

Hi Experts,
I am having trouble stripping only the last character and second last, of a variable

before sed:
107005075456+
1019918397440+
182400823296+
53550772224+
1810193428480+
+

expected result:
107005075456+
1019918397440+
182400823296+
53550772224+
1810193428480

we tried sed -e 's/\+$//' but its stripping all + but I want to get rid of only the last + and the one on the second last line

Could you please help!
DevSupportAsked:
Who is Participating?
 
Hanno P.S.Connect With a Mentor IT Consultant and Infrastructure ArchitectCommented:
maybe, we want to try this:

#!/bin/sh

file={name of file you want to modify}
lines=`wc -l $file`

cat $file | sed -e "$`expr $lines -1` s/\+$//" -e "$lines s/\+$//"

Open in new window

1
 
Abhimanyu SuriConnect With a Mentor Sr Database EngineerCommented:
>> cat abc.txt
107005075456+
1019918397440+
182400823296+
53550772224+
1810193428480+
xx+


>> sed '$ s/.$//' abc.txt
107005075456+
1019918397440+
182400823296+
53550772224+
1810193428480+
xx

Open in new window


Replace $ with line number

Thanks,
Suri
0
 
arnoldConnect With a Mentor Commented:
echo `echo $variable_where_the_data` | sed -e 's/\+$//'
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
arnoldCommented:
Alternatively add a number at the end, 0
0
 
simon3270Connect With a Mentor Commented:
A little heavy-handed, and assumes that there are no "|" characters in your input, but you could use:
cat $file | tr '\n' '|' | sed 's/+|+//' | tr '|' '\n'

Open in new window

Converts all newlines into the "|" character, removes "+|+" (so the + at the end of a line, the newline replacement character, and the + at the start of the next line) then converts the "|" back to newlines.

Edit: You say you have a variable with the data in, so the command becomes:
var=$(echo "$var" | tr '\n' '|' | sed 's/+|+//' | tr '|' '\n')

Open in new window

1
 
DevSupportAuthor Commented:
Thank You so much for helping!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.