Delete line from file if matches regular expression - sed

I am attempting to delete a line from a text file if it matches a regular expression. To accomplish this I was using sed in an Ubuntu environment combined with regular expressions. I have tried/referenced the following solutions: Sol1, Sol2, Sol3.
My current command is: sed '/[^"]+},/d' test.json with this command I am attempting to match and remove lines like:
{"hello},
{"go penguins!},
{"someone help1),
I am NOT trying to match or remove lines like: "should not match regex"}, Any line that ends with "}, should not be deleted.
I am not tied to using sed so any acceptable answer would work so long as my text file would look something like:
...
{"omg this is amazing"},
{"thanks for your help"},
{"no problem"},
...

How about sed '/\"},/!d' test.json?

It should by sed '/\"},/d' test.json (without !)

Related

Adding a space before each capital letter in a selected set of lines in a yaml file using sed

I want to write a regex for a shell script. It is used to match only this kind of lines in yml file. (lines with the tag summary: Example Summary)
summary: GetMembersSavedSearchesByMemberId
So What I want to do is add a space before each Uppercase letter and output like this
summary: Get Members Saved Searches By Member Id
I tried this regex
matchregex="summary[:][[:space:]].\([A-Z]\)"
replacement="summary: .\1"
sed -e "s/${matchregex}/${replacement}/g"
It is not working. What is the correct way of writing this?
Would you please try the following:
sed -E '/^summary:/ s/([a-z])([A-Z])/\1 \2/g'
Result:
summary: Get Members Saved Searches By Member Id
This might work for you (GNU sed):
sed 's/\B[[:upper:]]/ &/g' file
Globally insert a space inside a word where the following character is uppercase.

Insert specific lines from file before first occurrence of pattern using Sed

I want to insert a range of lines from a file, say something like 210,221r before the first occurrence of a pattern in a bunch of other files.
As I am clearly not a GNU sed expert, I cannot figure how to do this.
I tried
sed '0,/pattern/{210,221r file
}' bunch_of_files
But apparently file is read from line 210 to EOF.
Try this:
sed -r 's/(FIND_ME)/PUT_BEFORE\1/' test.text
-r enables extendend regular expressions
the string you are looking for ("FIND_ME") is inside parentheses, which creates a capture group
\1 puts the captured text into the replacement.
About your second question: You can read the replacement from a file like this*:
sed -r 's/(FIND_ME)/`cat REPLACEMENT.TXT`\1/' test.text
If replace special characters inside REPLACEMENT.TXT beforehand with sed you are golden.
*= this depends on your terminal emulator. It works in bash.
In https://stackoverflow.com/a/11246712/4328188 CodeGnome gave some "sed black magic" :
In order to insert text before a pattern, you need to swap the pattern space into the hold space before reading in the file. For example:
sed '/pattern/ {
h
r file
g
N
}' in
However, to read specific lines from file, one may have to use a two-calls solution similar to dummy's answer. I'd enjoy knowing of a one-call solution if it is possible though.

how to replace a line based on variable match using sed

Is it possible to use sed to replace some text based on the matching of a condition at the beginning of the text... For example, for the following file, I only want to replace the word 'guest' to 'unwanted-guest' only for the line that begins with the pattern '
541ce0a0c3b4f843ec000001' which is a variable.
541ce0a0c3b4f843ec000001:x:1000:1000:OpenShift guest:/var/lib/openshift/541ce0a0c3b4f843ec000001:/usr/bin/oo-trap-user
541ce468c3b4f843ec000029:x:1001:1001:OpenShift guest:/var/lib/openshift/541ce468c3b4f843ec000029:/usr/bin/oo-trap-user
Try:
sed '/^541ce0a0c3b4f843ec000001/ s/guest/unwanted-guest/'
We have placed a condition in front of the usual sed substitute command. The condition is:
/^541ce0a0c3b4f843ec000001/
This condition limits sed to considering only lines that start with 541ce0a0c3b4f843ec000001 (The caret ^ means must-be-at-the-beginning-of-a-line). The substitute command is:
s/guest/unwanted-guest/
This replaces the first occurrence of guest on the line with unwanted-guest.
Example
Applying this command to your sample input (placed in a file named file):
$ sed '/^541ce0a0c3b4f843ec000001/ s/guest/unwanted-guest/' file
541ce0a0c3b4f843ec000001:x:1000:1000:OpenShift unwanted-guest:/var/lib/openshift/541ce0a0c3b4f843ec000001:/usr/bin/oo-trap-user
541ce468c3b4f843ec000029:x:1001:1001:OpenShift guest:/var/lib/openshift/541ce468c3b4f843ec000029:/usr/bin/oo-trap-user
Using with a variable
$ id=541ce0a0c3b4f843ec000001
$ sed "/^$id/ s/guest/unwanted-guest/" file
541ce0a0c3b4f843ec000001:x:1000:1000:OpenShift unwanted-guest:/var/lib/openshift/541ce0a0c3b4f843ec000001:/usr/bin/oo-trap-user
541ce468c3b4f843ec000029:x:1001:1001:OpenShift guest:/var/lib/openshift/541ce468c3b4f843ec000029:/usr/bin/oo-trap-user

sed replace a word at a line which begins with a specific pattern using

How can I replace a word at a line which begins with a specific pattern on FreeBSD?
Consider the following file contents:
this is to test
that was for test
I want to replace "test" at the line which begins with "this".
In order to perform a replacement for lines starting with this, say:
$ sed '/^this/ s/test/something/' inputfile
this is to something
that was for test
This would replace the word test with something on lines starting with this.
If you want to replace all instances of test on the matching lines, supply the g option to sed:
sed '/^this/ s/test/something/g' inputfile
To make the changes in-place, use the below command:
sed -i '/^this/ s/test/something/g' inputfile;

Replace 3 lines with another line SED Syntax

This is a simple question, I'm not sure if i'm able to do this with sed/awk
How can I make sed search for these 3 lines and replace with a line with a determined string?
<Blarg>
<Bllarg>
<Blllarg>
replace with
<test>
I tried with sed "s/<Blarg>\n<Bllarg>\n<Blllarg>/<test>/g" But it just don't seem to find these lines. Probably something with my break line character (?) \n. Am I missing something?
Because sed usually handles only one line at a time, your pattern will never match. Try this:
sed '1N;$!N;s/<Blarg>\n<Bllarg>\n<Blllarg>/<test>/;P;D' filename
This might work for you:
sed '/<Blarg>/ {N;N;s/<Blarg>\n<Bllarg>\n<Blllarg>/<test>/}' <filename>
It works as follows:
Search the file till <Blarg> is found
Then append the two following lines to the current pattern space using N;N;
Check if the current pattern space matches <Blarg>\n<Bllarg>\n<Blllarg>
If so, then substitute it with <test>
You can use range addresses with regular expressions an the c command, which does exactly what you are asking for:
sed '/<Blarg>/,/<Blllarg>/c<test>' filename