sed: filter string subset from lines matching regexp

Mark :

I have a file of the following format:

abc: A B C D E
abc: 1 2 3 4 5 
def  D E F G H
def: 10 11 12 23 99
...

That is a first line with strings after ':' is a header for the next line with numbers. I'd like to use sed to extract only a line starting with PATTERN string with numbers in the line.

Number of numbers in a line is variable, but assume that I know exactly how many I'm expecting, so I tried this command:

% sed 's/^abc: \([0-9]+ [0-9]+ [0-9]+\)$/\1/g' < file.txt

But it dumps all entries from the file. What am I doing wrong?

Quasímodo :
  1. sed does substitutions and prints each line, whether a substitution happens or not.

  2. Your regular expression is wrong. It would match only three numbers separated by spaces if extended regex flag was given (-E). Without it, not even that, because the + sign will be interpreted literally.

  3. The best here is to use addresses and only print lines that have a match:

sed -nE '/^abc: [0-9]+ [0-9]+ [0-9]+ [0-9]+ [0-9]+$/p' < file.txt

or better,

sed -nE '/^abc:( [0-9]+){5}$/p' < file.txt

The -n flag disables the "print all lines" behavior of sed described in (1). Only the lines that reach the p command will be printed.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=390350&siteId=1