Reputation: 43
Say I have a pattern - file1 of this content
48 0 1000
50 0 3000
and file2 that I want to edit of the following content:
48 0 1000
bla
...
bla
49 0 2000
bla
...
bla
50 0 3000
bla
...
bla
Now, I want to delete lines that
do not match pattern
and all the following block lines which length is given by number in the third column.
So the result is:
48 0 1000
bla
...
bla
50 0 3000
bla
...
bla
and wc -l < result is 4002.
My idea is to take a loop through file2, start in the first line and take a loop through file1. If match, then move line marker over a number that is in column 3 nl=awk 'NR==1{print $3}'
, if not delete sed -i 'lm,lm+$nl d' file2
Thank you.
Upvotes: 0
Views: 94
Reputation: 43
Here is the code in Fortran:
nr = 0
do
read(1,*,iostat=io) i
if (io/=0) exit
nr = nr + 1
end do
rewind(1)
allocate( I1(nr),I2(nr) )
do j = 1, nr
read(1,*) I1(j), I2(j)
end do
largeN = 10000
nb = 0
do
! Nbl...no. in the third column
read(2,*,iostat=io) int1, int2, Nbl
match = .false.
do j = 1, nr
if ( (int1.eq.I1(j)).and.(int2.eq.I2(j)) ) then
! avoid duplicities, change matched arrays to sth. that is not contained in columns 1 and 2 in file1
I1(j) = largeN
I2(j) = largeN
match = .true.
! output in unit=3
write(3,*) int1, int2, Nbl
end if
end do
if (io/=0) exit
nb = nb + 1
do i = 1, Nbl
read(2,*) x
if(match) write(3,*) x
end do
end do
End
Upvotes: 0
Reputation: 89557
You can use this command:
awk 'NR==FNR{a[$0]=1;next;}/^([[:digit:]]+[[:blank:]]+){2}[[:digit:]]+$/{f=a[$0]}f' file1 file2
details:
NR==FNR { # when the first file is processed
a[$0]=1 # store each line in an array
next # jump to the next line
}
# file2: if a line has the format
/^([[:digit:]]+[[:blank:]]+){2}[[:digit:]]+$/ {
f=a[$0] # then set the flag f to a[$0] (1 if it exists, 0 if not)
}
f # when f is set to 1, print the line.
Upvotes: 1