UNIX for Dummies Questions & Answers

This forum is closed for new posts. Please post beginner questions to learn unix and learn linux in this forum UNIX for Beginners Questions & Answers

Remove a carriage return at end of variable

 

 
Thread Tools Search this Thread Display Modes
    #1  
Old 11-17-2004
flagship99 flagship99 is offline
Registered User
 
Join Date: Nov 2004
Last Activity: 17 November 2004, 5:39 PM EST
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Remove a carriage return at end of variable

Is there a command in unix to remove a carriage return character(^M) at the end of a variable value?
Sponsored Links
    #2  
Old 11-17-2004
PxT's Unix or Linux Image
PxT PxT is offline Forum Advisor  
Registered User
 
Join Date: Oct 2000
Last Activity: 7 May 2009, 12:22 PM EDT
Location: Sacramento, CA
Posts: 909
Thanks: 0
Thanked 4 Times in 4 Posts
Code:
$ FOO="bar
> "
$ echo "$FOO"
bar

$ BAR=$FOO
$ echo "$BAR"
bar

$ BAR=`echo $FOO`
$ echo "$BAR"
bar
$


In other words, "echo" will chomp it when used without quotes.
Sponsored Links
    #3  
Old 11-17-2004
flagship99 flagship99 is offline
Registered User
 
Join Date: Nov 2004
Last Activity: 17 November 2004, 5:39 PM EST
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
^M must not be carriage return

Before and after I tried to remove the ^M with the echo command, (per your instructions) it did not put an extra line between the output of these commands:
echo $variable
echo $variable
echo $variable
or
echo $bar
echo $bar
echo $bar

Sorry, I must have wrong, ^M must not be a carriage return, at least not to the echo command. After doing the
bar=`echo $variable`, the ^M is still there at the end of the variable bar.
    #4  
Old 11-17-2004
bhargav's Unix or Linux Image
bhargav bhargav is offline Forum Advisor  
Registered User
 
Join Date: Sep 2004
Last Activity: 22 January 2016, 4:39 PM EST
Location: USA
Posts: 512
Thanks: 0
Thanked 2 Times in 2 Posts
echo "helloworld^M" | sed 's/\^M//'
Sponsored Links
    #5  
Old 11-17-2004
bhargav's Unix or Linux Image
bhargav bhargav is offline Forum Advisor  
Registered User
 
Join Date: Sep 2004
Last Activity: 22 January 2016, 4:39 PM EST
Location: USA
Posts: 512
Thanks: 0
Thanked 2 Times in 2 Posts
If u just simply want to chop off the last character ....
do the following ....

echo "hello" | sed 's/.$//'


or u can file streams as input to sed .... ;
Sponsored Links
    #6  
Old 11-17-2004
flagship99 flagship99 is offline
Registered User
 
Join Date: Nov 2004
Last Activity: 17 November 2004, 5:39 PM EST
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
That worked, THANKS!!!!

The sed command to remove any last character, worked like a champ! Thank you.
Sponsored Links
 

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
Awk to remove carriage return from 65th field pinnacle Shell Programming and Scripting 7 03-22-2012 11:51 AM
Remove carriage return in a record agathaeleanor Shell Programming and Scripting 10 08-03-2011 04:29 AM
Remove ^M (carriage return) with string manipulation aquila_1 UNIX for Dummies Questions & Answers 3 05-28-2011 04:24 PM
Insert a line including Variable & Carriage Return / sed command as Variable lowmaster Shell Programming and Scripting 2 05-19-2009 11:26 PM
To remove carriage return between the line shash UNIX for Dummies Questions & Answers 5 07-31-2007 12:12 PM



All times are GMT -4. The time now is 08:32 AM.

Unix & Linux Forums Content Copyrightę1993-2018. All Rights Reserved.
×
UNIX.COM Login
Username:
Password:  
Show Password





Not a Forum Member?
Forgot Password?