How to grep substring from a line ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to grep substring from a line ?
# 8  
Old 10-22-2008
Quote:
Originally Posted by titanicstarin
Hi all,
I am stuck in a tricky situation where i want to search a substring from a line through shell script.What i meant by that is given in the example shown below:

Line :: This is the TEST(Message(with some tricky word)) from which i want to search.
Result substring should be : Message(with some tricky word)

How can i get only "Message(with some tricky word)" from the Line "This is the TEST(Message(with some tricky word)) from which i want to search." ?

I cant format a RegExp for this kind of search. Please help me to format such tricky Regular Expression with some useful commands .Thanks in advance.
Here are some more examples for every one to make it more clear what i want ...!

EXAMPLE ::
The input string will be like this :

This is another example where TEST( string is going to be (after TEST everytime ) and it will be in the (braces all the time like this ) so this clears my requirements. ) ;

Result should be :

string is going to be (after TEST everytime )

There will be more variety in terms of braces but the string to be searched will always be after TEST and in the braces starts immediately after TEST upto the end of that brace only.
# 9  
Old 10-22-2008
Quote:
Originally Posted by titanicstarin
Here are some more examples for every one to make it more clear what i want ...!

EXAMPLE ::
The input string will be like this :

This is another example where TEST( string is going to be (after TEST everytime ) and it will be in the (braces all the time like this ) so this clears my requirements. ) ;

Result should be :

string is going to be (after TEST everytime )

There will be more variety in terms of braces but the string to be searched will always be after TEST and in the braces starts immediately after TEST upto the end of that brace only.
Thank You all for your reply. I think i have found the solution for my query.I am putting it here.

echo "This is the TEST(Message(with some tricky word)) from which i want to search." | sed -e 's/.*TEST(//g ; s/),.*//g'
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep -q not works with multiples grep in same line

Hi, I'm trying to make a grep to see if exists occurrences with a sentence like these: grep -qi "message" file0 | grep -i $date | grep -vi "exception" echo $? 1 If I execute without -q modifier I can find occurrences. Someone could help me please? Thanks and sorry for my English! (1 Reply)
Discussion started by: mierdatuti
1 Replies

2. Shell Programming and Scripting

To append new data at the end of each line based on substring of last column

Hi guys, I need to append new data at the end of each line of the files. This new data is based on substring (3rd fields) of last column. Input file xxx.csv: U1234|1-5X|orange|1-5X|Act|1-5X|0.1 /sac/orange 12345 0 U5678|1-7X|grape|1-7X|Act|1-7X|0.1 /sac/grape 5678 0... (5 Replies)
Discussion started by: null7
5 Replies

3. UNIX for Dummies Questions & Answers

Piping grep into awk, read the next line using grep

Hi, I have a number of files containing the information below. """"" Fundallinfo 6.3950 14.9715 14.0482 """"" I would like to grep for Fundallinfo and use it to read the next line? I ideally would like to read the three numbers that follow in the next line and... (2 Replies)
Discussion started by: Paul Moghadam
2 Replies

4. Shell Programming and Scripting

How to Grep than scan line below grep pattern

Hello Colleagues, I have a file that looks like below. 6-12731913-12731913 9230760143480 410018547148230 20131002193434+0500 20131002193434+0500 ;20131002T161031000-10.50.241.21-21912131-1419034760, ver: 0 20131009 92220056296730 CC0P abc Core_Context_R1A SMS 6-12726796-12726796... (14 Replies)
Discussion started by: umarsatti
14 Replies

5. Shell Programming and Scripting

Extracting a substring from Line

Hi All I have a line which is as below 1234567 Gagan Paswani zz23432 1000000000 1000000000 ASTHEYXX-RTUC zz23432 I need to extract the first occurence of zz23432 which will have the reg expression as {2}{5} Could you please assist as to how I can extract this... (3 Replies)
Discussion started by: Prashantckc
3 Replies

6. Shell Programming and Scripting

Extract substring specif position and length from file line

Hi gurus, I am trying to figure out how to extract substring from file line (all lines in file), as specified position and specified legth. Example input (file lines) dhaskjdsa dsadhkjsa dhsakjdsad hsadkjh dsahjdksahdsad sahkjd sahdkjsahd sajkdh adhjsak I want to extract substring on... (5 Replies)
Discussion started by: ProsteJa
5 Replies

7. UNIX for Dummies Questions & Answers

Grep of a SubString

Dear Folks, I am having a requirement to grep a word which could be a part of a substring of a file. Please help me with the command for korn shell. Let me give you a example I am having a file in which the data is based on positions. Lets say 1-5 is Emp ID 6-10 is Emp Name 11-15 is Salary... (5 Replies)
Discussion started by: dinesh1985
5 Replies

8. UNIX for Dummies Questions & Answers

Using sed to extract a substring at end of line

This is the line that I am using: sed 's/^*\({3}*$\)/\1 /' <test.txt >results.txt and suppose that test.txt contains the following lines: http://www.example.com/200904/AUS.txt http://www.example.com/200903/_RUS.txt http://www.example.com/200902/.FRA.txt What I expected to see in results.txt... (6 Replies)
Discussion started by: figaro
6 Replies

9. UNIX for Dummies Questions & Answers

grep exact string/ avoid substring search

Hi All, I have 2 programs running by the following names: a_testloop.sh testloop.sh I read these programs names from a file and store each of them into a variable called $program. On the completion of the above programs i should send an email. When i use grep with ps to see if any of... (3 Replies)
Discussion started by: albertashish
3 Replies

10. Shell Programming and Scripting

sed, grep, awk, regex -- extracting a matched substring from a file/string

Ok, I'm stumped and can't seem to find relevant info. (I'm not even sure, I might have asked something similar before.): I'm trying to use shell scripting/UNIX commands to extract URLs from a fairly large web page, with a view to ultimately wrapping this in PHP with exec() and including the... (2 Replies)
Discussion started by: ropers
2 Replies
Login or Register to Ask a Question