$ echo 'mike(hussey) AND mike(donald) AND mike(ryan) AND mike(johnson)' | perl -ne '@surnames=$_=~/\(([^)]+)\)/g;print join"\n",@surnames' -
hussey
donald
ryan
johnson
To get the other fields (and print the values from each input line on a single output line) try:
awk -F '[()]' '
{ for(i = 2; i < NF; i += 2)
printf("%s%s", $i, i == NF - 1 ? "\n" : " ")
}' filename
Note that (even though it doesn't matter with the sample input you provided) you don't want the pipe symbol in the list of characters to be treated as field separators in your -F option option-argument.
$ echo 'mike(hussey) AND mike(donald) AND mike(ryan) AND mike(johnson)' | sed '/\(^[^(]*(\|)[^(]*(\|)[^(]*\)/s// /g;s/ //'
hussey donald ryan johnson
Another awk solution:
$ echo 'mike(hussey) AND mike(donald) AND mike(ryan) AND mike(johnson)' | awk -F'[()]' '{while((i+=2)<NF) {x=x T $i;T=" "} print x}'
hussey donald ryan johnson
@krishmaths - your answer suits my requirement perfectly @skrynesaver- perl also works but it shows output in separate line. @Don cragon - It works only when number of columns is known
---------- Post updated at 03:53 PM ---------- Previous update was at 03:52 PM ----------
I assume by @Don cragon you mean me. I don't understand your comment. In the tests I ran, it works on any input line where there are one or more matched pairs of opening and closing parentheses. For example, with the input:
mike(hussey) AND mike(donald) AND mike(ryan) AND mike(johnson)
(1) (2) (3) (4) (5) (6) (7) (8) (9) (10)(11)
nothing to print from this line
lots of junk (real data) more junk
it prints:
hussey donald ryan johnson
1 2 3 4 5 6 7 8 9 10 11
real data
which seems to match what you requested. You didn't say what should happen when there are no parentheses.