print word after pattern match in two instances


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting print word after pattern match in two instances
# 1  
Old 03-01-2011
print word after pattern match in two instances

i have a file like below. how can i printout the digits followed by the pattern -bwout and -bwin. say i run the script by entering line number 145 (the fourth line), then the o/p should be like
5000000 1024000
Code:
8 test1 -ipprot erp -ppsout 500 -ppsin 500 -bwout 300000 -bwin 300000 -statsdevice test1 -stats 
30 test2 -addr a.b.c.249 -wdwthresh 3 -bwout 512000 -bwin 512000 -statsdevice test2 -stats 
125 test3 -addr a.b.c.0 -addrmsk 255.255.255.0 -bwlink total1 -statsdevice testing3 -stats -global 
145 test4 -group -bwout 1024000 -bwin 5000000 -statsdevice group4 -stats 
1451 test5 -addr a.b.c.251 -bwout 256000 -bwin 512000 -bwlink test4 -statsdevice test5 -stats 
14539 test6 -addr a.b.c.34 -bwboth 128000 -bwlink xyz128 -statsdevice abc123 -stats

did i make myself clear?

tia

Last edited by Franklin52; 03-01-2011 at 04:30 AM.. Reason: Please use code tags, thank you.
# 2  
Old 03-01-2011
Could this help you ?
Code:
awk -v v1=145 '$1 == v1{for(i=1;i<=NF;i++){if(j==1){printf $(i)FS ;j=0};if($i =="-bwout" || $i == "-bwin"){j=1} }printf "\n"}' inputfile

# 3  
Old 03-01-2011
Code:
sed '/^145 /!d;s/^.*-bwout \([^ ]*\).*-bwin \([^ ]*\).*$/\1 \2/' infile

or (to ensure that lines without bwout or bwin are not displayed) :

Code:
sed '/-bwout/!d;/-bwin/!d;/^145 /!d;s/^.*-bwout \([^ ]*\).*-bwin \([^ ]*\).*$/\1 \2/' infile


Last edited by ctsgnb; 03-01-2011 at 05:39 AM..
# 4  
Old 03-01-2011
Code:
sed -n '/^145 / {s/.*-bwout \([0-9]*\).*-bwin \([0-9]*\).*/\2 \1/;p}' infile

# 5  
Old 03-01-2011
Thank you all

What will happen if the line is like below? How to get the same info for number 2255?

Code:
/usr/bwmgr/utils/bwmgr em1 -x 2255 -name radius1 -addr a.b.c.0 -addrmsk 255.255.255.224 -bwout 1024000 -bwin 2048000 -statsdevice radius1 -stats

tia

Last edited by sb245; 03-01-2011 at 07:34 AM.. Reason: code tag
# 6  
Old 03-01-2011
Code:
echo '/usr/bwmgr/utils/bwmgr em1 -x 2255 -name radius1 -addr a.b.c.0 -addrmsk 255.255.255.224 -bwout 1024000 -bwin 2048000 -statsdevice radius1 -stats'|
sed 's/.*-x \([^ ]*\) .*/\1/'

# 7  
Old 03-01-2011
Code:
sed '/-bwout/!d;/-bwin/!d;/ -x 2255 /!d;s/^.*-bwout \([^ ]*\).*-bwin \([^ ]*\).*$/\1 \2/' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match Pattern and print pattern and multiple lines into one line

Hello Experts , require help . See below output: File inputs ------------------------------------------ Server Host = mike id rl images allocated last updated density vimages expiration last read <------- STATUS ------->... (4 Replies)
Discussion started by: tigerhills
4 Replies

2. Shell Programming and Scripting

Print string after the word match

Hi, I have the logs : cat logsx.txt 744906,{"reportingGroups":,"version":"2.0"} 678874,{"reportingGroups":,"version":"2.0"} 193571,{"reportingGroups":,"version":"2.0"} 811537,{"reportingGroups":,"version":"2.0"} 772024,{"reportingGroups":,"version":"2.0"}... (5 Replies)
Discussion started by: justbow
5 Replies

3. Shell Programming and Scripting

Print a word after a match

Hi all, I have the below line: 08 03 * * 1-5 XXXXXXXXXXXXX -ENVI LDNFOUAT10 -EXE xxxxxxxx -CONFIG \${xxxxx} -SUBCLASS RESET -START -EXTRAAPPARGS \" -env 38LDNFOUAT10 \" >> /tmp/SRRC_xxxxxxx_start.log.`/usr/bin/date +\%Y\%m\%d` 2>&1 I want to print just one word after the string "-env", in... (7 Replies)
Discussion started by: Cvg
7 Replies

4. UNIX for Dummies Questions & Answers

Match Pattern after certain pattern and Print words next to Pattern

Hi experts , im new to Unix,AWK ,and im just not able to get this right. I need to match for some patterns if it matches I need to print the next few words to it.. I have only three such conditions to match… But I need to print only those words that comes after satisfying the first condition..... (2 Replies)
Discussion started by: 100bees
2 Replies

5. Shell Programming and Scripting

Script to match a pattern and print only the pattern and after that

Hi, I am writing a shell script to parse some files, and gather data. The data in the files is displayed as below. .......xyz: abz: ...... .......xyz: abz: ..... I have tried using awk and cut, bu the position of these values keep changing, so I can use awk and split it into columns. ... (14 Replies)
Discussion started by: Serena
14 Replies

6. Shell Programming and Scripting

Need one liner to search pattern and print everything expect 6 lines from where pattern match made

i need to search for a pattern from a big file and print everything expect the next 6 lines from where the pattern match was made. (8 Replies)
Discussion started by: chidori
8 Replies

7. Solaris

Error: svcs: Pattern 'pooladm.conf' doesn't match any instances

Hi, I got the following errors during zfs resource pool configuration. Please help. Thanks. # svcs *pool* svcs: Pattern 'pooladm.conf' doesn't match any instances STATE STIME FMRI # svcadm enable system/pools:default # svcs *pool* svcs: Pattern 'pooladm.conf' doesn't match any... (4 Replies)
Discussion started by: aixlover
4 Replies

8. Shell Programming and Scripting

Print all the words after a match word

Hi, I want to print all words till the last word after the match of "ERROR" word. For e.g. I'll get an sqlplus error with e.g. 1 $ ./calltest_fn.ksh var test_var:=test_fn1; calltest_fn.ksh file1 file2 file3 ERROR at line 4: ORA-06550: line 4, column 11: PLS-00201: identifier... (5 Replies)
Discussion started by: dips_ag
5 Replies

9. Shell Programming and Scripting

Search word in a line and print earlier pattern match

Hi All, I have almost 1000+ files and I want to search specific pattern. Looking forwarded your input. Search for: word1.word2 (Which procedure contain this word, I need procedure name in output. Expected output: procedure test1 procedure test2 procedure test3 procedure test4 ... (7 Replies)
Discussion started by: susau_79
7 Replies

10. Shell Programming and Scripting

Replace 1 word after pattern match

Hi, Here is my pattern CREATE USER LZ IDENTIFIED BY VALUES 'A0144280ESD70' DEFAULT TABLESPACE USERS TEMPORARY TABLESPACE TEMP1 PROFILE DEVELOPER_D_1 ACCOUNT UNLOCK / The Sed command must look for the Line that contains TEMPORARY TABLESPACE and replace the immediate word... (4 Replies)
Discussion started by: rajan_san
4 Replies
Login or Register to Ask a Question