If a,b,c,name,col1,col2,col3,
are the second fields of each line in the original, then you could do the test against name
at the same time you pick out these (this still leaves the annoying final comma):
$ awk -vORS=, 'p {print $2}; $2 == "name" {p=1} ' input; echo
col1,col2,col3,
So, starting with what you had (awk 'ORS="," {print $2}'
), we add a test variable p
that tells if name
has already been seen. We print the second field only if p
has been set to a true value earlier, and set it to true if the second field happens to be name
. With the tests in this order, the name
column itself is not printed. We could also ignore empty lines in input by changing p {print $2}
to $0 && p {print $2}
, that is, make a truthy (nonempty) input line a condition for the print, along with p
.
I assumed here that the original input looks like this:
x a
x b
x c
x name
x col1
x col2
x col3
Alternatively, starting from the comma-separated list a,b,c,name,somename,othername,col3,
:
$ echo 'a,b,c,name,somename,othername,col3,' |
sed -e 's/.*,name,//' -e s'/,$//'
somename,othername,col3
Note the commas on both sides of ,name,
in the pattern, they keep the greedy .*
from catching the later names that end in ...name
.
.*
eats everything up to the last occurrence ofname
, so if the input isblah,name,othername,bleh
,othername
is gone too, and onlybleh
is left.