Solved grep two strings into one line

balanga

Son of Beastie

Reaction score: 217
Messages: 4,024

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?
 

SirDice

Administrator
Staff member
Administrator
Moderator

Reaction score: 11,564
Messages: 37,880

Something like this?

cat somefile | egrep "string1|string2"

Not on the same line though, but there are ways around that.
 

mtu

Active Member

Reaction score: 103
Messages: 157

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.)
 
OP
B

balanga

Son of Beastie

Reaction score: 217
Messages: 4,024

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.
 

mtu

Active Member

Reaction score: 103
Messages: 157

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.
 
OP
B

balanga

Son of Beastie

Reaction score: 217
Messages: 4,024

I was hoping that each match would appear on a line by itself... Tried putting in a '\n' but that didn't work...
 

aragats

Daemon

Reaction score: 689
Messages: 1,602

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'
 

Alain De Vos

Daemon

Reaction score: 542
Messages: 1,881

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}'
 

SirDice

Administrator
Staff member
Administrator
Moderator

Reaction score: 11,564
Messages: 37,880

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.
 

mtu

Active Member

Reaction score: 103
Messages: 157

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.
 

Alain De Vos

Daemon

Reaction score: 542
Messages: 1,881

It's not soo bad. I learned something new by how other people solve the same problem.
 
OP
B

balanga

Son of Beastie

Reaction score: 217
Messages: 4,024

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
 

Alain De Vos

Daemon

Reaction score: 542
Messages: 1,881

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
 
OP
B

balanga

Son of Beastie

Reaction score: 217
Messages: 4,024

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.
 
Top