search a long line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting search a long line
# 1  
Old 03-21-2007
search a long line

Hey all, I need to retrieve something from a line, say

<TEST><A>123</A><B>456</B><ID>1111</ID><C>789</C><D>000</D></TEST><TEST><A>123</A><B>456</B><ID>2222</ID><C>789</C><D>000</D></TEST>


I need to match <ID>111</ID>, so I want to retrieve

<ID>1111</ID><C>789</C><D>000</D></TEST>

is this possible, can anyone help? Thank you!
# 2  
Old 03-21-2007
Code:
sed "s;\(</TEST>\)\(<TEST>\);\1\\
\2;g" f | sed -n "s/.*\(<ID>1\{1,\}.*\)/\1/p"


Last edited by anbu23; 03-21-2007 at 07:05 AM.. Reason: Removed filename at the end of command
# 3  
Old 03-21-2007
I don't quite get what exactly that command does, do you mind explain it a little? Thanks!
# 4  
Old 03-21-2007
if you have Python and know the language, here's an alternative:
Code:
#!/usr/bin/python
import sys
choice=sys.argv[1]
for line in open("file"):
     for li in line.split("<TEST>"):	
	  if "<ID>%s</ID>" % choice in li:
	       ind = li.index("<ID>1111</ID>")
	       print li[ind:]

output:
Code:
# ./test.py 1111
<ID>1111</ID><C>789</C><D>000</D></TEST>


Last edited by ghostdog74; 03-21-2007 at 08:21 AM..
# 5  
Old 03-21-2007
Code:
$ cat file
<TEST><A>123</A><B>456</B><ID>1111</ID><C>789</C><D>000</D></TEST><TEST><A>123</A><B>456</B><ID>2222</ID><C>789</C><D>000</D></TEST>

First sed separates TEST tags into separate lines
Code:
$ sed "s;\(</TEST>\)\(<TEST>\);\1\\
> \2;g" file
<TEST><A>123</A><B>456</B><ID>1111</ID><C>789</C><D>000</D></TEST>
<TEST><A>123</A><B>456</B><ID>2222</ID><C>789</C><D>000</D></TEST>

Second sed matches <ID>1</ID> to till end of the line
1\{1,\} matches from one 1 to n number of 1s.
for example matches 1, 11, 111, 1111 and so on
Code:
$ sed "s;\(</TEST>\)\(<TEST>\);\1\\
> \2;g" file | sed -n "s/.*\(<ID>1\{1,\}.*\)/\1/p"
<ID>1111</ID><C>789</C><D>000</D></TEST>

# 6  
Old 03-21-2007
but my ID is random, say 28654..then what should I do?
# 7  
Old 03-21-2007
Code:
id="28654"
sed "s;\(</TEST>\)\(<TEST>\);\1\\
\2;g" file | sed -n "s/.*\(<ID>${id}.*\)/\1/p"

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Solaris

Line too long error Replace string with new line line character

I get a file which has all its content in a single row. The file contains xml data containing 3000 records, but all in a single row, making it difficult for Unix to Process the file. I decided to insert a new line character at all occurrences of a particular string in this file (say replacing... (4 Replies)
Discussion started by: ducati
4 Replies

2. Shell Programming and Scripting

cron woes - line too long ??

Here is my cron entry: 13 10 * * * /home/my_login/mystf/myscriptsize.sh > /home/my_login/mystf/`date +"%Y%m%d"`log.csv Here is my cron error: unexpected EOF while looking for matching ``' ...cron executes: /home/my_login/mystf/myscriptsize.sh > /home/my_login/mystf/`date +" ..and NOT... (2 Replies)
Discussion started by: landog
2 Replies

3. Shell Programming and Scripting

perl search and replace - search in first line and replance in 2nd line

Dear All, i want to search particular string and want to replance next line value. following is the test file. search string is tmp,??? ,10:1 "???" may contain any 3 character it should remain the same and next line replace with ,10:50 tmp,123 --- if match tmp,??? then... (3 Replies)
Discussion started by: arvindng
3 Replies

4. UNIX for Dummies Questions & Answers

vi : Line too Long Error

Hi ! I am woking on Solaris 5.10 and face a problem sometimes when i try to open files in the vi editor . Sometime when the file is big i am not able to view the file in vi and i get the error like I guess vi have some line size setting and if the lines are ,longer than that... (4 Replies)
Discussion started by: Paarth
4 Replies

5. UNIX for Dummies Questions & Answers

grep long line

I am trying to get the count of a word from an xml using the below command grep "search word" -c test.xml The result is only 1, because my xml contains only one line (hule line- several thousand characters). I guess grep command will process for only 128 characters for each line. ... (7 Replies)
Discussion started by: venky23
7 Replies

6. Shell Programming and Scripting

grep : search a long char contain space

Hi, i have to search for a char like that : export var1="i am not happy /not happy" with a command like : grep $var1 file but this not working with me !!! thank you in advance. (2 Replies)
Discussion started by: tizilfin
2 Replies

7. Shell Programming and Scripting

Perl: Search for string on line then search and replace text

Hi All, I have a file that I need to be able to find a pattern match on a line, search that line for a text pattern, and replace that text. An example of 4 lines in my file is: 1. MatchText_randomNumberOfText moreData ReplaceMe moreData 2. MatchText_randomNumberOfText moreData moreData... (4 Replies)
Discussion started by: Crypto
4 Replies

8. UNIX for Dummies Questions & Answers

vi.....line too long

I'm getting a "line too long" error when I try to vi a particular file. I really just need to view the contents as I don't have to change anything. Any suggestions?? I already tried to cat but it didnt work........:( (7 Replies)
Discussion started by: shorty
7 Replies

9. Shell Programming and Scripting

line too long using awk or sed or tr

Goodmorning, I have MKS Toolkit (K-Shell) running on a windows server. On it I have a c program that in a true unix environment works fine, but here it adds an extra '0000000000000016000A' in various places in the file that the c program produces that I need to remove. Here is what the file... (3 Replies)
Discussion started by: philplasma
3 Replies
Login or Register to Ask a Question