How to print first matching range in awk?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to print first matching range in awk?
# 1  
Old 07-04-2011
How to print first matching range in awk?

following is input -

HTML Code:
<Schema>
                                <schema_name>admin</schema_name>
                                <Version>1.1</Version>
                                <schema_name>admin</schema_name>
                                <Version>1.2</Version>
       </Schema>
        <Schema>
                                <schema_name>HR</schema_name>
                                <Version>1.1</Version>
                                <schema_name>HR</schema_name>
                                <Version>1.2</Version>
                                <schema_name>HR</schema_name>
                                <Version>1.3</Version>
        </Schema>
       <Schema>
                                <schema_name>Travel</schema_name>
                                <Version>1.1</Version>
        </Schema>
output should be like below one by one-
HTML Code:
<Schema>
                                <schema_name>admin</schema_name>
                                <Version>1.1</Version>
                                <schema_name>admin</schema_name>
                                <Version>1.2</Version>
       </Schema>
thanks
# 2  
Old 07-04-2011
Code:
awk '/^<Schema>/ {print; f=1; next} f && /<\/Schema>/ {print; f=0} f' infile
<Schema>
                                <schema_name>admin</schema_name>
                                <Version>1.1</Version>
                                <schema_name>admin</schema_name>
                                <Version>1.2</Version>
       </Schema>

# 3  
Old 07-04-2011
Code:
sed '/<\/Schema>/q' file.html

Will print just the first section.
# 4  
Old 07-04-2011
Hi Mirni,

thanks for your post. But after getting this, i need 2nd match. How i'll get that.?

thanks
# 5  
Old 07-04-2011
Code:
# cat tst
<Schema>
                                <schema_name>admin</schema_name>
                                <Version>1.1</Version>
                                <schema_name>admin</schema_name>
                                <Version>1.2</Version>
       </Schema>
        <Schema>
                                <schema_name>HR</schema_name>
                                <Version>1.1</Version>
                                <schema_name>HR</schema_name>
                                <Version>1.2</Version>
                                <schema_name>HR</schema_name>
                                <Version>1.3</Version>
        </Schema>
       <Schema>
                                <schema_name>Travel</schema_name>
                                <Version>1.1</Version>
        </Schema>
# for i in 1 2 3
> do
> echo "RECORD Number $i ..........."
> sed 's:/Schema>:&#:' tst | tr '#' '\n' | nawk -v r="$i" 'BEGIN{RS=""}NR==r'
> done
RECORD Number 1 ...........
<Schema>
                                <schema_name>admin</schema_name>
                                <Version>1.1</Version>
                                <schema_name>admin</schema_name>
                                <Version>1.2</Version>
       </Schema>
RECORD Number 2 ...........
        <Schema>
                                <schema_name>HR</schema_name>
                                <Version>1.1</Version>
                                <schema_name>HR</schema_name>
                                <Version>1.2</Version>
                                <schema_name>HR</schema_name>
                                <Version>1.3</Version>
        </Schema>
RECORD Number 3 ...........
       <Schema>
                                <schema_name>Travel</schema_name>
                                <Version>1.1</Version>
        </Schema>
#

# 6  
Old 07-04-2011
Second match of what exactly? The more specific you are, the better chances you have for finding help.
# 7  
Old 07-04-2011
Avoiding the "for" loop :

Code:
# sed 's:/Schema>:&#:' tst | tr '#' '\n' | nawk 'BEGIN{RS=""}{if(!A[NR]++) print "Record Number " NR " .........."}1'
Record Number 1 ..........
<Schema>
                                <schema_name>admin</schema_name>
                                <Version>1.1</Version>
                                <schema_name>admin</schema_name>
                                <Version>1.2</Version>
       </Schema>
Record Number 2 ..........
        <Schema>
                                <schema_name>HR</schema_name>
                                <Version>1.1</Version>
                                <schema_name>HR</schema_name>
                                <Version>1.2</Version>
                                <schema_name>HR</schema_name>
                                <Version>1.3</Version>
        </Schema>
Record Number 3 ..........
       <Schema>
                                <schema_name>Travel</schema_name>
                                <Version>1.1</Version>
        </Schema>
#

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to print text in field if match and range is met

In the awk below I am trying to match the value in $4 of file1 with the split value from $4 in file2. I store the value of $4 in file1 in A and the split value (using the _ for the split) in array. I then strore the value in $2 as min, the value in $3 as max, and the value in $1 as chr. If A is... (6 Replies)
Discussion started by: cmccabe
6 Replies

2. Shell Programming and Scripting

awk to print out lines that do not fall between range in file

In the awk below I am trying to print out those lines in file2 that are no between $2 and $3 in file1. Both files are tab-delimeted and I think it's close but currently it is printeing out the matches. The --- are not part of the files they are just to show what lines match or fall into the range... (6 Replies)
Discussion started by: cmccabe
6 Replies

3. UNIX for Dummies Questions & Answers

How to print specific range using awk?

I want to print specific range of rows and then its columns using awk command. lets say if a file contain 15 line then i need to print line 5 to 9. awk '{print;for( NR>=5&&NR<=9); do; print "<tr>\n<td>"$1"</td><td>"$2"</td><td>"$3"</td>\n</tr" done}' xyz.csv not sure what's wrong... (9 Replies)
Discussion started by: amyt1234
9 Replies

4. Shell Programming and Scripting

How to print in awk matching $1 values ,to $1,$4 example given.?

Hi Experts, I am trying to get the output from a matching pattern but unable to construct the awk command: file : aa bb cc 11 dd aa cc 33 cc 22 45 68 aa 33 44 44 dd aa cc 37 aa 33 44 67 I want the output to be : ( if $1 match to "aa" start of the line,then print $4 of that line, and... (3 Replies)
Discussion started by: rveri
3 Replies

5. Shell Programming and Scripting

print range of lines matching pattern and previous line

Hi all, on Solaris 10, I'd like to print a range of lines starting at pattern but also including the very first line before pattern. the following doesn't print the range starting at pattern and going down to the end of file: cat <my file> | sed -n -e '/<pattern>{x;p;}/' I need to include the... (1 Reply)
Discussion started by: siriche
1 Replies

6. Shell Programming and Scripting

awk to print range of fields

Hi file.in and file.out are in csv format. the code I have now is, cat file.in | awk -F"," '!($1$2$3$4$5$6$7$8 in a){a;print $0}' > file.out Here, I am printing entire line using $0. however, I want to print $1 to $150 and it should be in csv format. Cut -d is not good in performace.... (3 Replies)
Discussion started by: krishnix
3 Replies

7. Shell Programming and Scripting

AWK Script - Print a column - within a Row Range

Hi, Please read the whole thread. I have been working on this script below. It works fine, feel free to copy and test with the INPUT File below as well. example: PACKET DATA PROTOCOL CONTEXT DATA APNID PDPADD EQOSID VPAA PDPCH PDPTY PDPID 10 ... (6 Replies)
Discussion started by: panapty
6 Replies

8. Shell Programming and Scripting

How to Print from matching word to end using awk

Team, Could some one help me in Printing from matching word to end using awk For ex: Input: I am tester for now I am tester yesterday I am tester tomorrow O/p tester for now tester yesterday tester tomorrow i.e Starting from tester till end of sentence (5 Replies)
Discussion started by: mallak
5 Replies

9. Shell Programming and Scripting

Print lines matching value(s) in other file using awk

Hi, I have two comma separated files. I would like to see field 1 value of File1 exact match in field 2 of File2. If the value matches, then it should print matched lines from File2. I have achieved the results using cut, paste and egrep -f but I would like to use awk as it is efficient way and... (7 Replies)
Discussion started by: SBC
7 Replies

10. Shell Programming and Scripting

Print matching field using awk

Hi All, I have a string like below: str="Hold=True Map=False 'This will map the data' Run=Yes Modify=False" I want to print the field Run=Yes and retrive the value "Yes". I cannot use simple awk command because the position of the "Run" will be different at different times. Is there a way... (6 Replies)
Discussion started by: deepakgang
6 Replies
Login or Register to Ask a Question