Using SED to extract a word or string.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using SED to extract a word or string.
# 1  
Old 01-25-2012
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 table that was last "

I have tried to use the SED string with the following

Code:
echo " This has been entered in MS_RECENT_ACTIVITY the table that was last " |sed -n 's/.*\([Mm][Ss]_.*\).*/\1/p'

The results are
Quote:
MS_RECENT_ACTIVITY the table that was last
How can I only get "MS_RECENT_ACTIVITY" extracted always starting with the " MS_" and ending at the first following blank or space using SED? The wording following will not be constant as the string will not always be "the table that..." I am attempting to avoid using a pipe to awk to print the first word.

Thank you for your assistance.

Last edited by simpletech369; 01-25-2012 at 12:26 PM..
# 2  
Old 01-25-2012
Try:
Code:
sed -n 's/^.*\([Mm][Ss]_[^ ]*\).*/\1/p'

# 3  
Old 01-25-2012
That code works perfect. Thank you so much.Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Command to extract word from a string

Hi All, I have a word and from that word would like to search for certain set of string, is there any command to do so ? EX : Components from the above word, would like to search for strings set and extract the search string and then do if stmt... pon nen ent Com say... (2 Replies)
Discussion started by: Optimus81
2 Replies

2. Shell Programming and Scripting

Extract First and matching word from string in UNIX

Thank you (2 Replies)
Discussion started by: Pratik Majithia
2 Replies

3. Shell Programming and Scripting

sed relacement of word after particular string in a file

Hi All, I am trying to replace the string in a file after some particular string in the file. I am reading both string from one input file. localhostTest|http:\\localhost:7222 After string "locatTest" it should place the "http:\\localhost:7222" The below logic is replacing... (5 Replies)
Discussion started by: sharsour
5 Replies

4. Shell Programming and Scripting

sed command to remove a word from string

Hello All, I am running a command find . -name amp.cfg | cut -c 3- which gives me output something like below rel/prod/amp.cfg rel/fld/amp.cfg deb/detail/amp.cfg deb/err/amp.cfg I want to remove trailing "/amp.cfg" so that i should get output something like... (7 Replies)
Discussion started by: anand.shah
7 Replies

5. Shell Programming and Scripting

how to extract last word and a number from a string

I have the following script (which I made by my self) #!/bin/bash # add a few empty lines to make it more legible # add a date description on each update interval echo "" >> /home/user/DYN_DNS_IP_change.log echo "" >> /home/user/DYN_DNS_IP_change.log echo "" >>... (6 Replies)
Discussion started by: mahirzukic2
6 Replies

6. 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

7. Shell Programming and Scripting

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 cat test.txt | sed -n 's/.*get*\(\)//p But... (8 Replies)
Discussion started by: prasbala
8 Replies

8. Shell Programming and Scripting

extract string portion using sed

Hi All I have 3 files as listed below and highlighted in bold the portions of the filenames I need to extract: TOS_TABIN218_20090323.200903231830 TOS_TABIN219_1_20090323.200903231830 TOS_TABIN219_2_20090323.200903231830 I tried source_tabin_name=`echo $fname | sed 's/_.*//'` but I... (6 Replies)
Discussion started by: santam
6 Replies

9. Shell Programming and Scripting

Extract single or multi word string in Cshell

I am using the following code: set LASInputFile = `ls *. | head -1` set COMPLine = `grep -i :COMPANY $LASInputFile` to extract the following line from my input file: COMP. XYZ Public Company :COMPANY NAME I now need to extract the full name of the company which... (15 Replies)
Discussion started by: phudgens
15 Replies

10. Shell Programming and Scripting

perl newbie: how to extract an unknown word from a string

hi, im quite new to perl regexp. i have a problem where i want to extract a word from a given string. but the word is unknown, only fact is that it appears as the second word in the string. Eg. input string(s) : char var1 = 'A'; int var2 = 10; char *ptr; and what i want to do is... (3 Replies)
Discussion started by: wolwy_pete
3 Replies
Login or Register to Ask a Question