Print new line as string literal in unix or shell [duplicate] - linux

This question already has answers here:
How to replace one character with two characters using tr
(5 answers)
Closed 3 years ago.
Hi I have a shell script that has
variable="apple banana monkey"
I want it to be
apple\nbanana\nmonkey
But when I try and execute
echo $variable | tr ' ' '\n'
It results to
apple
banana
monkey
I want to get the actual literal of new line and not the evaluated value.
I have tried echo -e or echo -n or even put numerous escapes \\ but to no avail.
Please help. Thanks

tr command translates chars into chars by performing a 1 to 1 mapping. You are asking the tool to translate a space into two chars, which is something that cannot be done with tr.
If you accept a command switch, you can try with sed:
echo "$variable" | sed 's/ /\\n/g'

Related

perl replace with bash variable [duplicate]

This question already has answers here:
How can I process options using Perl in -n or -p mode?
(2 answers)
Closed 12 months ago.
could someone please explain, why perl doesn't replace the regexp:
root#machine08:~# VERSIONK8S='${VERSION_KUBERNETES:-1.23.3-00}'
# with sed as i want it
root#machine08:~# sed -e "s/^VERSIONK8S=.*/VERSIONK8S=${VERSIONK8S}/g" /root/coding/k8s-setup/allnodes_basic_setup.sh | grep ^VERSIONK8S=
VERSIONK8S=${VERSION_KUBERNETES:-1.23.3-00}
# with perl not exactly as i want it
root#machine08:~# perl -pe "s/^VERSIONK8S=.*/VERSIONK8S=${VERSIONK8S}/g" /root/coding/k8s-setup/allnodes_basic_setup.sh | grep ^VERSIONK8S=
VERSIONK8S=-e
From Perldoc:
If the delimiter chosen is a single quote, no variable interpolation is done on either the PATTERN or the REPLACEMENT. Otherwise, if the PATTERN contains a $ that looks like a variable rather than an end-of-string test, the variable will be interpolated into the pattern at run-time.
Perl tries to expand the substitution string ${VERSION_KUBERNETES:-1.23.3-00} as a perl variable starting with $ and fails. To avoid the expansion, please try:
perl -pe "s'^VERSIONK8S=.*'VERSIONK8S=${VERSIONK8S}'g" /root/coding/k8s-setup/allnodes_basic_setup.sh | grep ^VERSIONK8S=
by using a single quote as a delimiter instead of a slash such as s'pattern'replacement'.

SED replace a word inside a bracket [duplicate]

This question already has answers here:
What is the difference between `sed -i -e` and `sed -ie`?
(2 answers)
Closed 2 years ago.
I need to change some words in bulk, but because of the brackets inside it, I think I do something wrong.
Line that needs to be changed
echo "CMD_PLUGINS_ADMIN/admin/index.html";
I need to change it to this:
echo "CMD_PLUGINS_ADMIN/reseller/index.html";
I tried it with: sed -ie 's/admin/reseller/' *
But does not change anything, I hope someone knows the right command for it.
$ echo '"CMD_PLUGINS_ADMIN/admin/index.html";' | sed 's/\/admin\//\/reseller\//g'
"CMD_PLUGINS_ADMIN/reseller/index.html";
your input has slash and you are using slash as sed seperator
Either escape the slashes in input by preceeding them with backslash:
echo '"CMD_PLUGINS_ADMIN/admin/index.html";' | sed 's/\/admin\//\/reseller\//g'
"CMD_PLUGINS_ADMIN/reseller/index.html";
OR change seperator to any other like pipe:
echo '"CMD_PLUGINS_ADMIN/admin/index.html";' | sed 's|admin|reseller|g'
"CMD_PLUGINS_ADMIN/reseller/index.html";

how to use sed command properly to replace values containing / delimiter [duplicate]

This question already has answers here:
Using different delimiters in sed commands and range addresses
(3 answers)
Closed 5 years ago.
File: abc.properties
tomcat.home=/opt/tomcat
Set to /usr/local/tomcat. Following cmd is working.
sed -i "/tomcat.home=/ s/=.*/="usr\\/local\\/tomcat"/" abc.properties
Set to $WORKSPACE/tomcat. Following cmd is NOT working since value of the $WORKSPACE is having / delimeters.
sed -i "/tomcat.home=/ s/=.*/="$WORKSPACE\\/tomcat"/" abc.properties
Anyone has an idea how to success above cmd.
Thank you and appreciate your support...
Sed lets you use any character you want as the delimiter. Whatever follows the s is used as the separator:
sed -Ee 's/foo/bar/'
sed -Ee 's|foo|bar|'
sed -Ee 's#foo#bar#'
^- All of those are equivalent.
The other option is to escape all your / as \/, but that gets nightmarish fast. Prefer to just pick a separator character that doesn't collide with characters you're trying to use for something else.

Sed - How to switch two words in a line [duplicate]

This question already has answers here:
exchange two words using sed
(5 answers)
Closed 5 years ago.
I'm trying to write a shell script that switches the first and third words in a line. In this case only strings that contain letters (both upper- and lowercase) count as words, everything else (numbers, punctuation, whitespace) is considered whitespace.
For example:
abc123def. ghi...jkl
would turn into:
ghi123def. abc...jkl
I tried the following, but it doesn't work:
sed 's/\([a-zA-Z][a-zA-Z]*\)[^A-Z^a-z]\([a-zA-Z][a-zA-Z]*\)[^A-Z^a-z]\([a-zA-Z][a-zA-Z]*\)/\3 \2 \1/' input.txt
With sed:
$ echo "abc123def. ghi...jkl" | sed -r 's/([A-Za-z]*)([^A-Za-z]*[A-Za-z]*[^A-Za-z]*)([A-Za-z]*)(.*)/\3\2\1\4/g'
$ ghi123def. abc...jkl

Read first characters of a variable in bash script? [duplicate]

This question already has answers here:
In Bash, how can I check if a string begins with some value?
(13 answers)
Closed 6 years ago.
I have a file where i get some informations through a bash script to put data in a DB table and i'd like to know how to read the first characters of a variable because if it starts with "CE-" that line's data will go into a table if not they must be inserted in an other one, how can i do this?
Like this-
var=CE-xxxxx
echo "$var"
output- CE-xxxxx
var2=$(echo "$var" | cut -c 1-3)
echo "$var2"
output- CE-
Then you can check if $var2 matches your criteria and use it further.
You can use cut to get the bytes that you need:
V="CE-IMPORTANT"
I=$(echo $V | cut -b 4-)
If you want to use the - as separator:
I=$(echo $V | cut -d '-' -f 2)
In both cases you get "IMPORTANT" in I var

Resources