This question already has answers here:
Replace All Lines That Do Not Contain Matched String
(4 answers)
Closed 3 years ago.
I have a problem with making sed command, which gonna change lines, where =sometext= occurs and change it to another pattern, but will not do it when https occcurs in that line. I have no idea how I should change this command:sed -i 's/=\([^=]*\)=/{{\1}}/g'
You'll want to read the sed manual about matching lines: https://www.gnu.org/software/sed/manual/sed.html chapter 4:
The following command replaces the word ‘hello’ with ‘world’ only in lines not containing the word ‘apple’:
sed '/apple/!s/hello/world/' input.txt > output.txt
Use multiple blocks, e.g.:
sed '/=sometext=/ { /https/b; s/.../.../; }'
Related
This question already has answers here:
sed multiline delete with pattern
(2 answers)
Closed 2 years ago.
I need to remove strings from a text file that matches a REGEX pattern, using regex101 my pattern match works fine, but when I execute using sed, nothing gets deleted and for some reason the regex is not working:
https://regex101.com/r/oLNrDB/1/
I need to remove all occurrences of all text including newlines between the following 2 strings:
DELIMITER ;;
some text with newlines
DELIMITER ;
The sed command I am using is:
sed '/DELIMITER ;;[\S\s]*?DELIMITER ;/d' myfile.sql;
but the output is identical to the input file, what am I doing wrong ?
The problem here is that sed reads files line-by-line and applies the pattern to each line separately. In your case, this means that the one pattern can't match both the starting and finishing delimiter because no one line contains them both.
The sed way of doing this is to use a range with the delete command, /start pattern/,/end pattern/d, which means delete all lines between the start pattern and end pattern inclusive. For example
$ cat foo.txt
some text before
DELIMITER ;;
some text with newlines
DELIMITER ;
some text after
$ sed '/DELIMITER ;;/,/DELIMITER ;/d' foo.txt
some text before
some text after
This question already has answers here:
Grep for literal strings
(6 answers)
Closed 4 years ago.
I want to copy over all lines in a file (file1.txt) containing Cmd:[41] over to another file (file2.txt)
awk '/Cmd:[41]/' file1.txt > file2.txt
This command doesn't seem to work. The file2.txt is of size 0. However there are lines in file1.txt that contains Cmd:[41]
Is there some specific awk escape character that I should be using. The problem is with [41] part. The other part of the search string seems to work fine.
You can just change your command in the following way and it will work:
awk '/Cmd:\[41\]/' file1.txt > file2.txt
Explanations:
'/Cmd:[41]/' will match lines that contain: Cmd:4 or Cmd:1 but will not match lines that contain literally Cmd:[41] as [...] are used in regex to define a character range, or a list of characters that can be matched therefore you need to escape them by adding a \ before them.
This question already has answers here:
Why doesn't `\d` work in regular expressions in sed? [duplicate]
(3 answers)
Closed 5 years ago.
I am trying to replace some character strings using sed in batch console. My input consists in a file with lines like these:
$ head qiimetax_sorted.txt
A61579.1.1437
D_0__Bacteria;D_1__Thermotogae;D_2__Thermotogae;D_3__Thermotogales;
D_4__Fervidobacteriaceae;D_5__Fervidobacterium;Ambiguous_taxa;D_7__;
D_8__;D_9__;D_10__;D_11__;D_12__;D_13__;D_14__
AAAA02020712.626.2096
D_0__Bacteria;D_1__Proteobacteria;D_2__Alphaproteobacteria;D_3__Rhizobiales;
D_4__Bradyrhizobiaceae;D_5__uncultured;D_6__Oryza sativa
Indica Group (long-grained rice);D_7__;D_8__;D_9__;D_10__;D_11__;D_12__;
D_13__;D_14__
Now I'm trying to erase the 'D_number__' string before the names with this sed command and it is not replacing anything:
sed -r 's/D_\d+__//g' qiimetax_sorted.txt > qiimesed.txt
Any idea of which is the problem?
Thanks!
Your regex syntax is perl like.
So if you want to keep it :
perl -pe 's/D_\d+__//g' qiimetax_sorted.txt > qiimesed.tx
or with sed :
sed -r 's/D_[0-9]+__//g' qiimetax_sorted.txt > qiimesed.tx
This question already has answers here:
sed search and replace strings containing / [duplicate]
(2 answers)
Closed 7 years ago.
I'm trying to replace a line in a csv file that includes '/' characters in one of its columns. Is there a way to replace it with sed? I'm using two variables: one to store the line to replace and another one for the string that will replace it.
sed -i "/s/${CURRENTLINE}/${NEWLINE}/g"
example of the line format:
907;name;2015;4444;DOC;44;user;06/03/2015
Thanks in advance!
Your sed command starts with s and you can use an alternate reges delimiter:
sed -i "s~${CURRENTLINE}~${NEWLINE}~g" file.csv
This question already has answers here:
"sed" special characters handling
(3 answers)
Is it possible to escape regex metacharacters reliably with sed
(4 answers)
Escape a string for a sed replace pattern
(17 answers)
Closed 5 years ago.
I have a file called ethernet containing multiple lines. I have saved one of these lines as a variable called old_line. The contents of this variable looks like this:
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="2r:11:89:89:9g:ah", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="eth*", NAME="eth1"
I have created a second variable called new_line that is similar to old_line but with some modifications in the text.
I want to substitute the contents of old_line with the contents of new_line using sed. So far I have the following, but it doesn't work:
sed -i "s/${old_line}/${new_line}/g" ethernet
You need to escape your oldline so that it contains no regex special characters, luckily this can be done with sed.
old_line=$(echo "${old_line}" | sed -e 's/[]$.*[\^]/\\&/g' )
sed -i -e "s/${old_line}/${new_line}/g" ethernet
Since ${old_line} contains many regex special metacharacters like *, ? etc therefore your sed is failing.
Use this awk command instead that uses no regex:
awk -v old="$old_line" -v new="$new_line" 'p=index($0, old) {
print substr($0, 1, p-1) new substr($0, p+length(old)) }' ethernet