sed: remove all but pattern

sed delete pattern
sed regex
sed remove everything before first match
sed remove characters after match
sed remove first character from specific line
sed remove last character from each line
sed exclude pattern
man sed

I have a file build like this:

smthPATTERNsmthPATTERN PATTERNsmthPATTERN smthPATTERNsmthPATTERNPATTERN

The rules are: 'smth' can be anything but 'PATTERN' each 'smth' can be different 'PATTERN' is well definable by regex

Is there sed command to remove all but pattern. The result to above would be:

PATTERNPATTERN PATTERNPATTERN PATTERNPATTERNPATTERN

If not sed than what?

Real example:

14:New: [42.083482 11945.506401 3791.240272], time 0.5000000  
34:New: [70.367422 11591.443051 3686.804348], time 2.4999999  
59:New: [55.001608 11181.824750 3529.426501], time 4.4999999  
84:New: [46.503589 10733.180988 3400.994526], time 6.4999999  
109:New: [48.331394 10438.083437 3321.319648], time 8.5000000  
134:New: [42.924864 10101.462941 3208.426613], time 10.5000000  

PATTRN is a floating point number (regex [0-9]*\.[09]*) I expect result like that:

42.083482 11945.506401 3791.240272 0.5000000  
70.367422 11591.443051 3686.804348 2.4999999  
55.001608 11181.824750 3529.426501 4.4999999  
46.503589 10733.180988 3400.994526 6.4999999  
48.331394 10438.083437 3321.319648 8.5000000  
42.924864 10101.462941 3208.426613 10.5000000  

Using a substitution command with sed you could do:

sed -E  "s/smth-//g" file

sed - how to remove everything but a defined pattern?, If you're not completely tied to sed, this is exactly what grep -o does: grep -o '[0-9​]\{1,3\}%'. How do I use sed to delete all lines that do not contain a specific pattern. For example, I have this file: cat kitty dog giraffe panda lion tiger I want a sed command that, when called, will delete all lines that do not contain the word cat: cat kitty dog

Pure sed regex doesn't easily allow you to say "anything except a pattern". Try

sed 's/[^p]\|p\($\|[^a]\)\|pa\($\|[^t]\)\|pat\($\|[^t]\)\|patt\($\|[^e]\)\|patte\($\|[^r]\)\|patter\($\|[^n]\)//g'

Modern regex such as Perl's lets you easily do

perl -pe 's/(?!pattern).//g'

Replace everything but pattern in a line using sed, 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​  Delete lines in a text file that contain a specific string. Asked 8 years, 9 months ago. Active 6 months ago. Viewed 1.7m times. How would I use sed to delete all lines in a text file that contain a specific string? shell sed text-parsing in-place. improve this question. edited Dec 11 '17 at 23:44. 54 silver badges. 78 bronze badges.

you need to define "-" as field separator and after that act like a normal substitution so sed /s/...

sed - 20 examples to remove / delete characters , This sed command finds the pattern and replaces with another pattern. When the To remove everything except the last n characters in a file: Two `sed` commands are used in this example to remove those lines that contain the pattern ‘php‘ two times. The first `sed` command will replace the second occurrence of ‘php’ in each line by ‘dl‘ and send the output into the second `sed` command as input. The second `sed` command will delete those lines that contain the text, ‘dl‘.

sed: Remove All Except Digits (Numbers) From Input, How do I remove all (text, special characters, white spaces, tabs etc) from my text file (input) except numbers (digits) using sed command? delete all occurrences of the space character, code 0x20. delete all horizontal space, including the horizontal tab character, "\t" delete all whitespace, including newline, " " and others; The right tool for the job. If sed it not a requirement for some hidden reason, better use the right tool for the job.

[SOLVED] how to exclude everything except a pattern with sed, first of all thanks everyone but i forgot to tell why i should use sed the reason is that i want to edit the input file (with -i option of sed) just removing  If you need to delete the lines but create a backup of the original file, then use option -i in the format, -i.bak. This will create a .bak of the original file name. sed -i.bak '/green\|violet/d' colors. To delete all the lines except that contain a specific pattern or keyword; sed '/green/!d' colors This is color green.

USEFUL ONE-LINE SCRIPTS FOR SED (Unix stream editor), 6,\}\)\n/\1 /' # number each line of file, but only print numbers if line is not blank sed '/. assumes that all lines end with CR/LF sed 's/^M$//' # in bash/tcsh, press seds # delete lines matching pattern sed '/pattern/d' # delete ALL blank lines 

Comments
  • in text processing, every character can be important.. if - is not part of your real input, don't put it in sample... finding a solution will depend on knowing nature of pattern and smth, so a more realistic sample would help... if you need a generic solution, use perl to extract only matching patterns and print them (with a delimiter between if needed).. that way it wouldn't matter where smth is placed, how many characters it is made up of, how many times it occurs, etc
  • For your "real example" you would do better to cut off the front and back sections using the square brackets as delimiters. eg: sed "s/^[^[]*\[//; s/].*$//".