Required help to print diff columns for 2 patterns using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Required help to print diff columns for 2 patterns using awk
# 1  
Old 09-25-2012
Required help to print diff columns for 2 patterns using awk

Hi All,
I need help for below scenario :

I have a principals.xml_24092012backup file :
Code:
cat principals.xml_24092012backup
 
   </user>
      <user username="eramire" password="2D393C01720749256303D204826A374D9AE9ABABBF8A">
         <roleMapping rolename="VIEW_EVERYTHING"/>
      </user>
      <user username="dgarza" password="74E07848E16A8D7E8CCFD9C7A55C49F7C6A12EDED5B0">
         <roleMapping rolename="VIEW_EVERYTHING"/>
         <roleMapping rolename="Ebis Read Only"/>
      </user>
      <user username="ecortes" password="11CCE44B88E035556821323F2853AEBFEB6A3DE88088">
         <roleMapping rolename="VIEW_EVERYTHING"/>
      </user>


Below output is getting when I run ->
Code:
cat principals.xml_24092012backup | grep -i -e username -e rolename | awk '{ print " " $2 " " $3 " " $4}' 
  
 username="eramire" password="2D393C01720749256303D204826A374D9AE9ABABBF8A">
 rolename="VIEW_EVERYTHING"/>
 username="dgarza" password="74E07848E16A8D7E8CCFD9C7A55C49F7C6A12EDED5B0">
 rolename="VIEW_EVERYTHING"/>
 rolename="Ebis Read Only"/>
 username="ecortes" password="11CCE44B88E035556821323F2853AEBFEB6A3DE88088">
 rolename="VIEW_EVERYTHING"/>


But I want to print column 2 ($2) for "username" pattern and column 2,3,4 for "rolename" pattern.
So Expected Output would be :
Code:
 username="eramire" 
 rolename="VIEW_EVERYTHING"/>
 username="dgarza" 
 rolename="VIEW_EVERYTHING"/>
 rolename="Ebis Read Only"/>
 username="ecortes" 
 rolename="VIEW_EVERYTHING"/>

Could you please help me out to get above output.
Thanks in advance!

Regards,
Kiran

Last edited by Franklin52; 09-25-2012 at 07:42 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 09-25-2012
Try
Code:
awk '/username/ {print $2} /rolename/ {print $2 $3 $4}' infile
username="eramire"
rolename="VIEW_EVERYTHING"/>
username="dgarza"
rolename="VIEW_EVERYTHING"/>
rolename="EbisReadOnly"/>
username="ecortes"
rolename="VIEW_EVERYTHING"/>

This User Gave Thanks to RudiC For This Post:
# 3  
Old 09-25-2012
Hi Rudi,

Thanks for the update.
command is giving expected output.

Regards,
Kiran
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to print multiple required columns dynamically in a file using the header name?

Hi All, i am trying to print required multiple columns dynamically from a fie. But i am able to print only one column at a time. i am new to shell script, please help me on this issue. i am using below script awk -v COLT=$1 ' NR==1 { for (i=1; i<=NF; i++) { ... (2 Replies)
Discussion started by: balu1234
2 Replies

2. UNIX for Beginners Questions & Answers

How to use "awk" to print columns from different files in separate columns?

Hi, I'm trying to copy and paste the sixth column from a bunch of files into a single file having each column pasted in separate columns (and not one after each other in just one column.) I tried this code but works only partially because it copied and pasted 50 rows of each column... (6 Replies)
Discussion started by: Frastra
6 Replies

3. Shell Programming and Scripting

awk to print before and after lines then count of patterns

What i'm trying to do here is show X amount of lines before and after the string "serialNumber" is found. BEFORE=3 AFTER=2 gawk '{a=$0} {count=0} /serialNumber/ && /./ {for(i=NR-'"${BEFORE}"';i<=NR;i++){count++ ;print a}for(i=1;i<'"${AFTER}"';i++){getline; print ; count ++; print... (5 Replies)
Discussion started by: SkySmart
5 Replies

4. Shell Programming and Scripting

Find matched patterns and print them with other patterns not the whole line

Hi, I am trying to extract some patterns from a line. The input file is space delimited and i could not use column to get value after "IN" or "OUT" patterns as there could be multiple white spaces before the next digits that i need to print in the output file . I need to print 3 patterns in a... (3 Replies)
Discussion started by: redse171
3 Replies

5. Shell Programming and Scripting

Search two patterns using awk to print the variable sum

Coins.txt: gold 1 1986 USA American Eagle gold 1 1908 Austria-Hungary Franz Josef 100 Korona silver 10 1981 USA ingot gold 1 1984 Switzerland ingot gold 1 1979 RSA Krugerrand gold 0.5 1981 RSA Krugerrand gold 0.1 1986 PRC Panda silver 1 1986 USA Liberty dollar gold 0.25 1986 USA Liberty... (2 Replies)
Discussion started by: Ramesh M
2 Replies

6. Shell Programming and Scripting

[Solved] Sed/awk print between patterns the first occurrence

Guys, I am trying the following: i have a log file of a webbap which logs in the following pattern: 2011-08-14 21:10:04,535 blablabla ERROR blablabla bla bla bla bla 2011-08-14 21:10:04,535 blablabla ERROR blablabla bla bla bla ... (6 Replies)
Discussion started by: ppolianidis
6 Replies

7. Shell Programming and Scripting

Sed/awk print between different patterns the first occurrence

Thanks for the help yesterday. I have a little modification today, I am trying the following: i have a log file of a webbap which logs in the following pattern: 2011-08-14 21:10:04,535 blablabla ERROR Exception1 blablabla bla bla bla bla 2011-08-14... (2 Replies)
Discussion started by: ppolianidis
2 Replies

8. Shell Programming and Scripting

awk: Multiple search patterns & print in an one liner

I would like to print result of multiple search pattern invoked from an one liner. The code looks like this but won't work gawk -F '{{if ($0 ~ /pattern1/) pat1=$1 && if ($0 ~ /pattern2/) pat2=$2} ; print pat1, pat2}' Can anybody help getting the right code? (10 Replies)
Discussion started by: sdf
10 Replies

9. Shell Programming and Scripting

Print required values at end of the file by using AWK

I am looking help in awk, quick overview. we will get feed from external system . The input file looks like below. Detail Id Info Id Order Id STATUS Status Date FileDetail 99127942 819718 CMOG223481502 PR 04-17-2011 06:01:34PM... (7 Replies)
Discussion started by: dvrbabu
7 Replies

10. Shell Programming and Scripting

cannot print the columns i want with awk.

hi friends! i have a script where a execute a veritas command, available_media wich retrieves me a list of tapes .lst then i execute cat /tmp/listtapes.lst | grep -v VL |sed '/^$/d'|awk -F, '{print $1, $3, $4, $9} ' > /tmp/media1.lst but it prints all the columns instead of the four... (3 Replies)
Discussion started by: pabloli150
3 Replies
Login or Register to Ask a Question