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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Abhimanyu SuriDatabase 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
arnoldCommented:
echo `echo $variable_where_the_data` | sed -e 's/\+$//'
0
arnoldCommented:
Alternatively add a number at the end, 0
0
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Hanno P.S.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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
simon3270Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.