Shell script to replace string having space with another string - linux

I'm trying to replace following string to multi lines as following
setsid /usr/local/bin/Naming_Service ${OPTIONS} &
replacing with
setsid /usr/local/bin/Naming_Service ${OPTIONS_13016} &
setsid /usr/local/bin/Naming_Service ${OPTIONS_13018} &
I tried with this command
sed '0,/setsid \/usr\/var\/run\/Naming_Serivce ${OPTIONS}/s//setsid \/usr\/var\/run\/Naming_Serivce ${OPTIONS_13016}\n\setsid \/usr\/var\/run\/Naming_Serivce ${OPTIONS_13018}\n /' script > new_script
can you please help to resolve

sed 's/^\(.*\)\(${OPTIONS}\)\(.*\)$/\1${OPTIONS_13016}\3\n\1${OPTIONS_13018}\3/' < script > new_script
(...) - create groups
\1 \3 - using these groups
\n - newline
.* - any character

For Your requirement use below syntax
Syntax:
sed -e "s/setsid \/usr\/local\/bin\/Naming_Service \${OPTIONS}/setsid \/usr\/local\/bin\/Naming_Service \${OPTIONS_13016} \&\nsetsid \/usr\/local\/bin\/Naming_Service \${OPTIONS_13018}/g" script > new_script

Related

How can I distinguish between file names with and without suffix?

I am writing a bash shell script to output the suffixes of filenames.
In this case I use:
sed 's|.*\.||'
So the output is e.g.:
png
exe
c
But what do I do if the file name has no suffix and therefore no dot? My output should be "no suffix", but I don't know how to do this with sed.
EDIT
What I've already tried:
Directory:
abc.x
abc.y
abc
Input:
find . -type f | sed -E 's/^[^.]+$/no suffix/; s/.*\.//'
Output:
x
y
/abc
Use 2 consecutive substitutions:
sed -E 's/^[^.]+$/no suffix/; s/.+\.//'
One in awk. First some test material that was not provided:
$ cat foo
this.foo
that.bar
nothing
The awk:
$ awk '{n=split($0,a,".");print (n>1?a[n]:"no suffix")}' foo
foo
bar
no suffix
$ cat file
abc.x
abc.y
abc
$ awk -F'.' '{print (NF>1 ? $NF : "no suffix")}' file
x
y
no suffix
How about
sed '/.*\./s///;t;s/.*/no suffix/'
The regex matches lines with a dot. On those lines, we perform a substitution. If a substitution occurred, we are done. Otherwise, perform the other substitution.
The use of an empty regex in the substitution pattern uses the previous pattern. The t command branches if a substitution occurred; without an argument, we branch to the end of the script. (Otherwise, you can set a label with :label and branch to that with tlabel.)
You can accomplish the same with the POSIX shell parameter expansions without invoking separate utilities. For example, to test whether a file contains a '.' you can simply use test, e.g.:
[ "$i" = "${i%.*}" ]
See Posix Programmer's Manual - Shell Command Language - Parameter Expansion
If it tests TRUE, then no extension is present, otherwise, you can use an additional parameter expansion to obtain the extension itself, e.g.
[ "$i" = "${i%.*}" ] && echo "$i - no suffix" || echo "$i - ${i##*.}"
(note: you would need an additional test to exclude .foo (e.g. dotfiles), but that is left to you)
Wrap that in a loop and exclude directory files and you can test every file within a directory or use read within a loop and pipe a list of names to it. For example, looping over the files in a directory would results in:
...
ftlcdfil.c - c
geany-plugin_build_w_gtk+2.patch - patch
geany-plugins.spec - spec
geany.spec - spec
geany_build_w_gtk+2.patch - patch
getfl - no suffix
gtkw_save_test.txt - txt
gtkwrite-master.zip - zip
helloleap - no suffix
helloleap.c - c
jnl - no suffix
messages - no suffix
opensuse_15.0_1s_delay.svg - svg
...
Using Perl
/tmp> ls ab*
abc abc.x abc.y
/tmp> perl -e ' print /\./? "$_\n" : "no suffix\n" for(glob("ab*")) '
no suffix
abc.x
abc.y
/tmp>

How to find and replace the string with iterated values (multi lines) in shell script

this is my shell script to replace the string
OPTIONS="-p ${PIDFILE}"
with multi line string values
OPTIONS_1234="-p ${PIDFILE_1234} -ORBEndpoint iiop://localhost:1234"
OPTIONS_1235="-p ${PIDFILE_1235} -ORBEndpoint iiop://localhost:1235"
OPTIONS_1236="-p ${PIDFILE_1236} -ORBEndpoint iiop://localhost:1236"
based on my input port number , that many OPTIONS should be created.
#!/bin/bash
NS_HOSTNAME= localhost
namingService_ports = 1234,1235,1236
IFS=','
read -r -a portArray <<< "$namingService_ports"
for port in ${portArray[#]}; do
sed '0,/\PTIONS="-p ${PIDFILE}"/ s//\OPTIONS_'"$port"'="-p ${PIDFILE_'"$port"'} -ORBEndpoint iiop:\/\/'"$NS_HOSTNAME"':'"$port"'"\n /' "/etc/init.d/tao" > "tao_ns1"
done
can some suggest me how sed command will take the multi line with for loop and move to the file "tao_ns1"
You basically have 3 issues with your script.
The assignment namingService_ports = 1234,1235,1236 is incorrect in bash and it should be without spaces, i.e. namingService_ports="1234,1235,1236"
There is a typographical error in first sed option for OPTIONS i.e. sed '0,/\PTIONS="-p ${PIDFILE}" should have been sed '0,/\OPTIONS="-p ${PIDFILE}"
And while writing to a file "tao_ns1" you are using the > operator which basically over-writes the file every-time you run it. It should have been the >> append operator, which appends the new line to the file for every iteration of the loop.
Also http://www.shellcheck.net/, that you have carriage return characters in your script, run tr command on it before proceeding next.
tr -d '\r' < current_script.sh > new_script.sh
With the above fixes made.
#!/bin/bash
NS_HOSTNAME="localhost"
namingService_ports="1234,1235,1236"
IFS=','
read -r -a portArray <<< "$namingService_ports"
for port in ${portArray[#]}; do
sed '0,/\OPTIONS="-p ${PIDFILE}"/ s//\OPTIONS_'"$port"'="-p ${PIDFILE_'"$port"'} -ORBEndpoint iiop:\/\/'"$NS_HOSTNAME"':'"$port"'"\n /' "/etc/init.d/tao" >> "tao_ns1"
done
should work fine.
Two examples that I hope helps to reach want you want to ...
Changing a string and redirect the result to a new file, replacing AAA to BBB:
echo "xxxAAAxxx" | sed "s/AAA/BBB/i" > out.ini
Same logic (replacing BBBto CCC), but at this time replace some string inside an existing file
sed -i "s/BBB/CCC/i" out.ini

How to replace the a specific character in sed command which have predefine meaning?

I have this text
" File: 'space folder' "
I want to replace this with only this
" space folder "
using sed or awk?
But when i try to do with it using sed it's not taking the command!
Does anyone have solution for this.
If I get your intent correctly, you need all text between single quotes; you can use this:
$ sed -r "s/^.*'([^']*)'.*$/\"\1\"/g" <<< "\" File: 'space folder' \""
"space folder"
$
Edit1: explanation
command <<< string => <<< indicates here string that is you pass a string to the command.
Our final string is this:
$ echo -e "\" File: 'space folder' \""
" File: 'space folder' "
$
since our string contains single quotes we use double quotes for sed command:
-r switch enables extended regular expression
"s/^.*'([^']*)'.*$/\"\1\"/g"
the above command substitutes the whole line with text present between single quotes.
Regular expression breakdown:
^ matches start of line
.* matches 0 or more characters
' matches a literal single quote
([^']*) matches 0 or more characters that are not single quote
and remembers it as a captured group with backreference number \1
' matches literal single quote
.* matches 0 or more chars
$ matches end of line

how to edit a line using sed or awk in linux containing a certain number or string

My Stress.k file is as follows
180.4430
*INCLUDE
$# filename
*STRESS_INITIALIZATION
*END
I want it to be like
180.4430
*INCLUDE
$# filename
*STRESS_INITIALIZATION
*/home/hassan/534.k
*END
for that I used sed as follows
a="$(cat flow.k)"
sed -i -e '/*END/i \*/home/hassan/$a.k ' Stress.k
where flow.k has only a single number like 534.k or something . Here sed put the line before END but it doesn't take the value of a , instead it puts the same alphabet and it doesn't understand $a.k.
Please also tell me how to delete the second last line or the line with a string hassan for example so that I can delete it first and the for the next step I use it to enter my required line.
if possible please also suggest the alternatives.
best regards
bash variables are only replaced when in double quotes, e.g.
sed -i -e "/*END/i \*/home/hassan/$a.k " Stress.k
Use double quotes to allow the variable to be expanded.
sed -i -e "/*END/i \*/home/hassan/$a.k " Stress.k
To replace the string, do it as you read in the file:
a=$(sed 's/534/100/' flow.k)
To delete a line:
sed '/hassan/d' inputfile
To read a file into the stream after the current line:
sed '/foo/r filename' inputfile

Replace one character with another in Bash

I need to replace a space ( ) with a dot (.) in a string in bash.
I think this would be pretty simple, but I'm new so I can't figure out how to modify a similar example for this use.
Use inline shell string replacement. Example:
foo=" "
# replace first blank only
bar=${foo/ /.}
# replace all blanks
bar=${foo// /.}
See http://tldp.org/LDP/abs/html/string-manipulation.html for more details.
You could use tr, like this:
tr " " .
Example:
# echo "hello world" | tr " " .
hello.world
From man tr:
DESCRIPTION
Translate, squeeze, and/or delete characters from standard input, writ‐
ing to standard output.
In bash, you can do pattern replacement in a string with the ${VARIABLE//PATTERN/REPLACEMENT} construct. Use just / and not // to replace only the first occurrence. The pattern is a wildcard pattern, like file globs.
string='foo bar qux'
one="${string/ /.}" # sets one to 'foo.bar qux'
all="${string// /.}" # sets all to 'foo.bar.qux'
Try this
echo "hello world" | sed 's/ /./g'
Use parameter substitution:
string=${string// /.}
Try this for paths:
echo \"hello world\"|sed 's/ /+/g'|sed 's/+/\/g'|sed 's/\"//g'
It replaces the space inside the double-quoted string with a + sing, then replaces the + sign with a backslash, then removes/replaces the double-quotes.
I had to use this to replace the spaces in one of my paths in Cygwin.
echo \"$(cygpath -u $JAVA_HOME)\"|sed 's/ /+/g'|sed 's/+/\\/g'|sed 's/\"//g'
The recommended solution by shellcheck would be the following:
string="Hello World" ; echo "${string// /.}"
output: Hello.World

Resources