Remove a carriage return at end of variable


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Remove a carriage return at end of variable
# 1  
Old 11-17-2004
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?
# 2  
Old 11-17-2004
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.
# 3  
Old 11-17-2004
^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
echo "helloworld^M" | sed 's/\^M//'
# 5  
Old 11-17-2004
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 .... ;
# 6  
Old 11-17-2004
That worked, THANKS!!!!

The sed command to remove any last character, worked like a champ! Thank you.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Remove carriage return

I need to remove the carriage return comes inbetween the record. Need to have CR only at the end. I used the below command. tr -d '\n' < filewithcarriagereturns > filewithoutcarriagereturns But its removing all the CR and giving one line output. Input File: 12345 abcdegh... (11 Replies)
Discussion started by: srvn_saru
11 Replies

2. Shell Programming and Scripting

Remove carriage return from the variable

Hi, I try to handle very large numbers with a bash script. I run ssh command in a remote server and store the output in a local variable. But this output contains a return carriage at the end. So I try to remove it by tr But I can't figure out the right notation with printf. So my problem... (6 Replies)
Discussion started by: Meacham12
6 Replies

3. Shell Programming and Scripting

Remove carriage return in a record

Hi all gurus, I need help in removing carriage return existed within a record delimited by pipe <|>. Sample: A_01|Test1|Testing1|Remarks1 A_02|Test2|Test ing2|Remarks2 A_03|Test3|Testing3| Remarks3 Desire output: A_01|Test1|Testing1|Remarks1 A_02|Test2|Testing2|Remarks2... (10 Replies)
Discussion started by: agathaeleanor
10 Replies

4. Shell Programming and Scripting

Need a carriage return at end of each line

Hi All, I am reading two files and writing out the file name and count of lines in each file to an output file. My script looks like this: echo "input_file1.out;`wc -l < input_file1.out | sed 's/^]*\(.*\)]*$/\1/'` " > comp_file1.out echo "input_file2.out;`wc -l < input_file2.out | sed... (2 Replies)
Discussion started by: Hangman2
2 Replies

5. Shell Programming and Scripting

Insert a line including Variable & Carriage Return / sed command as Variable

I want to instert Category:XXXXX into the 2. line something like this should work, but I have somewhere the wrong sytanx. something with the linebreak goes wrong: sed "2i\\${n}Category:$cat\n" Sample: Titel Blahh Blahh abllk sdhsd sjdhf Blahh Blah Blahh Blahh Should look like... (2 Replies)
Discussion started by: lowmaster
2 Replies

6. UNIX for Advanced & Expert Users

Padding Carriage return to the end of XML file

Hi All, I am getting a xml file where the first field contains a carriage return and the all other fields doesnot contains any carriage return. So all the other records comes in the second line. <?xml version="1.0" encoding="UTF-8"?> <ns0:iSeriesCspIntegration... (3 Replies)
Discussion started by: dasj22
3 Replies

7. Shell Programming and Scripting

add carriage return at end of file

Hi I would like to add carriage return at end of file, because we need to mask the customer names for detailed records. Some what the file doesnot have carriage at end of line of last record.So that i 'll get 2 records when use ---aa.txt----- 1|aaa|bbb|ccc 2|bbbb|hghgh|ggg 000002 tail... (2 Replies)
Discussion started by: HAA
2 Replies

8. UNIX for Dummies Questions & Answers

To remove carriage return between the line

Hi, I have a situation where I need to remove the carriage return between the lines. For.eg. The input file: 1,ad,"adc sdfd",edf 2,asd,"def fde",asd The output file should be 1,ad,adc sdfd,edf 2,asd,def fde,asd Thanks Shash (5 Replies)
Discussion started by: shash
5 Replies

9. Shell Programming and Scripting

Carriage Return at end of file

Hi, I have a script that outputs a file that contains the dates from the previous month, which is then used by our application to run processes on each date contained in the file. My problem is is that my script created a blank line at the bottom of the file which causes issues for our... (14 Replies)
Discussion started by: bd_joy
14 Replies

10. Shell Programming and Scripting

Regex to pick up name from the following including carriage return at end of the line

has anyone got any suggestions how i would pick up the string as part of a substitution inclusive of the carriage return. ie i want to pick up <<NAME>> from the PS output but the <<; seems to be on the line before the NAME. Any ideas are appreciated! ... (3 Replies)
Discussion started by: Shakey21
3 Replies
Login or Register to Ask a Question