deleting 'carriage return' from string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting deleting 'carriage return' from string
# 1  
Old 03-10-2008
deleting 'carriage return' from string

hi there

I'm getting a string from a sqlplus query, and I need to compare it with another string

Problem here, is that the string i get from query brings a 'carriage return' with it, and comparing throws always false value.

i need to delete all carriage retun charactres in string.

how can i do that?

regards
VIKO
# 2  
Old 03-10-2008
give an exampel from your file... if there is a ^M at the end of each line you can use the "dos2unix" command for example.
# 3  
Old 03-10-2008
Or:

Code:
sed 's/^M$//' file > newfile

Press Ctrl-V then Ctrl-M toget ^M in the sed command.

Regards
# 4  
Old 03-10-2008
i get string from a sql query, so it's in a variable: $string

i can't see carriage return any time in variable, because echo $string does no print these. and i'm not sure i'm talking about a CR.

but i'm sure something is in there, because if i try
var1='
reply'

and then i try

if [ $var1 == 'reply' ]; then

this returns false value. but again, i'm not sure if a carriage return is inside the variable.

i was trying with
${string/^M/}

but no changes.

Last edited by viko; 03-10-2008 at 04:30 PM..
# 5  
Old 03-11-2008
well.

i made it, and just to help anyone eho needs something like this:

i have made something like:

dirty_var='
something'

var="$(echo $dirty_var | grep some_char_u_know_exists_in_var)"

for checking:
bash-2.05$ if [ "$dirty_var" == "something" ]; then echo OK; fi
bash-2.05$ if [ "$(echo $dirty_var | grep some)" == "something" ]; then echo OK; fi
OK
bash-2.05$



regards
VIKO
# 6  
Old 03-11-2008
Quote:
Originally Posted by viko
i get string from a sql query, so it's in a variable: $string

i can't see carriage return any time in variable, because echo $string does no print these. and i'm not sure i'm talking about a CR.

<snip>
If you pipe the line(s) to "od -c" you will be able to "see" the special characters (\t for tabs, etc.). That should help you make a more precise fix.

Example:

cat file.txt | od -c
# 7  
Old 03-13-2008
Thanks

that's very usefull for me

regards
VIKKKO
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

ksh string replacement over a carriage return

Hi. I need to make multiple string replacements in a file but several of the strings have been broken up by a carriage return so that the first few characters are on one line and the remainder on the following line. E.g like the word 'post' in the following: Use descriptive thread... (3 Replies)
Discussion started by: Big_Jeffrey
3 Replies

2. Shell Programming and Scripting

Substitute \n with carriage return

Hello all, I've a flat file in the following format: AB\001\CDED\001\ABC\001\nEG\001\HIJF\001\EFG\001\nHI\003\HIUL\003\HIJ\003 And I want to substitute \n with the carriage return. Any help is appreciated! Regards, - Seth (8 Replies)
Discussion started by: sethmj
8 Replies

3. Shell Programming and Scripting

Carriage return ksh

Hello, How do i usecarriage return in ksh. I want to do an echo "bla bla" and another echo "bla bla" will appear and replace the first echo on screen. I tried: until ; do echo "bla bla \r" done please advice. Thanks. (3 Replies)
Discussion started by: LiorAmitai
3 Replies

4. Shell Programming and Scripting

Removing Carriage return in a file after particular string

Hi All, I want to remove carriage return in a file using some unix command without writing a script my file is as follows abc1 abc2 abc3 abc4 abc5 bac6 abc1 abc2 abc3 abc4 abc5 bac6 I want the output as follows: abc1 abc2 abc3 abc4 abc5 bac6 abc1 abc2 abc3 abc4 abc5 bac6 , Please... (7 Replies)
Discussion started by: manish8484
7 Replies

5. Shell Programming and Scripting

Search_Replace with a Carriage Return

Hey folks, I've been working on this for some time. Seems simple, but I'm stumped. I need the following data format: New_York:Commercial Geology Geophysics Petrophysics Production_Engineering Reservoir_Engineering Pasadena:Commercial ... (5 Replies)
Discussion started by: leepet01
5 Replies

6. Shell Programming and Scripting

2 carriage return within a record

Hi all, need your help in replacing carriage return in a record. Input: col1|col2|col3|col4|col5|col6|col7|col8|col9|col10 1|aa|bb|cc|dd|eee eee|ff|ggggg|hh hhh|iii 2|zz|yy|xx|ww|vv|uu|tt|ss|rr Output: col1|col2|col3|col4|col5|col6|col7|col8|col9|col10... (12 Replies)
Discussion started by: agathaeleanor
12 Replies

7. UNIX for Dummies Questions & Answers

Remove ^M (carriage return) with string manipulation

Hello, I want remove ^M at end of my files line if I use command : tr -d '\r' <inp>out it work fine but get I the same result by manipulating the string ? I want this because in my text file I manipulate some other part I have input "the cat^M" I want output "the cat" I have made... (3 Replies)
Discussion started by: aquila_1
3 Replies

8. UNIX for Dummies Questions & Answers

carriage return and linefeed

hi can anyone please tell me the difference between carriage return, linefeed and newline ? (2 Replies)
Discussion started by: streetfi8er
2 Replies

9. Shell Programming and Scripting

Dont want carriage return

I have observed with print & echo, they produce carriage return <CR> or newline, after they display string next to them. Is there anyway to avoide these <CR> after the intended string is displayed? (3 Replies)
Discussion started by: videsh77
3 Replies

10. Shell Programming and Scripting

Capture carriage return.

I try to test the carriage return in a variable. $ LENGTH=`expr $VARIABLE : ".*"` will return the length of the variable. But this doesn't work if $VARIABLE has zero length. Any help will be well appreciated. Thanks in advance. Giovanni (4 Replies)
Discussion started by: gio123bg
4 Replies
Login or Register to Ask a Question