Solved grep two strings into one line

I'm a bit of a novice when it comes to grep and can't work out how to do something or even if it's possible...

I'm looking for two strings in a file which would appear on separate lines and I want to output them both on the same line.

How to do that?
 
Something like this?

cat somefile | egrep "string1|string2"

Not on the same line though, but there are ways around that.
 
Given a file named test with the following contents:
Code:
one
two
three
four
… you can call egrep "one|four" test | xargs echo -n; echo to give you:
Code:
one four
(The final ; echo is to end the output with a newline.)
 
What I'm trying to do is create a list of partitions on a number of disks which have freebsd partitions.
I tried this as per your suggestion:-

geom part list | egrep 'Name|freebsd-ufs' | xargs echo -n; echo

It came out in one blob which I would have been able to handle with sed, but I couldn't redirect the output to a file.
 
but I couldn't redirect the output to a file.
That's because you're probably trying to redirect by putting a > at the end of the line.

But that will only catch the final chained command after the last ;, which is just echo.

To do what you want, enclose the entire command in a ( … ) subshell and redirect its output: ( geom part list | egrep 'Name|freebsd-ufs' | xargs echo -n; echo) > file.
 
I was hoping that each match would appear on a line by itself... Tried putting in a '\n' but that didn't work...
 
I was hoping that each match would appear on a line by itself... Tried putting in a '\n' but that didn't work...
\n won't work, you need a literal newline. In this particular case you can insert newlines in front of each i. Name (i = 1, 2, ...):
Code:
geom part list | egrep 'Name|freebsd-ufs' | xargs echo -n | sed 's/\([0-9]*\.\)/\
\1/g'
 
Same idea as aragats
Code:
geom part list | egrep 'Name|freebsd-ufs' | xargs -J {} echo {} |  sed "s/\./\n/g" | grep "freebsd-ufs"  | awk '{print $2,$4}'
 
Ehm, why lots of echos with seds and greps to get rid of something the echos introduce?

geom part list | egrep 'Name|freebsd-ufs' done. Everything on its own line.
 
All I wanted was a simple list of all the freebsd-ufs partitions on all the disks attached locally, one per line.
SirDice has already given you the answer:
gpart show -p | grep 'freebsd-ufs' | tr -s ' ' | cut -d ' ' -f 4?
This thread has got to be the most inefficient way of solving such a simple scripting task. You have kept five people guessing what it is you actually want over the course of half a day. I'm angry at myself for getting interested in the first place.
 
SirDice has already given you the answer:

This thread has got to be the most inefficient way of solving such a simple scripting task. You have kept five people guessing what it is you actually want over the course of half a day. I'm angry at myself for getting interested in the first place.
Everyone is free to email their invoices to /dev/nul
 
The solution of SirDice uses cut, but then you must be carefull with multiple spaces and he needed tr -s ' '.
awk is more powerfull in splitting
 
Apologies to anyone who thought their time was wasted in this little exercise... but maybe something was learnt by one or two people...

I originally made a mistake in using geom part list rather than gpart show -p for compiling my list.
 
Back
Top