假设我有一个模式 - 此内容的file1
48 0 1000
50 0 3000
我要编辑以下内容的和file2:
48 0 1000
bla
...
bla
49 0 2000
bla
...
bla
50 0 3000
bla
...
bla
现在,我想删除
行与模式不匹配
以及以下所有块行,其长度由第三列中的数字给出。
结果是:
48 0 1000
bla
...
bla
50 0 3000
bla
...
bla
和wc -l<结果是4002。
我的想法是通过file2循环,从第一行开始并通过file1循环。如果匹配,则将行标记移到第3列nl = awk 'NR==1{print $3}'
中的数字上,如果不是则删除sed -i< lm,lm + $ nl d' file2的
谢谢。
答案 0 :(得分:1)
您可以使用此命令:
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.
细节:
<style>
.article {
float: left;
margin: 5px;
padding: 5px;
width: 33%; /* Flexible widths */
height: 300px;
border: 1px solid black;
}
</style>
答案 1 :(得分:0)
以下是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