0

I wanted to concatenate two variables, but it seems that there is some overwriting.

#!/bin/bash
NUMBER1=$(seq 1 900 | sort -R | head -1)
FIRST=$(sed -n ''$NUMBER1'p' names.txt)
echo ${FIRST}
echo "${FIRST}${NUMBER1}"

Where names.txt is a list of names. For example when I run this code, I get the output as,

Gregoria

159goria

Notice $FIRST was partially overwritten by $NUMBER1 .

The correct output should have been,

Gregoria

Gregoria159

can someone please help me ? Thanks

2
  • 1
    Your input file has CR+LF line endings. Run dos2unix or some other utility to remove CRs. For example, dos2unix names.txt might help. Commented Mar 15, 2014 at 4:56
  • please show us names.txt content Commented Mar 15, 2014 at 5:00

1 Answer 1

2

Your names.txt file has Windows line-endings, CR-LF. The CR (carriage return) is not being recognized as part of the new-line sequence by sed, so it stays on the end of the line Gregoria<CR>; consequently, the next characters get overprinted at the beginning of the line.

Use dos2unix or some equivalent to fix the line endings.

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

2 Comments

What you change are the line-endings. How you change them, if you have dos2unix installed, is dos2unix names.txt. For a lot of elaboration, see en.wikipedia.org/wiki/Newline
alternative to dos2unix: sed -i 's/\r$//' filename

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.