Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Removing the Last Character in a String 2

Status
Not open for further replies.

danhodges99

IS-IT--Management
Feb 6, 2003
21
GB
Hi,

I need to put the single line contents of a file into a variable, but remove the last character, for example the file would have this sort of contents:

2;4;3;10;67;54;96;

And I want the variable to be:

2;4;3;10;67;54;96 (notice the last ";" has gone).

Unfortunately I can't just do a cut of characters 1-x as the string will fluctuate in length!

Thanks.
 
This is untested, but may get you started.

var = `awk '{sub(/.$/,"");print}' infile`

CaKiwi

"I love mankind, it's people I can't stand" - Linus Van Pelt
 
Your shell?

Bash can use this trick:
Code:
while read line
do
    echo "Line before: $line"
    line=${line%?$}
    echo "Line after: $line"
done 

or this:

while read line
do  
  echo "Line before: $line"
  cnt=`expr ${#line} - 1`  
  line=`echo $line | cut -c 1-$cnt`   
  echo "After: $line" 
done < filename
 
One liner with [tt]sed[/tt]...
[tt]
sed 's/;$//g' infile > outfile
[/tt]
Hope this helps.

 
I need to remove a single quote as last character on each line in a string, but when I use this command I am getting a syntax error:
sed 's/'$//g' infile > outfile
For example,
I need to change this
ADDUSER HFA3343'
to
ADDUSER HFA3343
Could you please assist.
Thanks.
 
sed "s!'\$!!" infile > outfile

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top