shell script replace and add next line with special characters - linux

I am trying replace a line with another line and add new line below that using shell script. My file content looks like below. I want to replace export PATH with JAVA_HOME=/usr/lib/jvm/java-1.8.0-ibm-1.8.0.2.10-1jpp.1.el7.x86_64
export PATH JAVA_HOME
before:
export PATH
It should be after:
JAVA_HOME=/usr/lib/jvm/java-1.8.0-ibm-1.8.0.2.10-1jpp.1.el7.x86_64
export PATH JAVA_HOME
That means, it has to replace replace export PATH with these two lines.
Please help me how can I do with sed or shell script
Thanks,
Kumar.

sed -e 's#^export PATH$#JAVA_HOME=/usr/lib/jvm/java-1.8.0-ibm-1.8.0.2.10-1jpp.1.el7.x86_64\n\nexport PATH JAVA_HOME#' <yourfile.txt
This is just a single substitute command in sed.
Some notes:
The # is used after s instead of the usual / to avoid having to quote all slashes in the path. Otherwise you have to quote each / in the path this way: /. ow you only have to quote #.
The escape sequence \n insert a newline.
Add the -i parameter to sed if you want to actually update the file. The code above only prints the new file to stdout.
The regex is anchored (^...$) so that it matches the whole line and not just a part of it.

Related

How to read a variable from file, modify and safe it to an other variale

What I want:
There is a file /scripts/backup/config.cfg which contains variables. In my specific case the important ones are:
BACKUPLOCATION=""
ROOTLOCATION="/backup"
Then there is a script /scripts/backup/performBackup.sh
For a specific reason I want a part of the script do the following operations:
read the value of the variable ROOTLOCATION
add a ("/" and) timestamp (Date&Time)
safe the new created value to BACKUPLOCATION (by replacing its current value)
Example
If this is the previous state of the config.cfg:
BACKUPLOCATION="dummy"
ROOTLOCATION="/backup"
After the script ran it should be:
BACKUPLOCATION="/backup/2020-05-02-23-00"
ROOTLOCATION="/backup/"
What I tried
First of all the config file gets "loaded" using
source /scripts/backup/config.cfg
I then tried to use the sed command but the quotes are messing with me. Here is one try (which didn't work):
sed -i 's/BACKUPLOCATION\=.*/BACKUPLOCATION="'$ROOTLOCATION/$(date +%Y-%m-%d-%H-%M)'"/' /scripts/backup/config.cfg
Try this:
source /scripts/backup/config.cfg
sed -i 's|BACKUPLOCATION=.*|BACKUPLOCATION="'"$ROOTLOCATION/$(date +%Y-%m-%d-%H-%M)"'"|' /scripts/backup/config.cfg
The problem with your sed is that you use / as delimiter, which is present in $ROOTLOCATION after expansion, therefore sed fails. I used |, which is usually is not present in filenames. If you ever create a file with |, that sed will fail too! So, "know your data" :)

How to include path inside bashrc in linux for envirnment

Im testing my code for automation of the installation of a software
In bashrc file below:
# User specific aliases and functions
export JAVA_HOME=/opt/jdk-9.0.1
export JRE_HOME=/opt/jdk-9.0.1/jre
export SCALA_HOME=/opt/scala-2.13.0
export PATH=$PATH:/opt/jdk-9.0.1/bin:/opt/jdk-9.0.1/jre/bin
Here im trying to add $SCALA_HOME/bin to PATH.
this is the required output:
`export PATH=$PATH:/opt/jdk-9.0.1/bin:/opt/jdk-9.0.1/jre/bin:/opt/scala-2.13.0`
`sed -i '1n;/^export PATH/i\export SCALA_HOME=/opt/scala-2.13.0' .bashrc`
the above code worked to append SCALA_HOME above path but for appending in the same line im not able to do
`sed -i "s/\"export PATH\":.*,$/\"export PATH\": \":$SCALA_HOME/bin\",/g" .bashrc
sed: -e expression #1, char 40: unknown option to `s'`
please help me get the correct sed command to append SCALA_HOME in the PATH
You can use this:
sed '/export PATH/ s/$/:\$SCALA_HOME\/bin/' .bashrc
's/\(export PATH=.*\)/\1:\$SCALA_HOME\/bin/'
To go through your expression:
s/\"export PATH\":.*,$/\"export PATH\": \":$SCALA_HOME/bin\",/g"
The \"export will look for "export in your file. Why do you expect a double quote before the export? It isn't there in the example. Likewise, PATH\": in the pattern will look for PATH": in the file. That double quote isn't there either. Your ,$ at the end of your pattern will also prevent it from matching anywhere.

How do I create files with special characters in Linux?

I am using the touch command to try and create a file with the name "\?$*'KwaMe'*$?\" (quotation marks included as part of the file name). However when I type touch "\?$*'KwaMe'*$?\" in the Terminal, it doesn't give me the result I am expecting. How can I create this file?
You need to escape special characters with the backslash symbol (\).
This command will create a file named "\?$*'KwaMe'*$?\":
touch \"\\\?\$\*\'KwaMe\'\*\$\?\\\"
Explanation
Double your \, like this: \\, so that your shell does not interpret the backslashes from your filename as escape characters.
Escape " and ', like this: \", \', so that your shell interprets the double quotes as part of the filename.
Escape $, like this: \$, otherwise your shell will think you're using a variable.
Escape ? and *, like this: \?, \*, to prevent filename expansion.

How do you replace a line with forward slashes in it in EX?

I'm running a script for vim EX mode I've tried every escape character and word identifier I can find.
it needs to find the string "/etc/walker" and replace it with "/etc/runner"
% s/\</etc/walker\>/\</etc/runner\>/g
wq
same issue with a script to append at the end of the file. It doesn't do anything. I'm trying to append "/etc/walker"
$
a
\</etc/walker\>
.
wq
what I've tried on regex editors seems to work there but not in EX
Thanks for your help
Try this:
:s#/etc/walker#/etc/runner#
Notice the use of # as a delimiter, that way you don't have to add back slashes.
You could also use:
:s#/etc/walker#/etc/runner#
For appending at the end of the line:
:s#$#/etc/walker#
In EX mode just remove the : at the beginning.

Shell script handle string with sed

I have a text file, each line is one or more file paths separated with space, all the file has suffix dl, e.g.
/some/path/file.dl
/some/other/path/file2.dl /some/other/path2/file3.dl
/some/other/path3/file4.dl /some/other/path4/file5.dl ...
...
Now I need to transform the above file to another text file. Only the first file of every line should be changed to /out/P{fileName}.h:, {fileName} is the original file name without directory and suffix. e.g.
/out/Pfile.h:
/out/Pfile2.h: /some/other/path2/file3.dl
/out/Pfile4.h: /some/other/path2/file5.dl ...
...
So how can I write the linux shell script?
Try this command:
$ sed -r 's#^\S*/(\S*)\.dl#/out/P\1.h:#' input
/out/Pfile.h:
/out/Pfile2.h: /some/other/path2/file3.dl
/out/Pfile4.h: /some/other/path4/file5.dl

Resources