one more query for searching string.......


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting one more query for searching string.......
# 1  
Old 02-01-2006
one more query for searching string.......

Dear friends,
I have one more query,
incase a file contains multiple tags of same name, then how to get the required string between the tags, in which the string begins with "O/M"

i.e., file1.txt contains following text(please note that all the following tags are in single line)
----------------------------
<aaa> some text1</aaa><bbb> some text2</bbb><ccc> some text3</ccc><kkk>W/X12345</kkk><ddd> some text4</ddd><eee> some text5</eee><fff> some text6</fff><kkk>3333</kkk><ggg> some text7</ggg><hhh> some text8</hhh>
----------------------------
please see that above file conatins "<kkk>" twice. i.e.,
<kkk>W/X12345</kkk>
and
<kkk>3333</kkk>

I have to extract the entire string that starts with "W/X" till the end tag("</kkk>")
i.e., I need to extract "W/X12345"

this paramenter,W/X12345, length may not be constant, some times it may be
W/X123
or
W/X87789989
or
W/X90798897989
ect...
please tell me how do I get this paramenter?


waiting for ur reply,
swamymns
# 2  
Old 02-01-2006
lets say your string is str

echo "${str}"|sed 's#W/X.*[!<]//g'

This will give you the string str - contents in W/X....<
the pattern that you need to match is W/X.*[!<] meaning characters starting at W/X followed by any characters till '<'
# 3  
Old 02-01-2006
sed: command garbled: s#W/X.*[!<]//g...

Hi friend,
thanks for ur reply, but when I use the ur command,
echo "${str}"|sed 's#W/X.*[!<]//g'
where str contains the complete file content.

I ma getting following error...
sed: command garbled: s#W/X.*[!<]//g

please go through my query once again and kindly give me the solution..
waiting for ur reply,

swamymns
# 4  
Old 02-01-2006
please help me...

dear friends please help me..
incase a file contains multiple tags of same name, then how to get the required string between the tags, in which the string begins with "O/M"

i.e., file1.txt contains following text(please note that all the following tags are in single line)
----------------------------
<aaa> some text1</aaa><bbb> some text2</bbb><ccc> some text3</ccc><kkk>W/X12345</kkk><ddd> some text4</ddd><eee> some text5</eee><fff> some text6</fff><kkk>3333</kkk><ggg> some text7</ggg><hhh> some text8</hhh>
----------------------------
please see that above file conatins "<kkk>" twice. i.e.,
<kkk>W/X12345</kkk>
and
<kkk>3333</kkk>

I have to extract the entire string that starts with "W/X" till the end tag("</kkk>")
i.e., I need to extract "W/X12345"

this paramenter,W/X12345, length may not be constant, some times it may be
W/X123
or
W/X87789989
or
W/X90798897989
ect...
please tell me how do I get this paramenter?


waiting for ur reply,
swamymns
# 5  
Old 02-01-2006
Hi,

This might not be very efficient, but hopefully will work in your case:
cat file_name|tr -d '\n'| awk -F"<[a-zA-Z/0-9]*>" '{for (i=0;i<=NF;i+=1)print $i}' |grep "^W/X*"


(file_name is where you put your filename)

Please let me know how this fares...
# 6  
Old 02-02-2006
i m sorry the command should have been

echo "${str}"|sed 's#W/X.*[!<]##g'
# 7  
Old 02-03-2006
Quote:
Originally Posted by Abhishek Ghose
Hi,

This might not be very efficient, but hopefully will work in your case:
cat file_name|tr -d '\n'| awk -F"<[a-zA-Z/0-9]*>" '{for (i=0;i<=NF;i+=1)print $i}' |grep "^W/X*"


(file_name is where you put your filename)

Please let me know how this fares...
I didnt notice that you said everything is on a single line. I assumed that its spread across all across a file, and a starting tag may/maynot have a matching closing tag. In case of single line:
cat file_name|awk -F"<[a-zA-Z/0-9]*>" '{for (i=0;i<=NF;i+=1)print $i}' |grep "^W/X*"
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Searching for variable string

Hi Guys, So im trying to search for the most recent occurance of a string in the most recently updated .log file in a certain directory. The string i am searching for is a value, now if this value is greater than 800 i would like an email sent out with some text (blah blah blah). This is what... (7 Replies)
Discussion started by: hello_world
7 Replies

2. Programming

Searching String which repeates

Hi All, Any one please help me with the below scenario. I have a file with the data like below This is the integer variable name is <abc1> This is the Float variable name is <abc1> This is the integer variable name is <abc2> This is the Float variable name is <abc2> This is the integer... (1 Reply)
Discussion started by: jhon1257
1 Replies

3. Shell Programming and Scripting

Searching for a string using loop.

Hi i am very new to shell scripting. I have got stuck on a portion on my script. Problem : I have 30 logfiles inside /home/test directory. I need to find the string "@ended today" in all the 30 logfiles and print the name of the files which did not have this string. i need to this by... (3 Replies)
Discussion started by: Soma Das
3 Replies

4. Shell Programming and Scripting

Searching a String

Hi everyone ! suppose i'm searching for a specific string in a file so it is very easy, i use the following command grep 'keyword' file_name but how to search a word which is repeated maximum number of times in a file, for example in the following text i have to search a word which is... (12 Replies)
Discussion started by: ourned
12 Replies

5. Shell Programming and Scripting

searching the required string and appending string to it.

Hi all, I have some data in the form of adc|nvhs|nahssn|njadnk|nkfds in the above data i need to write a script so thet it will append "|||" to the third occurnace in the string ..... the outout should look like adc|nvhs|nahssn||||njadnk|nkfds Thanks, Firestar. (6 Replies)
Discussion started by: firestar
6 Replies

6. Shell Programming and Scripting

searching each file for a string

Hi Guys... I want to search for each file that contains a particular string. e.g find . -print | xargs grep -i string_name Now my issue is the files that I search in are gzipped. Will I be able to find the string, using the above commands, even if the files are gzipped? Please... (2 Replies)
Discussion started by: Phuti
2 Replies

7. Shell Programming and Scripting

Searching a string in a file

Hi, I am new to unix shell scripting. I have a requirement. Could anyone help me writing the script for the same? Here goes the requirement: I have a config file let's say temp.config. Here is the data in the config file temp.config : ------------- name=victor age=42 state=texas... (5 Replies)
Discussion started by: badrimohanty
5 Replies

8. UNIX for Dummies Questions & Answers

searching for a string in a file

I need to search for a specific string in a file and if this string exist I need to replace it with something else. I am not sure how I could do this, using an if statement. (2 Replies)
Discussion started by: ROOZ
2 Replies

9. Shell Programming and Scripting

Extracting a string from one file and searching the same string in other files

Hi, Need to extract a string from one file and search the same in other files. Ex: I have file1 of hundred lines with no delimiters not even space. I have 3 more files. I should get 1 to 10 characters say substring from each line of file1 and search that string in rest of the files and get... (1 Reply)
Discussion started by: mohancrr
1 Replies

10. Shell Programming and Scripting

Searching a string for a character

I have a co-worker that is trying to make a Wheel of Fortune game and he wants to know if there is a way to search a string for the letter given by the user. (7 Replies)
Discussion started by: turbulence
7 Replies
Login or Register to Ask a Question