sed to extract all strings


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed to extract all strings
# 1  
Old 04-01-2013
sed to extract all strings

Hi,
I have a text file containing 2 lines as follows:
Quote:
AME WHERE AME.BUSINESS_UNIT = PS_PROJ_RESOURCE.BUSINESS_UNIT AND AME.PROJECT_ID = PS_PROJ_RESOURCE.PROJECT_ID AND AME.ACTIVITY_ID =
PS_PROJ_RESOURCE.ACTIVITY_ID AND AME.RES_USER1 = PS_PROJ_RESOURCE.RES_USER1 AND AME.RESOURCE_ID_FROM = PS_PROJ_RESOURCE.RESOURCE_ID_
FROM AND AME.ANALYSIS_TYPE = 'AME' AND AME.BI_DISTRIB_STATUS = 'P'
AME WHERE AME.BUSINESS_UNIT = PS_PROJ_RESOURCE.BUSINESS_UNIT AND AME.PROJECT_ID = PS_PROJ_RESOURCE.PROJECT_ID AND AME.ACTIVITY_ID =
PS_PROJ_RESOURCE.ACTIVITY_ID AND AME.RES_USER1 = PS_PROJ_RESOURCE.RES_USER1 AND AME.RESOURCE_ID_FROM = PS_PROJ_RESOURCE.RESOURCE_ID_
FROM AND AME.ANALYSIS_TYPE = 'AME' AND AME.BI_DISTRIB_STATUS = 'P'
I'm trying to extract all the strings following an "AME." The output would be as follows:

Code:
BUSINESS_UNIT
PROJECT_ID
ACTIVITY_ID
RES_USER1
RESOURCE_ID_FROM
ANALYSIS_TYPE
BI_DISTRIB_STATUS
BUSINESS_UNIT
PROJECT_ID
ACTIVITY_ID
RES_USER1
RESOURCE_ID_FROM
ANALYSIS_TYPE
BI_DISTRIB_STATUS

Tried the following sed:

Code:
 
sed -n 's/.*\(AME\.\)\(.*\)/\2/gp' l.l | awk '{print $1}'

But returns only:

Code:
BI_DISTRIB_STATUS
BI_DISTRIB_STATUS

Any help would be great as I'm new to HP-UX Korn shell scripting

Last edited by Scrutinizer; 04-01-2013 at 03:42 PM.. Reason: code tags, reformatting
# 2  
Old 04-01-2013
Trying to verify...

You are trying to get the first word after
Code:
AME.

# 3  
Old 04-01-2013
Will awk do?
Code:
awk '{for(i=1;i<=NF;i++){if($i ~ /AME.[A-Z]/){split($i,a,/\./);print a[2]}}}' input_file

--ahamed
This User Gave Thanks to ahamed101 For This Post:
# 4  
Old 04-01-2013
Correct, I want to extract the first word after the "AME."

---------- Post updated at 03:09 PM ---------- Previous update was at 03:04 PM ----------

Thank you Ahamed101, that works perfect.
# 5  
Old 04-01-2013
An approach using grep that seems to work for the given text:
Code:
grep -o "AME\.[[:alnum:]_]*[[:space:]]" file | cut -c 5-

# 6  
Old 04-02-2013
Code:
$ sed "s/[^=]*\(AME.[^ ]*\) =/\1 /g;s/AME./\\
/g;s/ *[^ ]* *$//" file

BUSINESS_UNIT
PROJECT_ID

RES_USER1
RESOURCE_ID_FROM

ANALYSIS_TYPE
BI_DISTRIB_STATUS

BUSINESS_UNIT
PROJECT_ID

RES_USER1
RESOURCE_ID_FROM

ANALYSIS_TYPE
BI_DISTRIB_STATUS


Last edited by anbu23; 04-02-2013 at 02:36 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract strings from output

I am having the following output when executing a dig command : dig @1.1.1.1 google.com +noall +answer +stats ; <<>> DiG 9.11.4-P1 <<>> @1.1.1.1 google.com +noall +answer +stats ; (1 server found) ;; global options: +cmd obodrm.prod.at.dmdsdp.com. 86154 IN A ... (1 Reply)
Discussion started by: liviusbr
1 Replies

2. UNIX for Beginners Questions & Answers

Extract content between strings

Hello i am stuck with this. i have input which is as follows /type/work /works/OL10627594W 3 2019-04-24T16:46:21.351549 {"created": {"type": "/type/datetime", "value": "2009-12-11T03:18:17.488715"}, "title": "Tog the dog", "covers": , "last_modified": {"type":... (3 Replies)
Discussion started by: ahfze
3 Replies

3. UNIX for Dummies Questions & Answers

Issue when using egrep to extract strings (too many strings)

Dear all, I have a data like below (n of rows=400,000) and I want to extract the rows with certain strings. I use code below. It works if there is not too many strings for example n of strings <5000. while I have 90,000 strings to extract. If I use the egrep code below, I will get error: ... (3 Replies)
Discussion started by: forevertl
3 Replies

4. Shell Programming and Scripting

Extract text between two strings

Hi, I have a text like these: ECHO "BEGGINING THE SHELL....." MV FILE1 > FILE2 UNIQ_ID=${1} PARTITION_1=`${PL}/Q${CON}.KSH "SELECT ....." PARTITION_2=`${PL}/Q${CON}.KSH "SELECT ........" ${PL}/Q${CON}.KSH "CREATE ...." IF .... ....... I would like to extract only text that only... (4 Replies)
Discussion started by: mierdatuti
4 Replies

5. UNIX for Dummies Questions & Answers

Extract strings based on the value

I have a file with multiple columns (in this case, the file has 3 columns): NM_001006304 (-33.7) XM_418228 (-38.4) JN880447 (-33.7) CR387600 (-33.7) CR524203 (-36.3) GALGA_6AKII_KRT75 (-33.7) GALGA25_SC7 (-31.9) CR352795 (-36.3) NM_204172 (-31.7) NM_204137 (-31.9) NM_001030561 (-36.3) AB011672... (7 Replies)
Discussion started by: yuejian
7 Replies

6. UNIX for Dummies Questions & Answers

Extract code between 2 strings.

Hi, Im having some problems with this. I have loaded a file with html code. All code is placed in the same line. I want to get everything between two given strings (including these strings and get only the first appearance). Example: File contains <html><body><a href='a.html'>abc</a><a... (5 Replies)
Discussion started by: ngb
5 Replies

7. Shell Programming and Scripting

Extract two strings from a file and create a new file with these strings

I have the following lines in a log file. It would be great if some one can help me to create a new file with the just entries in the below format. 66.150.161.195 HPSAC=Z05 66.150.161.196 HPSAC=A05 That is just extract the IP address and the string DPSAC=its value 66.150.161.195 -... (1 Reply)
Discussion started by: Tuxidow
1 Replies

8. Shell Programming and Scripting

How to Extract text between two strings?

Hi, I want to extract some text between two strings in a line i am using following command i.e; awk '/-string1/,/-string2/' filename contents of file is--- line1 line2 aaa -bbb -ccc -string1 c,d,e -string2 line4 but it is showing complete line which is having searched strings. aaa... (19 Replies)
Discussion started by: emresearch
19 Replies

9. UNIX for Advanced & Expert Users

bash/grep/awk/sed: How to extract every appearance of text between two specific strings

I have a text wich looks like this: clid=2 cid=6 client_database_id=35 client_nickname=Peter client_type=0|clid=3 cid=22 client_database_id=57 client_nickname=Paul client_type=0|clid=5 cid=22 client_database_id=7 client_nickname=Mary client_type=0|clid=6 cid=22 client_database_id=6... (3 Replies)
Discussion started by: Pioneer1976
3 Replies

10. UNIX for Dummies Questions & Answers

Using awk/sed to extract text between Strings

Dear Unix Gurus, I've got a data file with a few hundred lines (see truncated sample)... BEGIN_SCAN1 TASK_NAME=LA48 PDD Profiles PROGRAM=ArrayScan 1.00 21.220E+00 2.00 21.280E+00 END_DATA END_SCAN1 BEGIN_SCAN2 TASK_NAME=LA48 PDD Profiles 194.00 2.1870E+00 ... (5 Replies)
Discussion started by: tintin72
5 Replies
Login or Register to Ask a Question