1

I writing a shell script to parse some file using grep and use the result in subsequent commands.

The relevant part of the source code is

VERSION=`grep "^Stable tag:" readme.md | awk -F' ' '{print $NF}'`
echo $VERSION
echo "Readme file version is $VERSION is it correct"

The relevant line in the readme.md file is

Stable tag: 2.2.2

You would expect a output of

2.2.2
Readme file version is 2.2.2 is it correct

But I am getting the following as the output

2.2.2
 is it correctrsion is 2.2.2

I am pretty sure there is no (non-printable or non-ascii) character at the end of line in my text file. I checked it by enabling the :set invlist command in vim.

Any idea why it is happening like this? Or any other ideas to debug this issue?

3
  • 2
    You can use awk alone without grep: awk -F' ' '/^Stable tag:/{print $NF}' readme.md Commented Dec 21, 2012 at 13:40
  • @aragaer Thanks. Didn't know that I can use awk this way. Now that I have to include tr also (see the answer below), do you think I can still do it using awk alone? Commented Dec 21, 2012 at 13:56
  • man awk and I see sub there. Probablly something like awk -F' ' '/^Stable tag:/{sub("\r", "", $NF); print $NF}' readme.md, though I'd think about sed at that point already. Commented Dec 21, 2012 at 15:57

1 Answer 1

2

This looks like a DOS edited file and you working on a Unix like system. Strip the carriage return (Ctrl-M).

You could use tr(1) to remove those:

VERSION=`grep "^Stable tag:" readme.md | tr -d '\015' | awk -F' ' '{print $NF}'`

Awk probably also has some builtin feature to work on these but I do not know awk enough for this.

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

6 Comments

Any idea how I can remove it from the shell script variable?
tr -d '\r' also works, and is possibly more understandable than an octal code.
Use VERSION=$(sed '/Stable tag:/s/.* \(.*\)\r/\1/' readme.md) instead of the grep|tr|awk pipeline
VERSION=$(sed -n '/Stable tag:/s/.* \(.*\)\r/\1/p' readme.md) as readme.md might contain other lines too.
@glennjackman & aragaer: these should be answers, not comments.
|

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.