Print multiple lines from a Text Document - Bash [duplicate] - bash

This question already has answers here:
Losing newline after assigning grep result to a shell variable
(4 answers)
Closed 7 years ago.
I have a program that is trying to print multiple lines from a text document. I have my main bash program, and I am using the line:
Multlines=`</Users/$USER/Documents/text.txt`
echo $Multlines
Where text.txt may look something like
John
Smith
but the echo prints John Smith where I want it to print it on different lines.
How do I print it out that way?

To preserve the newline characters in the variable when printing with echo, you need to double quote it:
echo "$Multlines"

You could also just use cat instead of echo, assuming you had it available to you.

Related

How to write to a file with escape characters in bash [duplicate]

This question already has answers here:
Echo newline in Bash prints literal \n
(22 answers)
Closed 1 year ago.
A very simple question. I'd like to write several lines to a file with just one line containing multiple \n. I.e. my input string would be hello\nhello\nhello, I'd like to write it to a file hello.txt, and when cat hello.txt it should give me
hello
hello
hello
However, when I do echo "hello\nhello\nhello" > hello.txt, it just gave me the literal string, without converting \n to a newline.
I wonder how to write such a line to a file.
You want either echo -e or, generally even better printf
printf 'Hello %s\n\nWelcome to my %s document\nmultiline document\n\n\n\nEnd\n' \
"John" "yellow" > "/some/path/hello.txt";

I want to remove a line from a file using sed [duplicate]

This question already has an answer here:
How to delete a line by passing line number as variable?
(1 answer)
Closed 3 years ago.
The line is specicied by the user so I have the number of the line in a variable
sed $input 'd' file.txt > file.txt
The problem here is that I don't know where and how to put the variable $input. I have tried lot's of combinations and there are all wrong.
I know that if I put a single integer it works but I don't know the way with a variable
sed -i "${input}d" file.txt
The variable needs to be in braces, use double quotes to prevent matching problems, and use the -i switch to act directly on the file

How to preserve new line in shell script output? [duplicate]

This question already has answers here:
Capturing multiple line output into a Bash variable
(7 answers)
Closed 5 years ago.
I have a line in my shell script like below
output_logs=`sh script2.sh $1 $2`
script2.sh produce lots of lines of logs. I want to grep specific lines of its output. The problem I have is $output_logs has the entire output of script2.sh as a single line string and grep produces strange results because of this. How to get the logs from script2.sh to be stored in individual lines?
Maybe you should try something like this.
output_logs=`sh script2.sh $1 $2`
echo "$output_logs"
Note that this is different from echo $output_logs.
The double-quoted version of the variable preserves internal spacing of the value exactly as it is represented in the variable — newlines, tabs, multiple blanks and all — whereas the unquoted version replaces each sequence of one or more blanks, tabs and newlines with a single space.

sed. passing a variable in sed command [duplicate]

This question already has answers here:
Replace a string in shell script using a variable
(12 answers)
Closed 7 years ago.
I want to use sed command in a loop passing a variable say a such that it searches for a and in the line it gets a it replaces "true" to "false".
I have a text file containing 3000 different names and another xml file containing 15000 lines. in the lines in which these 3000 entries are there i need to make changes.
I have written a code snippet but that is not giving expected output. Can anyone help. Thanks in advance.
for i in {1..3000}; do
a=`awk NR==$i'{print $1}' names.txt`
# echo $a
sed -e '/$\a/ s/true/false/' abc.xml > abc_new.xml
done
You have to replace single-quotes(') around sed's parameters with double-quotes("). In bash, single-quote won't allow variable expansion. Also, you might want to use sed's in-place edit (pass -i option) in your for loop.
So the one liner script will look like:
for a in `cat names.txt`; do sed -i.bak -e "/$a/s/true/false/" abc.xml ; done

Bash loop - tokenize on lines rather than words [duplicate]

This question already has answers here:
Bash and filenames with spaces
(6 answers)
Closed 8 years ago.
I'm writing a script to do variable substitution into a Java properties file, of the format name=value. I have a source file, source.env like this:
TEST_ENV_1=test environment variable one
TEST_ENV_2=http://test.environment.com/one
#this is a comment with an equal sign=blah
TEST_ENV_3=/var/log/test/env/2.log
My script will replace every occurence of TEST_ENV_1 in the file dest.env with "test environment variable one", and so on.
I'm trying to process a line at a time, and having problems because looping on output from a command like sed or grep tokenizes on white space rather than the entire line:
$ for i in `sed '/^ *#/d;s/#.*//' source.env`; do
echo $i
done
TEST_ENV_1=test
environment
variable
one
TEST_ENV_2=http://test.environment.com/one
TEST_ENV_3=/var/log/test/env/2.log
How do I treat them as lines? What I want to be able to do is split each line apart on the "=" sign and make a sed script with a bunch of substitution regex's based on the source.env file.
sed '/^ *#/d;s/#.*//' source.env | while read LINE; do
echo "$LINE"
done
An alternative is to change $IFS as per #Jim's answer. It's better to avoid backticks in this case as they'll cause the entire file to be read in at once, whereas piping the output of sed to while above will allow the file to be processed line by line without reading the whole thing in to memory.

Resources