sed or Grep Parsing


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers sed or Grep Parsing
# 1  
Old 05-26-2014
sed or Grep Parsing

I would like to parse two strings from lines in a file only when both strings appear on the same line. For example, if I have the following line:

Code:
string1 string2 string3 string4 string5 string6 string7 string8 string9

I would like the output to be:

Code:
string2: string7

Can someone give me some advice? It may be possible that either string2 or string7 appear on a line, but not both and I don't want to parse those lines. I also don't want the text that appears between string 2 and string 7.

Thanks!

Last edited by Don Cragun; 05-26-2014 at 08:02 PM.. Reason: Add CODE tags.
# 2  
Old 05-26-2014
Is this a homework assignment? (If so, you must use the Homework & Coursework Questions forum and follow the special submission requirements for that forum.)

If not, I could interpret your request several ways.

Are you trying to print the 2nd and 7th "word" on a line for lines that contain 7 or more words?

Are you looking for lines that contain the literal string "string2" followed by the literal string "string7"?

Are you looking for lines that contain a word that is the literal string "string2" followed by a word that is the literal string "string7"?

Are you looking for lines that contain the literal string "string2" as the 2nd word on the line and the literal string "string7" as the 7th word on the line?

Is it one of the above but with words or strings matching two basic regular expression instead of two literal strings?

Please give us a more detailed description of what you're trying to do.
# 3  
Old 05-26-2014
Parsing

No this is not a homework assignment, this is work related. I am trying parse literal strings, not words, I just tied to simplify the example to make it easier to respond to. The strings are not necessarily the second or seventh word, but string2 always appears before string7. In fact, it is xml that I am trying to parse.

Thanks.
# 4  
Old 05-26-2014
The following would seem to do what you're asking for as long as string2 and string7 don't contain any <slash> (/) characters or characters with special meaning in a BRE (<period> (.), <left-square-bracket> ([), <backslash> (\), <asterisk> (*), <circumflex> (^), and <dollar-sign> ($)):
Code:
sed -n 's/.*string2.*string7.*/string2: string7/p' file

# 5  
Old 05-26-2014
Thanks Don. Actually string 2 looks like this:

Code:
(Interaction (username))

****where username can be any textual value

and string 7 is in the form:

Code:
TotalQuantity value="1"

****where 1 can be any numerical value

Andrew

Last edited by Don Cragun; 05-26-2014 at 09:53 PM.. Reason: Change COLOR tags to CODE tags.
# 6  
Old 05-26-2014
By definition, "when username can be any textual value" and "where 1 can be any numerical value" do not describe literal strings. The following should do what you have described now if the version of sed you're using conforms to the standards:
Code:
sed -n 's/.*(Interaction (\([^)]*\))).*TotalQuantity value="\([0-9]*\)".*/\1: \2/p' file

If file contains:
Code:
<tag>(Interaction (user1))</tag><tag2>TotalQuantity value="121"</tag2>
<tag>Interaction (user2)</tag><tag2>TotalQuantity value="122"</tag2>
<tag>(Interaction (user3))</tag><tag2>TotalQuantity value="123x"</tag2>
<tag>(Interaction (user4))</tag><tag2>TotalQuantity value="124"</tag2>

the script above produces:
Code:
user1: 121
user4: 124

If you're using a GNU utilities version of sed and the above doesn't work, we can convert the BRE to an ERE and use awk instead of sed.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Parsing via sed issue

sorry I messed up the last post with too many mistakes and corrections so I closed it and opening a new one which should be clear to everyone .my apologies to the admins. I am using sun solaris and Linux , what I want is SED to print any string (or output it to a file preferably) that does... (2 Replies)
Discussion started by: boncuk
2 Replies

2. Shell Programming and Scripting

parsing string into grep

I'm using sh under AIX 5.3 and after grep'ing, awk'ing, splitting a file, I end up with two variables as: 0123456 789Can these values be somehow passed into a grep as two different variables using xargs or something? What I want to do is this (the echo is just for effect here): echo 0123456 789... (2 Replies)
Discussion started by: say170
2 Replies

3. UNIX for Dummies Questions & Answers

awk/grep or parsing in python code

Hello, I am writing a python code. The output of the python code needs a little bit of parsing. From the output of python code, which has a lot of redundant data, I need to cut only those words or numbers which end with &. for example: if the output is-- "This is an example of tgbn123& what i... (0 Replies)
Discussion started by: Screech_you
0 Replies

4. Shell Programming and Scripting

Sed special parsing

What is the shortest & right way to remove the string "" with a sed statement ? echo 'whateverwhatever' | sed ........ ? :) (2 Replies)
Discussion started by: ctsgnb
2 Replies

5. Shell Programming and Scripting

Parsing out access.log with awk and grep

In part of my script I use awk to pull out the urls. awk '{print $8}' then I take them and send them to grep.` Some of them are straight .com/ or .org or whatever (address bar entries), while others are locations of images, js, etc. I'm trying to only pull any line that ends with .com/... (11 Replies)
Discussion started by: druisgod
11 Replies

6. Shell Programming and Scripting

sed (parsing value)

All, Can somebody provide me with some sed expertise on how to parse the following line. 27-MAR-2011 10:28:01 * (CONNECT_DATA=(SID=dmart)(CID=(PROGRAM=sqlplus)(HOST=mtasnprod1)(USER=mtasnord))) * (ADDRESS=(PROTOCOL=tcp)(HOST=10.197.7.47)(PORT=54881)) * establish * dmart * 0 I would like... (3 Replies)
Discussion started by: BeefStu
3 Replies

7. Shell Programming and Scripting

Parsing cron with sed

Hello I want to convert my cron list into a csv Can you please help me with sed ? eg: Convert #06,21,36,51 * * 1,2 * (. ~/.profile ; timex /some/path/script -30 -15) >> /some/path/logfile2 2>&1 * * * * * (. ~/.profile ; timex /some/path/script2) > /some/path/logfile2 To:... (1 Reply)
Discussion started by: drbiloukos
1 Replies

8. Shell Programming and Scripting

Parsing with awk or sed

I want to delete corrupt records from a file through awk or sed. Can anyone help me with this Thanks Striker Change subject to a descriptive one, ty. (1 Reply)
Discussion started by: Rahul_us
1 Replies

9. Shell Programming and Scripting

Sed parsing error

I'm having a problem with a sed script. A programmer needs to change columns 942,943,944 to blank spaces only where it has the number 999 in every line. I didn't have a copy of the data file to test with originally so made my own up with a bunch of x's and put 999 in columns 5-7. The sed... (1 Reply)
Discussion started by: gravy26
1 Replies

10. Shell Programming and Scripting

awk sed parsing

hi , i would like to parse some file with the fallowing data : data data data "unwanted data" data data "unwanted data" data data data data #unwanted data. what i want it to have any coments between "" and after # to be erased using awk or/and sed. has anyone an idea? thanks. (3 Replies)
Discussion started by: Darsh
3 Replies
Login or Register to Ask a Question