grep a string in a line using sed


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers grep a string in a line using sed
# 1  
Old 02-02-2007
grep a string in a line using sed

Hi,

I'm trying to grep a string in a line using sed.

My original data looks like this:

MRTG$ grep -i "System" $file

<H1>Traffic Analysis for 15 -- sERITHC3602.t-mobile.co.uk</H1> <TABLE> <TR><TD>System:</TD> <TD>sERITHC3602 in </TD></TR> <TR><TD>Maintainer:</TD> <TD></TD></TR> <TR><TD>Description:</TD><TD>ATM2/0.4-aal5 layer VC link to sCRDONC3601 ATM2/0.1 </TD></TR> <TR><TD>ifType:</TD> <TD>aal5 (49)</TD></TR> <TR><TD>ifName:</TD> <TD></TD></TR> <TR><TD>Max Speed:</TD> <TD>6144.0 kbits/s</TD></TR> <TR><TD>Ip:</TD> <TD>172.30.0.49 (serithc3602)</TD></TR> </TABLE><BR>


I think the result of grep is in one line and I'm trying to put some needed data in one line as well (separated using commas):

System,Description,ifType,ifName,Max Speed,Ip

sERITHC3602,ATM2/0.4-aal5 layer VC link to sCRDONC3601 ATM2/0.1,aal5 (49),,6144.0 kbits/s,172.30.0.49 (serithc3602)

Just to grep the System data (sERITHC3602), I used this sed command:

MRTG$ grep -i "System" $file | sed 's/.*<System>\(.*\)<\/Maintainer>.*/\1/'

I used System and Maintainer strings because I know the data I need is between these two but I can still see the whole line after using sed.

Hope somebody can see my syntax error.

Cheers! Smilie
# 2  
Old 02-02-2007
grep portion of a string using sed

Hi,

I'm trying to grep a string in a line using sed.

My original data looks like this:

MRTG$ grep -i "System" $file

<H1>Traffic Analysis for 15 -- sERITHC3602.t-mobile.co.uk</H1> <TABLE> <TR><TD>System:</TD> <TD>sERITHC3602 in </TD></TR> <TR><TD>Maintainer:</TD> <TD></TD></TR> <TR><TD>Description:</TD><TD>ATM2/0.4-aal5 layer VC link to sCRDONC3601 ATM2/0.1 </TD></TR> <TR><TD>ifType:</TD> <TD>aal5 (49)</TD></TR> <TR><TD>ifName:</TD> <TD></TD></TR> <TR><TD>Max Speed:</TD> <TD>6144.0 kbits/s</TD></TR> <TR><TD>Ip:</TD> <TD>172.30.0.49 (serithc3602)</TD></TR> </TABLE><BR>


I think the result of grep is in one line and I'm trying to put some needed data in one line as well (separated using commas):

System,Description,ifType,ifName,Max Speed,Ip

sERITHC3602,ATM2/0.4-aal5 layer VC link to sCRDONC3601 ATM2/0.1,aal5 (49),,6144.0 kbits/s,172.30.0.49 (serithc3602)

Just to grep the System data (sERITHC3602), I used this sed command:

MRTG$ grep -i "System" $file | sed 's/.*<System>\(.*\)<\/Maintainer>.*/\1/'

I used System and Maintainer strings because I know the data I need is between these two but I can still see the whole line after using sed.

Hope somebody can see my syntax error.

Cheers! Smilie
# 3  
Old 02-02-2007
Code:
<H1>Traffic Analysis for 15 -- sERITHC3602.t-mobile.co.uk</H1> <TABLE> <TR><TD>System:</TD> <TD>sERITHC3602 in </TD></TR> <TR><TD>Maintainer:</TD> <TD></TD></TR> <TR><TD>Description:</TD><TD>ATM2/0.4-aal5 layer VC link to sCRDONC3601 ATM2/0.1 </TD></TR> <TR><TD>ifType:</TD> <TD>aal5 (49)</TD></TR> <TR><TD>ifName:</TD> <TD></TD></TR> <TR><TD>Max Speed:</TD> <TD>6144.0 kbits/s</TD></TR> <TR><TD>Ip:</TD> <TD>172.30.0.49 (serithc3602)</TD></TR> </TABLE><BR>

System and Maintainer is not surrounded by angle brackets.
Try this
Code:
grep -i "System" $file | sed 's/.*System\(.*\)Maintainer.*/\1/'

# 4  
Old 02-02-2007
grep -i "System" $file | sed -e "s/^.*<TABLE>//" -e "s/<\/TABLE>.*$//" -e "s?[ ]*<TR><TD>[^<>]*:</TD>[ ]*<TD>\([^<>]*\)</TD></TR>?\1,?g" -e "s/,[ ]*$//"

Output:
sERITHC3602 in ,,ATM2/0.4-aal5 layer VC link to sCRDONC3601 ATM2/0.1 ,aal5 (49),,6144.0 kbits/s,172.30.0.49 (serithc3602)
# 5  
Old 02-06-2007
Hi anbu23 and sb008,

Sorry I only replied just now ... both your suggestions worked! thank you very much ...

sb008's one-liner script definitely reduced the original script I created ...

Best regards,
viadisky
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sed/grep: check if line exists, if not add line?

Hello, I'm trying to figure out how to speed up the following as I want to use multiple commands to search thousands of files. is there a way to speed things up? Example I want to search a bunch of files for a specific line, if this line already exists do nothing, if it doesn't exist add it... (4 Replies)
Discussion started by: f77hack
4 Replies

2. Shell Programming and Scripting

Using sed to get text between a string and next line after another string

Hi folks! I'm trying to get a part of a text inside a text file (sudoers actually) but I'm having trouble. Here is an example of a text: Cmnd_Alias DUMMY = /bin/ls /bin/car /usr/bin/whatever Cmnd_Alias TARGET = test test test test test \ ... (6 Replies)
Discussion started by: leandrorius
6 Replies

3. Shell Programming and Scripting

sed command to grep multiple pattern present in single line and delete that line

here is what i want to achieve.. i have a file with below contents cat fileName blah blah blah . .DROP this REJECT that . --sport 7800 -j REJECT --reject-with icmp-port-unreachable --dport 7800 -j REJECT --reject-with icmp-port-unreachable . . . more blah blah blah --dport 3306... (14 Replies)
Discussion started by: vivek d r
14 Replies

4. Shell Programming and Scripting

Grep a string from input file and delete next three lines including the line contains string in xml

Hi, 1_strings file contains $ cat 1_strings /home/$USER/Src /home/Valid /home/Review$ cat myxml <projected value="some string" path="/home/$USER/Src"> <input 1/> <estimate value/> <somestring/> </projected> <few more lines > <projected value="some string" path="/home/$USER/check">... (4 Replies)
Discussion started by: greet_sed
4 Replies

5. Shell Programming and Scripting

Grep a string and write a value to next line of found string

Hi, I have two variables x and y. i need to find a particular string in a file, a workflow name and then insert the values of x and y into the next lines of the workflow name. basically it is like as below wf_xxxxxx $$a= $$b= $$c= figo $$d=bentley i need to grep the 'wf_xxxx' and then... (6 Replies)
Discussion started by: angel12345
6 Replies

6. Shell Programming and Scripting

grep on string and printing line after until another string has been found

Hello Everyone, I just started scripting this week. I have no background in programming or scripting. I'm working on a script to grep for a variable in a log file Heres what the log file looks like. The x's are all random clutter xxxxxxxxxxxxxxxxxxxxx START: xxxxxxxxxxxx... (7 Replies)
Discussion started by: rxc23816
7 Replies

7. Shell Programming and Scripting

String: Grep / SED for multy line search

Hi, At first I want to please you to provide the solution with grep/sed if possible. :cool: File looks like: wished result: so I want in a new file BLUE@@RED string from first line like: grep "/folder_start" cs_src > tmp1 string from second line: grep "/main" cs_src... (14 Replies)
Discussion started by: unknown7
14 Replies

8. Shell Programming and Scripting

GREP/SED - get string in a line

Hi, I simply try to get a string in a line but I do smth. wrong. Hopfefully you can help me ;) tried smth like: ggrep -Eo " /folder1/folder2/folder3/* end" get_info_file > temp.file I played a bit around but could not specify the end string command... So this is the... (9 Replies)
Discussion started by: unknown7
9 Replies

9. Shell Programming and Scripting

Sed or Grep to delete line containing patter plus extra line

I'm new to using sed and grep commands, but have found them extremely useful. However I am having a hard time figuring this one out: Delete every line containing the word CEN and the next line as well. ie. test.txt blue 324 CEN green red blue 324 CEN green red blue to produce:... (2 Replies)
Discussion started by: rocketman88
2 Replies

10. Shell Programming and Scripting

Grep a string and print a string from the line below it

I know how to grep, copy and paste a string from a line. Now, what i want to do is to find a string and print a string from the line below it. To demonstrate: Name 1: ABC Age: 3 Sex: Male Name 2: DEF Age: 4 Sex: Male Output: 3 Male I know how to get "3". My biggest problem is to... (4 Replies)
Discussion started by: kingpeejay
4 Replies
Login or Register to Ask a Question