Remove Last Character of Line | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Remove Last Character of Line

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 05-19-2003
danhodges99
Guest
 
Posts: n/a
Remove Last Character of Line

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.
Sponsored Links
    #2  
Old 05-19-2003
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon (Administrator Emeritus)
 
Join Date: Aug 2001
Last Activity: 1 October 2014, 10:58 AM EDT
Location: Ashburn, Virginia
Posts: 9,930
Thanks: 64
Thanked 436 Times in 261 Posts
If you're using ksh, you can strip off the last character like this:
x=${x%?}
The Following User Says Thank You to Perderabo For This Useful Post:
lookoo (03-18-2013)
Sponsored Links
    #3  
Old 05-19-2003
oombera's Avatar
oombera oombera is offline Forum Advisor  
Registered User
 
Join Date: Aug 2002
Last Activity: 3 October 2004, 6:07 PM EDT
Location: Cleveland, OH
Posts: 804
Thanks: 0
Thanked 5 Times in 4 Posts
This is a continuation of this post.

You can use this if you use ksh:

someVar=`awk '{ printf $2 ";" }' file`; someVar=${someVar%;}

-----
Oops, oh well same idea
The Following User Says Thank You to oombera For This Useful Post:
lookoo (03-18-2013)
    #4  
Old 05-21-2003
Bashar Bashar is offline
Registered User
 
Join Date: Aug 2001
Last Activity: 11 November 2013, 7:16 AM EST
Location: Kuwait
Posts: 74
Thanks: 0
Thanked 1 Time in 1 Post
with vi its possible

:%s/.$//g

this will delete every last char on each line.
The Following User Says Thank You to Bashar For This Useful Post:
lookoo (03-18-2013)
Sponsored Links
    #5  
Old 05-21-2003
Optimus_P Optimus_P is offline Forum Advisor  
flim flam flamma jamma
 
Join Date: May 2001
Last Activity: 24 March 2008, 9:43 PM EDT
Location: Chicago IL, USA
Posts: 1,006
Thanks: 0
Thanked 2 Times in 1 Post
or how about sed. x=`echo $i|sed s/.$//`
The Following 2 Users Say Thank You to Optimus_P For This Useful Post:
krmarshall87 (06-07-2013), lookoo (03-18-2013)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to remove new line character at end of file. varun940 Shell Programming and Scripting 20 04-13-2012 02:21 AM
remove line feeds followed by character fluffdasheep Shell Programming and Scripting 5 07-07-2011 11:06 AM
How to remove new line character and append new line character in a file? sasikari HP-UX 8 10-27-2010 06:09 PM
To remove new line character shihabvk UNIX for Advanced & Expert Users 7 06-18-2009 07:44 AM
sed: remove first character from particular line hkansal Shell Programming and Scripting 6 06-11-2009 02:47 AM



All times are GMT -4. The time now is 06:27 AM.