Prev: awk and getline
Next: awk "not contain"
From: jplee3 on 17 May 2010 21:05 Hi all, I was wondering if there's a way to match more than one string in a line and then print/output the strings to the screen or a file, etc. I'm trying to achieve something similar to "grep -o "matching_string" file123" but with more than one string, and where each matching string is printed subsequently in a line. So I would want to match "matching_string1" and "matching_string2" from one line and print both those matches on a single line. Effectively, cutting out the text I don't want and printing out only what I want from a line. Is this even possible with grep or awk?
From: Jon LaBadie on 17 May 2010 23:35 jplee3 wrote: > Hi all, > > I was wondering if there's a way to match more than one string in a > line and then print/output the strings to the screen or a file, etc. > > I'm trying to achieve something similar to "grep -o "matching_string" > file123" but with more than one string, and where each matching string > is printed subsequently in a line. > > So I would want to match "matching_string1" and "matching_string2" > from one line and print both those matches on a single line. > Effectively, cutting out the text I don't want and printing out only > what I want from a line. > > Is this even possible with grep or awk? sed -n 's/.*\(matching_string1\).*\(matching_string2\).*/\1\2/p' input_file
From: Ed Morton on 18 May 2010 00:31 On 5/17/2010 8:05 PM, jplee3 wrote: > Hi all, > > I was wondering if there's a way to match more than one string in a > line and then print/output the strings to the screen or a file, etc. Do you mean find a string and print that string or do you mean find a regular expression and print the string that matches it? > I'm trying to achieve something similar to "grep -o "matching_string" > file123" but with more than one string, and where each matching string > is printed subsequently in a line. > > So I would want to match "matching_string1" and "matching_string2" > from one line and print both those matches on a single line. > Effectively, cutting out the text I don't want and printing out only > what I want from a line. > > Is this even possible with grep or awk? Yes, but provide some small sample input and the expected output given that input so we're not guessing too much. Ed.
From: jplee3 on 18 May 2010 13:00 On May 17, 9:31 pm, Ed Morton <mortons...(a)gmail.com> wrote: > On 5/17/2010 8:05 PM, jplee3 wrote: > > > Hi all, > > > I was wondering if there's a way to match more than one string in a > > line and then print/output the strings to the screen or a file, etc. > > Do you mean find a string and print that string or do you mean find a regular > expression and print the string that matches it? > > > I'm trying to achieve something similar to "grep -o "matching_string" > > file123" but with more than one string, and where each matching string > > is printed subsequently in a line. > > > So I would want to match "matching_string1" and "matching_string2" > > from one line and print both those matches on a single line. > > Effectively, cutting out the text I don't want and printing out only > > what I want from a line. > > > Is this even possible with grep or awk? > > Yes, but provide some small sample input and the expected output given that > input so we're not guessing too much. > > Ed. Sorry for the confusion. It is in fact a regex string that I would be looking for. For instance: say the original line is: "<ID>=apache_server1_12345678, <ID2>=blahblahblah, <ID3>=FUBAR=apache_server2_12345678| IPADDRESS=192.168.1.1|MSG=hello" there are hundreds of lines like these... I want to extract so that I will get the following result from each line (a list of IPs and hostnames): "192.168.1.1 apache_server1_12345678" Thanks guys!
From: jplee3 on 18 May 2010 13:12
On May 18, 10:00 am, jplee3 <jpl...(a)gmail.com> wrote: > On May 17, 9:31 pm, Ed Morton <mortons...(a)gmail.com> wrote: > > > > > On 5/17/2010 8:05 PM, jplee3 wrote: > > > > Hi all, > > > > I was wondering if there's a way to match more than one string in a > > > line and then print/output the strings to the screen or a file, etc. > > > Do you mean find a string and print that string or do you mean find a regular > > expression and print the string that matches it? > > > > I'm trying to achieve something similar to "grep -o "matching_string" > > > file123" but with more than one string, and where each matching string > > > is printed subsequently in a line. > > > > So I would want to match "matching_string1" and "matching_string2" > > > from one line and print both those matches on a single line. > > > Effectively, cutting out the text I don't want and printing out only > > > what I want from a line. > > > > Is this even possible with grep or awk? > > > Yes, but provide some small sample input and the expected output given that > > input so we're not guessing too much. > > > Ed. > > Sorry for the confusion. It is in fact a regex string that I would be > looking for. > > For instance: > > say the original line is: "<ID>=apache_server1_12345678, > <ID2>=blahblahblah, <ID3>=FUBAR=apache_server2_12345678| > IPADDRESS=192.168.1.1|MSG=hello" > > there are hundreds of lines like these... > > I want to extract so that I will get the following result from each > line (a list of IPs and hostnames): "192.168.1.1 > apache_server1_12345678" > > Thanks guys! Sorry, the formatting is screwed up on this one: I would want the list to be in this format "192.168.1.1 server1" - where there's a space or tab delimiter (not a carriage return/newline). Also, I'd want to extract "server1" from "apache_server1_12345678" Jon LaBadie's (thanks Jon!) sed command partially worked, but there's no delimiter between the server name and IP. Also, I'm not 100% sure what the regex would look like. For the IP address, I was using "[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}" for "grep -o" but not sure if that will differ for what I'm trying to do. I'm not sure what the regex for extracting the server hostname would be either. Thanks again! |