Replace everything but pattern in a line using sed
I have a file with multiple lines like this:
Note that
1. There might be variable number occurrences of PATTERN in a line.
2. <> are just placeholders, they do not form part of the pattern.
I need to replace all the <junk> with a nice separator, like a comma.
So I am left with, in the above case
In my case, PATTERN has the pattern "AB:" followed by 9 digits.
I am able to replace the pattern with comma, using the following command -
So I thought replacing everything BUT the pattern should be just one step further - but I am not able to find a solution. Isn't there a way to do this using sed, using the ! operator? I am not able to put my finger on the solution. Any help would be greatly appreciated!
Sometimes you can use some tricks to add extra data to break patterns out like this.. using for example, control characters (needs to be a character not found in the data)... consider:
Where your data is in test2.txt and in the first sed the ^B are literally Ctrl-B characters... this breaks the patterns out onto lines to themselves and replaces newlines with Ctrl-B and stitches things back together afterwords.
You are not per chance only interested in the patterns themselves or do they need to remain on their original lines? Otherwise if your system has grep -o you can do this:
and if you system does not have grep -o, you could do this:
Scrutinizer, I need all the patterns that occurred in a line, and they need to remain on the original lines, with the junk filtered out. I did try out the grep -o option, but then I couldn't tell which line a given pattern corresponds to.
codecaine, the junk follows no particular pattern, so I can't delete the junk using sed.
cjcox, I see what you mean - and how it would work. I was wondering if there would be a more straightforward solution. If I can replace all occurrences of a pattern with a string of my choice, surely I'd expect to be able to replace everything BUT the pattern with a small tweak of the command? Or is it not that straightforward?
Thanks again for all the help, really appreciate it!
'Hi
I'm using the following code to extract the lines(and redirect them to a txt file) after the pattern match. But the output is inclusive of the line with pattern match.
Which option is to be used to exclude the line containing the pattern?
sed -n '/Conn.*User/,$p' > consumers.txt (11 Replies)
my requirement is,
consider a file output
cat output
blah sdjfhjkd jsdfhjksdh
sdfs 23423 sdfsdf sdf"sdfsdf"sdfsdf"""""dsf
hellow there
this doesnt look good
et cetc etc
etcetera
i want to replace a line of line number 4 ("this doesnt look good") with some other line
... (3 Replies)
Sed replace using same pattern repeating multiple times in a line
I have text like below in a file:
I am trying to replace the above line to following
How can I acheive this?
I am able to do it if the occurrence is for 1 time:
But If I try like below
I am getting like this:
I have to... (4 Replies)
Hi How Are you?
I am doing fine!
I need to go now?
I will see you tomorrow!
Basically I need to replace the entire line containing "doing" with a blank line:
I need to the following output:
Hi How Are you?
I need to go now?
I will see you tomorrow!
Thanks in advance.... (1 Reply)
I have a pattern
username:x:32005:32006::/usr/local/user:/bin/bash
I need to match the line containing username and replace /bin/bash with /usr/local/my/bin/noshell
So it becomes
username:x:32005:32006::/usr/local/user:/usr/local/my/bin/noshell (7 Replies)
Hi guys,
got a problem here with sed on the command line.
If i have a string as below:
online xx:wer:xcv: sdf:/asdf/http:https-asdfd
How can i match the pattern "http:" and replace the start of the string to the pattern with null?
I tried the following but it doesn't work:
... (3 Replies)
This is my first post, please be nice. I have tried to google and read different tutorials.
The task at hand is:
Input file input.txt (example)
abc123defhij-E-1234jslo
456ujs-W-abXjklp
From this file the task is to grep the -E- and -W- strings that are unique and write a new file... (5 Replies)
Hello All,
I have a string "CP_STATUS OSSRC_R6_0_Shipment_R1H_CU AOM_901046 R1H_LLSV1_2008031", and I just want to extract LLSV1, but I dont get the expected result when using the sed command below.
# echo "CP_STATUS OSSRC_R6_0_Shipment_R1H_CU AOM_901046 R1H_LLSV1_2008031" | awk '{print... (4 Replies)
I need to replace the line containing "STAGE_DB" with the line
"STAGE_DB $DB # database that contains the table being loaded ($workingDB)"
Here $DB is passed during the runtime.
How can I do this?
Thanks,
Kousikan (2 Replies)