How to print line starts with specific word and contains specific word using sed?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to print line starts with specific word and contains specific word using sed?
# 8  
Old 10-10-2011
How about this:

Code:
tn=m_TASK_UPDATESSS
awk 'NR>1&&/^<MAPPING/{print RS $0;next}1' f1 | awk -v x="$tn" 'BEGIN{p="NAME=." x ". "}/^<MAPPING/&&match($0,p)' RS=""

# 9  
Old 10-11-2011
@Tmalik79

You have to replace "f1" with your filename.

By the way , what are you trying to achieve ?

1) filter out all <MAPPING ... > ... </MAPPING> block that not containing the pattern ?

or

2) filter out everything except the <MAPPING ... > ... </MAPPING> block that match the pattern ?

Please clarify what output you expect

Last edited by ctsgnb; 10-11-2011 at 04:47 AM..
# 10  
Old 10-11-2011
Thanks for all your help and time. This is really helpful to me and my whole project delivery is dependent on this.

I have tried above stmt and changed file name to.. I may not filter the data. File which i given is modified to restrict no of lines original file will have 1000+ lines. The file is generated from informatica repository.

what I am trying to do is..

1. My file has lot of content which i need to read and print specific properties within open and close tag.
In the above example..
First I need to get unique mapping open line and immediate mapping close tag and get
all the data in between lines to string or file and grep certain columns values.

Line starts with <MAPPING and same line contains NAME='xxxx' unique always so get the data from above unique line to till immediate </MAPPING> close tag to file or string.
<MAPPING ..NAME='xxxx'.. > some data </MAPPING>
the solution I want to apply for more similar tags.
Lets says output of the <MAPPING ..NAME='xxxx'.. > some data </MAPPING> can have below data inside. again I want repeats same steps to get the SESSIONTRANS to data.
<SESSIONTRANS NAME=’XXXX’>some data</SESSIONTRANS>
<FIELDS NAME=’xxxx’>somedata</FIELDS>

In simple words.
Step 1 : Capture data from 2 different lines to file as said above .ex. File name= k4.txt
Repeat Step1: in K4.txt to get other tags data. Example <SESSIONTRANS NAME=’XXX’ >some data</ SESSIONTRANS >
This will go on like wise to capture all required properties.
# 11  
Old 10-11-2011
Sorry but i don't get it, could you please provide and example of what final output you expect ?
# 12  
Old 10-11-2011
Here is the sample input file.

<MAPPING DESCRIPTION='' ISVALID='YES' NAME='m_UPROCS_DEPENDENCIES_LST' OBJECTVERSION='1' VERSIONNUMBER='2'>
<TRANSFORMATION DESCRIPTION='' NAME='SQ_M_UPROCS_DEPENDENCIES_LST' OBJECTVERSION='1' REUSABLE='NO' TYPE='Source Qualifier' VERSIONNUMBER='2'>
<TASK DESCRIPTION='' NAME='EventWait' REUSABLE='NO' TYPE='Event Wait' VERSIONNUMBER='2'>
<ATTRIBUTE NAME='Target load type' VALUE='Bulk'/>
</TASK>
</MAPPING>
<MAPPING DESCRIPTION='' ISVALID='YES' NAME='m_RULE_INFO' OBJECTVERSION='1' VERSIONNUMBER='2'>
<TRANSFORMATION DESCRIPTION='' NAME='SQ_M_RULE_INFO' OBJECTVERSION='1' REUSABLE='NO' TYPE='Targer Defination' VERSIONNUMBER='2'>
<TASK DESCRIPTION='' NAME='EventWait' REUSABLE='NO' TYPE='Event Wait' VERSIONNUMBER='2'>
<ATTRIBUTE NAME='Target load type' VALUE='Null'/>
</TASK>
</MAPPING>


sample OutPut:

Mapping Name,Transformation name,TRANSFORMATION TYPE,TASK NAME,TASK TYPE,TARGET LOAD TYPE
m_UPROCS_DEPENDENCIES_LST,SQ_M_UPROCS_DEPENDENCIES_LST,Source Qualifier,EventWait,Event Wait,Bulk
m_RULE_INFO,SQ_M_RULE_INFO,Targer Defination,EventWait,Event Wait,Null
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

In Vi "sed" substitute word on a specific line

i need to substitute word on a specific line. I was able to do it on command line like below but it is not working in vi. command line like below: sed -e '8s/table_name/schema.table_name/' file_name. in vi table_name and schema are my positional parameters that i pass into the script. ... (5 Replies)
Discussion started by: pimmit22043
5 Replies

2. Shell Programming and Scripting

Search for a specific word and print only the word from the input file

Hi, I have a sample file as shown below, I am looking for sed or any command which prints the complete word only from the input file. Ex: $ cat "sample.log" I am searching for a word which is present in this file We can do a pattern search using grep but I need to cut only the word which... (1 Reply)
Discussion started by: mohan_kumarcs
1 Replies

3. Shell Programming and Scripting

How to print multiple specific column after a specific word?

Hello.... Pls help me (and sorry my english) :) So I have a file (test.txt) with 1 long line.... for example: isgc jsfh udgf osff 8462 error iwzr 653 idchisfb isfbisfb sihfjfeb isfhsi gcz eifh How to print after the "error" word the 2nd 4th 5th and 7th word?? output well be: 653 isfbisfb... (2 Replies)
Discussion started by: marvinandco
2 Replies

4. Shell Programming and Scripting

sed / awk to get specific word in line

I have http log that I want to get words after specific "tag", this a sample line from the log: 98,POST,200 OK,www.facebook.com,Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1,/ajax/updatestatus.php?__a=1,datr=P_H1TgjTczCHxiGwdIF5tvpC; lu=Si1fMkcrU2SInpY8tk_7tAnw;... (6 Replies)
Discussion started by: erlanq
6 Replies

5. UNIX for Dummies Questions & Answers

How to print the specific word in a file.

Hi , My input file is below like that :- $cat abc.txt Service name: test_taf Service is enabled Server pool: test_tac Cardinality: 2 Disconnect: false Service role: PRIMARY Management policy: AUTOMATIC DTP transaction: false AQ HA notifications: true Failover type: SESSION... (3 Replies)
Discussion started by: sp001
3 Replies

6. Shell Programming and Scripting

break the string and print it in a new line after a specific word

Hi Gurus I am new to this forum.. I am using HP Unix OS. I have one single string in input file as shown below Abc123 | cde | fgh | ghik| lmno | Abc456 |one |two |three | four | Abc789 | five | Six | seven | eight | Abc098 | ........ I want to achive the result in a output file as shown... (3 Replies)
Discussion started by: kannansr621
3 Replies

7. Shell Programming and Scripting

search-word-print-specific-string

Hi, Our input xml looks like: <doc> <str name="account_id">1111</str> <str name="prd_id">DHEP155EK</str> </doc> - <doc> <str name="account_id">6666</str> <str name="prd_id">394531662</str> </doc> - <doc> <str name="account_id">6666</str> <str... (1 Reply)
Discussion started by: Jassz
1 Replies

8. Shell Programming and Scripting

awk or sed command to print specific string between word and blank space

My source is on each line 98.194.245.255 - - "GET /disp0201.php?poc=4060&roc=1&ps=R&ooc=13&mjv=6&mov=5&rel=5&bod=155&oxi=2&omj=5&ozn=1&dav=20&cd=&daz=&drc=&mo=&sid=&lang=EN&loc=JPN HTTP/1.1" 302 - "-" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.0.3705; .NET CLR... (5 Replies)
Discussion started by: elamurugu
5 Replies

9. Shell Programming and Scripting

Print a word specific number of times

Hi All, I wanted to know if there is a shell command to print a word n number of times The Input File is : Cat 4 Bat 3 Zall 1 Kite 2 Output File required is : Cat Cat Cat Cat Bat Bat Bat Zall Kite (4 Replies)
Discussion started by: sam_2921
4 Replies

10. Shell Programming and Scripting

Can "sed" substitute word on a specific line?

Hello experts, I know line number of the word I want to replace. Can "sed" substitute word on a specific line? As well, can sed substitute words inside a specific patten. ex. <word>lalala</word> #replace anything between <word> and </word> minifish (2 Replies)
Discussion started by: minifish
2 Replies
Login or Register to Ask a Question