sed creates duplicate line instead of replacing existing line - bash

I have a file (foo.txt) containing the following:
some-text 0
I use the following sed-command to replace the 0 with a 1:
search_text="some-text";
sed "s/${search_text} 0/${search_text} 1/" -i foo.txt;
This results in foo.txt containing:
some-text 0
some-text 1
How can I get it to replace the found line instead of appending a new line?
It occurs with GNU sed version 4.2.1 on SL06.

If you like to try awk
awk '/some-text/ {$2=1} 1' file

do you try
search_text='some-text'
sed -e "s/\(${search_text}\) 0/\1 1/" -i foo.txt
using group pattern instead of twice the search_text
which shell are you using (cause i see ; like c end of line not often used on several line in shell) ?

Related

bash sed replace a line number in a file containing a colon

I am on RHEL 7.3. Line 12 of myfile.txt looks like:
image: /currentimage/myimage
I need a bash script to change it to:
image: /newimage/otherimage
I tried doing it this way:
sed -i '12s/image: /currentimage/myimage/image: /newimage/otherimage/' ./myfile
But it fails with:
sed: unknown option to `s'
You are using / as your sed separator, AND it is used in your paths. Try using | as your separator instead.
sed -i '12s|image: /currentimage/myimage|image: /newimage/otherimage|' ./myfile
Additionally you can escape each / in the file path like so \/ .
If you can use Awk, it is pretty simple to move on the 12th line using the NR variable which represents the line being processed,
awk 'NR==12{$2="/newimage/otherimage/"}1' myfile > tmp && mv tmp myfile
The part > tmp && mv tmp myfile is equivalent to to the -i option in sed for an indirect way to do modify the file in-place.

How to delete a particular word from a file using shell script

I have a file contains
demo demo1 demo2 demo3 demo12 demo13 demo23
I just want to delete only one line contains demo with out delete other contents
Also I can't specify the line number. Line no: varies alternately
sed -e 's/\<wordtodelete\>//g' file.txt
This will also print the contents of the file to confirm the word is destroyed.
sed -e 's/\<wordtodelete\>//g' -i file.txt
Use -i if you want to overwrite the file in place.
Maybe these will help,
In awk you could use:
awk '{gsub("demo", "");print}' input
1 2 3 12 13 23
Using perl you could use:
perl -p -e 's/demo//g' input
1 2 3 12 13 23
Both the awk and perl use regex to match the word demo, then remove it from the line
sed -i '/\b**demo**\b/d' filename

remove lines based on file input pattern using sed

I have been trying to solve a simple sed line deletion problem.
Looked here and there. It didn't solve my problem.
My problem could simply be achieved by using sed -i'{/^1\|^2\|^3/d;}' infile.txt which deletes lines beginning with 1,2 and 3 from the infile.txt.
But what I want instead is to take the starting matching patterns from a file than manually feeding into the stream editor.
E.g: deletePattern
1
3
2
infile.txt
1 Line here
2 Line here
3 Line here
4 Line here
Desired output
4 Line here
Thank you in advance,
This grep should work:
grep -Fvf deletePattern infile.txt
4 Line here
But this will skip a line if patterns in deletePattern are found anywhere in the 2nd file.
More accurate results can be achieved by using this awk command:
awk 'FILENAME == ARGV[1] && FNR==NR{a[$1];next} !($1 in a)' deletePattern infile.txt
4 Line here
Putting together a quick command substitution combined with a character class will allow a relatively short oneliner:
$ sed -e "/^[$( while read -r ch; do a+=$ch; done <pattern.txt; echo "$a" )]/d" infile.txt
4 Line here
Of course, change the -e to -i for actual in-place substitution.
With GNU sed (for -f -):
sed 's!^[0-9][0-9]*$!/^&[^0-9]/d!' deletePattern | sed -f - infile.txt
The first sed transforms deletePattern into a sed script, then the second sed applies this script.
Try this:
sed '/^[123]/ d' infile.txt

how to use variable in sed '3d' ../log/file2.txt > ../log/file8.txt

I want to delete third line of file named file2. Its running successfully.No Issues with it.
sed '3d' ../log/file2.txt > ../log/file8.txt
Now i want to use variable VAR1 (where VAR1=2).
I wrote the following command
sed "${VAR1+1}d" ../log/file2.txt > ../log/file8.txt
but this command deleting the 2nd line. No error while executing the command.But I am not getting expected output
Please help me
How to use sed in this command to get the correct line to be deleted
Try
sed "$((VAR1+1))d" ../log/file2.txt
Remove a line using variable and awk
var1=4
awk NR!=v v=$var1 ../log/file2.txt > ../log/file8.txt
This removes the 4th line in file2
To make it more robust, use qutes
awk 'NR!=v' v="$var1" file

Shell script - How do I insert data into a separate file at a specific line?

In the following shell script, how do I insert ${today} into a separate existing file index.html at line 4? (Line 1-3 in index.html already has some code. Line 4 is empty. Line 5-EOL has some html code.)
#!/bin/sh
Today=$(date "+%Y.%m.%d-%H.%M.%S")
#insert ${today} into a separate existing file (index.html) in line 4
#<to-do>
I'd use awk for this:
awk 'NR==4 {print strftime("%Y.%m.%d-%H.%M.%S", systime())} 1' file
You can also pass in a variable if you don't want to generate the date string inside awk:
Today=$(date "+%Y.%m.%d-%H.%M.%S")
awk -vtoday=$Today 'NR==4 {print today} 1' file
The sed utility can insert text at specific lines. This might not be the best way to express it; it overwrites anything it finds on line 4.
Today=$(date "+%Y.%m.%d-%H.%M.%S")
sed -i -e "4s/^.*$/$Today/" index.html
The -i argument tells sed to edit in place--it effectively overwrites the input file. I think this option makes sed a better choice than awk for your problem. For testing, remove the -i argument, and it will write to stdout instead.
If you want this to work only if line 4 is a blank line (no whitespace, no characters), use this instead.
Today=$(date "+%Y.%m.%d-%H.%M.%S")
sed -i -e "4s/^$/$Today/" index.html

Resources