Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search and Insert data

Status
Not open for further replies.

jlguirao

Programmer
Nov 15, 2000
7
0
0
ES
Hello,

I want to make a awk script that given a pattern finds a record on a file and inserts this record after another patter into another given file.

In other words: find record with pattern1 on file1 and insert it into file2 after pattern2.


Any ideas?


Thanks in advance.





 
#
# File: copyit
#
# Purpose: To parse first file for record that contains
# a pattern, capture it in a variable, then,
# parse second file for a pattern and place
# the variable after this record.
#
# Usage: copyit <CR>
#
# Note-
# The first example program adds a copy of the record
# that contains the variable after *all* records in
# second file that contain the matched pattern!
#
# The second version of the program adds a copy of the
# record that contains the variable after *first* record
# found in second file that contains the matched pattern!
#

awk '

FILENAME == &quot;file1&quot; {
if( $0 ~ /acd/ ) foundit = $0
close(&quot;file1&quot;)
}

FILENAME == &quot;file2&quot; {
if( $0 ~ /qqq/ ) {
print
print foundit
} else print

}' file1 file2 > file3



awk '

FILENAME == &quot;file1&quot; {
if( $0 ~ /acd/ ) foundit = $0
close(&quot;file1&quot;)
}

FILENAME == &quot;file2&quot; {
if(( $0 ~ /qqq/ ) && ( !flag )) {
print
print foundit
flag = 1
} else print

}' file1 file2 > file3


# input file1

1234,abc,a12,zx,10
1235,acd,b35,dc,10
1456,cds,c25,fr,10
1290,cds,r45,fx,50



# input file2

1456,678,rrr,4679,10
1234,234,qqq,3456,10
1235,478,ddd,7890,10
1234,234,qqq,3456,10
1290,456,ttt,5867,50
1234,234,qqq,3456,10


Hope this helps!


flogrr
flogr@yahoo.com

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top