Echo Appending Text [duplicate] - bash

This question already has answers here:
How to append output to the end of a text file
(13 answers)
Closed 5 years ago.
If I'm in terminal and I write some basic string to a file, is there a way to continually append to that file using echo?
For instance, echo 'hello' > file will put 'hello' into that file, but what if I now want to append ' world' to file? I know that if I do echo ' world', it'll overwrite the first string I wrote into file. Is there any += operator I can use in bash?
EDIT: Nevermind, I can append using >>. Is it possible to append to the same line instead of to a new line?

echo -n 'Hello' > file
echo ', World!' >> file
The >> redirection operator means "append."
Here we're using a non-standard extension of echo, where -n tells it not to append a new line.
If you want standard-compliant behavior, use printf:
printf 'Hello' > file
printf ', World!\n' >> file
Edit: using double quote around a string containing the exclamation ! may not work with some versions. It could get interpreted as a history expansion token. Use single quotes to prevent this.

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";

bash native string manipulation displays jumbled output

I tried to use the bash string native manipulation for substituting string with my shell variables.
var1='123'
var2='2018-01-01'
var3='2018-01-02'
var4='myfunction('var1','var2','var3')'
var5=${var4/var1/$var1}
echo $var5
var5=${var5/var2/$var2}
echo $var5
var5=${var5/var1/$var3}
echo $var5
Expected output:
myfunction('123','var2','var3')
myfunction('123','2018-01-01','var3')
myfunction('123','2018-01-01','2018-01-02')
Actual output with jumbled strings:
myfunction('123','var2','var3')
myfunction('123','2018-01-01','var3')
')function('123','2018-01-01','2018-01-02
Here the last two characters shift at the beginning and I lose the first two characters of the string.
I can use SED for the same. But I am just trying to figure out why will the bash native string manipulation not work as expected. Is it because I am doing multiple substitutions ?
Thanks for your help.
I'm not able to replicate your output with my version (4.2.46) of bash:
david#localhost ~ % cat test.sh
var1='123'
var2='2018-01-01'
var3='2018-01-02'
var4='myfunction('var1','var2','var3')'
var5=${var4/var1/$var1}
echo $var5
var5=${var5/var2/$var2}
echo $var5
var5=${var5/var1/$var1}
echo $var5
and the output:
david#localhost ~ % bash test.sh
myfunction(123,var2,var3)
myfunction(123,2018-01-01,var3)
myfunction(123,2018-01-01,var3)
For what it's worth, I presume you also mean to replace var3 with $var3 in the final line of the script, rather than var1?
Additionally, setting $var4 with the below line would save the replacing of the strings, if that's not mandatory:
var4="myfunction('$var1','$var2','$var3')"
The single-quotes within the string value of var4 don't prevent the variable substitution, as they're within double-quotes already.
I was able to solve it by using dos2unix command on the file. The error was due to carriage return (CR) character at the end of var3 content.

Linux env variable with newlines + sed [duplicate]

This question already has answers here:
I just assigned a variable, but echo $variable shows something else
(7 answers)
Replace a word with multiple lines using sed?
(11 answers)
Closed 4 years ago.
I've got a command that I run that outputs to stdio that I need to redirect to an existing file, replacing the word replaceme with the two lines output by my command.
The command mycommand outputs like this:
somedata=this
somedata=that
I'm able to get the output into an environment variable:
export TWOLINES=$(mycommand)
When I echo it back out it comes out as a single line
echo $TWOLINES
Returns:
somedata1=this somedata2=that
I need to get this content into my file as two lines
Attempting to do:
sed -e "s,replaceme,${TWOLINES}," -i file
Returns: unterminated 's' command
Thoughts or other tools besides sed that could assist?

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