How can I replace a string in a file if line starts with another string using sed?
For example, replace this line:
connection = sqlite://keystone:[YOURPASSWORD]#[YOURIP]/keystone
With this line:
connection = mysql://keystone:password#10.1.1.10/keystone
Answer:
sed '/^start_string/s/search_string/replace_string/'
Information at http://www.gnu.org/software/sed/manual/sed.html#Addresses
If you want to change an entire line which starts with a pattern and ends with enything else, you can use command c description Example
sed -i '/^connection = sqlite/c\connection = mysql://keystone:password#10.1.1.10/keystone' your_file
You can do simply this :
sed -ri 's/sqlite/mysql/g' YOURFILE
sed '/^string1/ { s,string2,string3, }' file
This will replace string2 with string3 on all the lines that start with string1.
Related
The test23 file contains a line
root_url = %(protocol)s://%(domain)s:%(http_port)s/
I need to replace this with
root_url = %(protocol)s://%(domain)s:%(http_port)s/grafana
This below steps in the script doesn't work.
line_old='root_url = %(protocol)s://%(domain)s:%(http_port)s/'
line_new='root_url = %(protocol)s://%(domain)s:%(http_port)s/grafana'
sed -i "s/\${line_old}\/\${line_new}/\g" test23
Try changing delimiter of sed:
sed -i "s|${line_old}|${line_new}|g" test23
example
echo "${line_old}" | sed "s|${line_old}|${line_new}|g"
root_url = %(protocol)s://%(domain)s:%(http_port)s/grafana
I would use:
sed '/^root_url[ ][=]/s/$/grafana/' filename
Where:
/^root_url[ ][=]/ locates the line beginning with "root_url =", then
the normal substitution form 's/$/grafana/' simply appends "grafana" to the end of the line.
(note: add -i to edit the file in-place, or add -i.bak to edit in-place preserving a copy of the original file unchanged in filename.bak)
Results
root_url = %(protocol)s://%(domain)s:%(http_port)s/grafana
Look things over and let me know if that meets your needs and if you have any questions.
I have a file which has 2000 lines of data (file name is data.tsv). I want to replace the string with empty line where there is a matching pattern, in my case is PMC:
How can I do with Vim or other sed command?
Thanks,
Rio
With vim you can do it like this:
:g/PMC:/normal S
Try this with GNU sed:
sed -i 's/.*PMC:.*//' data.tsv
Another way is:
:%s/.*PMC:.*//
where the '%' means 'every line' and the 's' is subsitute. An alternate:
:g/PMC:/s/.*//
where the 'g/PMC:/' is short for 'global if line contains "PMC:"' and the 's/.*//' means 'match anything and replace it with the empty string'
I have a file like this (tens of variables) :
PLAY="play"
APPS="/opt/play/apps"
LD_FILER="/data/mysql"
DATA_LOG="/data/log"
I need a script that will output the variables into another file like this (with space between them):
PLAY=${PLAY} APPS=${APPS} LD_FILER=${LD_FILER}
Is it possible ?
I would say:
$ awk -F= '{printf "%s=${%s} ", $1,$1} END {print ""}' file
PLAY=${PLAY} APPS=${APPS} LD_FILER=${LD_FILER} DATA_LOG=${DATA_LOG}
This loops through the file and prints the content before = in a format var=${var} together with a space. At the end, it prints a new line.
Note this leaves a trailing space at the end of the line. If this matters, we can check how to improve it.
< input sed -e 's/\(.*\)=.*/\1=${\1}/' | tr \\n \ ; echo
sed 's/"\([^"]*"\)"/={\1}/;H;$!d
x;y/\n/ /;s/.//' YourFile
your sample exclude last line so if this is important
sed '/DATA_LOG=/ d
s/"\([^"]*"\)"/={\1}/;H;$!d
x;y/\n/ /;s/.//' YourFile
How to insert string to the beginning of the last line?
I want to add a time stamp to a text file which contains multiple lines
var1 = `date`
LINE1
LINE2
LINE3
...
(INSERT var1 here) LASTLINE
sed 's/^/test line /g' textfile inserts characters to the beginning of every line but how can I specifically modify the last line only?
Thanks
Going forward:
sed '$s/^/sample text /' textfile works, but only when inserting regular strings. If I try
var1 = "sample text"
and use substition, here are the problems I encounter
using single quotes in sed does not expand variables, so sed '$s/^/$var1/' textfile will insert the string $var1 into the beginning of the last line.
To enable variable substitution I tried using double quotes. It works when I specify the exact line number. something like:
sed "5s/^/$var1/" textfile
But when I try sed "$s/^/$var1" text file, it returns an error:
sed: -e expression #1, char 5: extra characters after command
Can someone help me please?
Like this:
sed '$s/^/test line /' textfile
$ indicates last line. Similarly, you can insert into a any specific line by putting the line number in place of $
But when I try sed "$s/^/$var1" text file, it returns an error:
It returns an error because the shell attempts to expand $s since you've used double quotes. You need to escape the $ in $s.
sed "\$s/^/$var1/" filename
sedshould be the best tool, but awk can do this too:
awk '{a[++t]=$0} END {for (i=1;i<t;i++) print a[i];print v$0}' v="$var1" file
It will insert value of var1 in front of last line
Another variation
awk 'NR==f {$0=v$0}1' v="$var1" f=$(wc -l file)
PS you do not need to specify file after awk, not sure why. If you do so, it reads it double.
This command would work for you:
sed -i "5s/^/$var1 /" text file
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