3

I've been smashing my head against the wall for 2 hours already but can't get it work right.
Here is what I have:

  • String, say 133, with "\r" at the end, so it is "133\r".
  • Integer, which I want to add to the number from the string above.

I tried sed to remove "\r" with sed from that string and just (( )) with hope that math expression will truncate "\r" but still no success. Please, point me to my mistakes!

Thanks in advance!
Below is the file I read my string from and let's pretend that I'm forced to read this all the file (Screenshot from Notepad++).

6
  • Please include the code you actually tried. Commented Jun 6, 2013 at 12:44
  • Does the string end with an ASCII carriage return, or the two literal characters '\' and 'r'? Commented Jun 6, 2013 at 12:45
  • LF corresponds to \n, not \r !!! LF is for unix systems and \r is for dos, and corresponds to CR (see here:en.wikipedia.org/wiki/Newline) Commented Jun 6, 2013 at 13:02
  • @bendaizer, I'm totally confused then, cause when I try to sum string and int I'm getting error )2yntax error: invalid arithmetic operator (error token is "\r" Commented Jun 6, 2013 at 13:07
  • could you show your code ? Commented Jun 6, 2013 at 13:08

6 Answers 6

4

You could use awk:

awk 'BEGIN { var="133\r"; print var+3; }'

outputs 136

or you could use bash parameter expansion:

VAR="133\r"
VAR2=${VAR%\\r}
echo $VAR

outputs: 133, so now that VAR2 contains the number stripped of the \r you can add an integer to it.

Sign up to request clarification or add additional context in comments.

Comments

3
$ a=$'133\r'
$ printf "%s" "$a" | od -c
0000000   1   3   3  \r
0000004
$ echo $((a+3))
")syntax error: invalid arithmetic operator (error token is "
$ echo $(( ${a%[[:space:]]} + 3 ))
136

2 Comments

This is the state of the art! Thanks you a lot for your help!
of course, the key os to avoid getting the CR into the string in the first place, not dealing with it after the fact. This is a quick dos2unix clone: sed 's/\r$//' file | ...
3

No need for sed just to remove a substring. bash can do it for you.

#!/usr/bin/env bash

mystr="133\r"
myint=2

newstring=${mystr%\\r}               # Remove the substring "\r" from $mystr
myanswer=$(expr $newstring + $myint) # Add the two numbers
echo $myanswer                       # 135

5 Comments

unfortunately it spits out the following: expr: non-integer argument
See my comment in the question regarding what is actually at the end of your string.
Use the standard $(( newstring + myint )) in place of the expr command.
@chepner When I change $(expr $newstring + $myint) to $(( newstring + myint )) it says ")2yntax error: invalid arithmetic operator (error token is "\r
You have a literal carriage return, not a two-character sequence, at the end of the string. Use ${mystr%?} as a simple standard way to remove it. If there is a chance that mystr ends with something other than a carriage return, you can use the bash-specific (although slated to be included in the standard, I believe) ${mystr%$'\r'}.
2

Simple solution

a="133\r"
b=${a/\\r/}

or even simpler

b=${a%\\r}

as it was suggested by chepner

1 Comment

Even simpler (and standard): ${a%\\r}.
1

You can get the number using sed as

sed 's/\([0-9]*\).*/\1/'

Comments

1

If you want to remove \r with sed, don't forget to escape the \, this works for me :

echo "133\r" | sed 's/\\r//' 

and outputs 133

EDIT :

as I said in my comment : "LF corresponds to \n, not \r !!! LF is for unix systems and \r is for dos, and corresponds to CR (see here:en.wikipedia.org/wiki/Newline)"

So you should try this instead :

echo "133\n" | sed 's/\\n//' 

1 Comment

That outputs absolutely the same what it read from the file (please, find my screenshot in the edited post)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.