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 ?
# 1  
Old 10-21-2008
Question How to grep substring from a line ?

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.
# 2  
Old 10-21-2008
Question Can you provide a couple more examples?

To do things like this, one normally needs some 'anchors' or known targets. From your example I can assume certain things about character placement and parenthesis, but that may have been just the way you typed that one line.
Would like to see something along the lines of:

Message from user Joe (Sent 10/13/08(Fix the hard drive))
Print for PTR_1(Update 10/17/08(re-install driver complete))
Message for Sue(Sent 10/21/08(Out of disk space))

Are you trying to pull something like that apart?

Last edited by joeyg; 10-21-2008 at 09:29 AM.. Reason: Corrected spelling.
# 3  
Old 10-21-2008
only for that example string.
Code:
# string="This is the TEST(Message(with some tricky word))"
# echo ${string#*(}
Message(with some tricky word))
# str=${string#*(}
# echo ${str%)*}
Message(with some tricky word)

# 4  
Old 10-21-2008
sed --version
GNU sed version 4.1.5

Code:
echo "This is the TEST(Message(with some tricky word)) from which i want to search." \ 
| sed 's/.*TEST(\{1\}\(.*\))\{1,\}.*$/\1/'

explanation:
.* - look for any number of any character (including none)
TEST - followed by TEST
(\{1\} - followed by 1 ( [and only 1, if you want it to match more than 1 use \{1,\}]
\( - remember what you match from now on
.* - look for any number of any character (including none)
\) - end remembering matches
)\{1,\} - followed by 1 or more )
.*$ - followed by 0 or more characters to the end of the line

s/<regex>/\1/ - will substitute (s) what the regex matches, with the first remembered item from the regex.

so all that outputs
Code:
Message(with some tricky word)

# 5  
Old 10-21-2008
You have the line as a variable so why more tricky actions to get the same result?
It's sufficient if the grep command is successful..or have I missed something?

Regards
# 6  
Old 10-21-2008
Hammer & Screwdriver Here it is with a couple cut commands

Code:
> echo "This is the TEST(Message(with some tricky word)) from which i want to search." | cut -d")" -f1-2 | cut -d"(" -f2-
Message(with some tricky word)
>

# 7  
Old 10-21-2008
Code:
echo "This is the TEST(Message(with some tricky word)) from which i want to search." |  sed 's/.*\(TEST.*))\).*$/\1/'

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