Extract word using sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract word using sed
# 1  
Old 03-05-2010
Extract word using sed

Hello,
I am new to sed and am trying to extract a word using sed.

for example i have a line "const TotalAmount& getTotalAmount() const; " in the file test.txt

I am trying to extract getTotalAmount() from the line.

For this i tried
Code:
cat test.txt | sed -n 's/.*get[a-zA-Z]*\(\)//p

But the output is
Code:
       () const;

Could you please let me know the correct expression or what i am doing wrong.

Also could you please suggest some good basic tutorials for Sed.

Last edited by Franklin52; 03-05-2010 at 05:38 AM.. Reason: Please use code tags!
# 2  
Old 03-05-2010
For extracting the getTotalAmount() from the string.You use the following command.

Code:
sed -r 's/.*(get[a-zA-Z]+\(\)).*/\1/g' <file>

This is the best tutorial for reading about SED.

SED and Regular Expressions

Last edited by vivekraj; 03-05-2010 at 05:42 AM..
# 3  
Old 03-05-2010
You try this.

Code:
echo "const TotalAmount& getTotalAmount() const;" | sed -r 's/.{19}//;s/.{7}$//'


Last edited by thillai_selvan; 03-05-2010 at 05:45 AM..
# 4  
Old 03-05-2010
Code:
sed -r "s/.*(getTotalAmount\(\)).*/\1/g" file

# 5  
Old 03-05-2010
MySQL Solution

Try this
Code:
 cat test | sed -nr 's/.*(get[a-zA-Z]*\(\)).*/\1/p'

Output
Code:
getTotalAmount()

I believe that this tutorial would be nice for beginners
Sed - An Introduction and Tutorial
# 6  
Old 03-05-2010
Or you can try this also
Code:
sed -re 's/.{19}//;s/.{8}$//' test.txt

Here what I am doing is I am removing the first 19 characters and last 8 characters.
So you can easily get the middle remaining string as "getTotalAmount()".
# 7  
Old 03-05-2010
Code:
sed -n "s/.*\(get[a-zA-Z]*()\).*/\1/p" infile

Code:
grep -o "get[a-zA-Z]*()" infile


Last edited by Scrutinizer; 03-05-2010 at 06:40 AM.. Reason: You're right. That should be get[a-zA-Z]*() of course (pasto)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replacing a particular word with another word in all the xml's under a particular directory with sed

Hi Folks, Could you please advise what will be the SED command to replace a word in all xml's under a particular directory for example let say I rite now at the following below location $ cd /ter/rap/config now under config directory there will be lots of xml file , now my objective is to... (1 Reply)
Discussion started by: punpun66
1 Replies

2. Shell Programming and Scripting

Extract word between two KEYWORDS

Hi I want to extract all the words between two keywords HELLO & BYE. eg: Input 1_HELLO_HOW_ARE_YOU_BYE_TEST 1_HELLO_WHERE_ARE_BYE_TEST 1_HELLO_HOW_BYE_TEST Output Required: HOW_ARE_YOU WHERE_ARE HOW (7 Replies)
Discussion started by: dashing201
7 Replies

3. Shell Programming and Scripting

Using SED to extract a word or string.

I am working with the ksh shell in HP UNIX and I am attempting to extract a word, beginning with a particular string and ending at the first space. for example I want to extract the word or string MS_RECENT_ACTIVITY from the following string " This has been entered in MS_RECENT_ACTIVITY the... (2 Replies)
Discussion started by: simpletech369
2 Replies

4. Shell Programming and Scripting

Extract word from text (sed,awk, etc...)

Hello, I need some help extracting the number after the RBA e.g 15911688 from the below block of text (e.g: grep RBA |sed .......). The code should be valid for blocks if text generated at different times as well and not for the below text only. ... (2 Replies)
Discussion started by: drbiloukos
2 Replies

5. Shell Programming and Scripting

Extract a word from sentence

$SET_PARAMS='-param Run_Type_Parm=Month -param Portfolio_Parm="997" -param From_Date_Parm="2011-08-09"' Want to extract the value of "Portfolio_Parm" from $SET_PARAMS i.e in the above case "997" & assigned to new variable. The existence order of "Portfolio"Parm" can change, but the name... (2 Replies)
Discussion started by: SujeethP
2 Replies

6. Shell Programming and Scripting

extract whole thing in word, leaving behind last word. - perl

Hi, i've a string /u/user/DTE/T_LOGS/20110622_011532_TEST_11_HD_120/HD/TESi T_11_HD_120/hd-12 i need to get string, like /u/user/DTE/T_LOGS/20110622_011532_TEST_11_HD_120/HD the words from HD should get deleted, i need only a string till HD, i dont want to use any built in... (4 Replies)
Discussion started by: asak
4 Replies

7. Shell Programming and Scripting

Extract word from a line

Hi, I've searched the forum to get what I wanted but to no avail. Here's the problem I'm facing. The line is suppose as below: <INPUT DATABASE ="ORACLE" DBNAME ="UNIX" NAME ="FACT_TABLE" OWNERNAME ="DIPS"> Now I want to extract only FACT_TABLE. The trials are as follows: awk... (3 Replies)
Discussion started by: dips_ag
3 Replies

8. UNIX for Dummies Questions & Answers

Word extract from a line

In a file let a.txt, has 1 line code i.e. code QC:Tiger:10: /code I need to get my output like "Tiger 10". So how can i do this? (3 Replies)
Discussion started by: anupdas
3 Replies

9. Shell Programming and Scripting

Extract Part of a "Word", using AWK or SED????

I have been lurking on this forum for some time now and appreciate Everyone's help. I need to find a way to get the SystemID from this XML file. The file is much larger than just this one line but I can grep and get this line Printed. But really just need the "systemid". <test123: prefintem... (9 Replies)
Discussion started by: elbombillo
9 Replies

10. AIX

extract same word from two files

file1 contains: 3 file2 contains: 1 2 3 . . 20000 cat file1 | while read line do grep -s $line file2 >> file3 done my result file3 shows: 3 3 3 (4 Replies)
Discussion started by: tjmannonline
4 Replies
Login or Register to Ask a Question