Awk 在取消匹配特定字符串第一次出现之前和最后一次出现之后的所有行时,如何保留行的内容?

Awk 在取消匹配特定字符串第一次出现之前和最后一次出现之后的所有行时,如何保留行的内容?,awk,Awk,我使用的是一个awk脚本,它删除了字符串第一次出现之前和最后一次出现之后的所有行,在我的例子中是“演讲”,删除了中间的任何空行,同时也保留了中间的任何非演讲行 Awk脚本 awk ' /Lecture/{ found=1 } found && NF{ val=(val?val ORS:"")$0 } END{ if(val){ match(val,/.*Lecture [0-9]+/) print substr(val,RSTART

我使用的是一个awk脚本,它删除了字符串第一次出现之前和最后一次出现之后的所有行,在我的例子中是“演讲”,删除了中间的任何空行,同时也保留了中间的任何非演讲行

Awk脚本

awk '
/Lecture/{
  found=1
}
found && NF{
  val=(val?val ORS:"")$0
}
END{
  if(val){
    match(val,/.*Lecture [0-9]+/)
    print substr(val,RSTART,RLENGTH)
  }
}'  1.txt
My Dashboard
Fnfjfjf. random test
00:50

1:01:56
My Notes
No data found.

                                
Change Language                                                                                                                  + English                                                          

Submit


Estimation of Working Capital Lecture 1

Estimation of Working Capital Lecture 2

Estimation of Working Capital Lecture 3
Retain this line 
Money Market Lecture 254

Money Market Lecture 255

Money Market Lecture 256

International Trade Lecture 257

International Trade Lecture 258

International Trade Lecture 259B Some random text gndgnkdbkdlbkmdbmldbm
Terms And Conditions
84749473837373
Random text fifjfofifofjfkfkf
Estimation of Working Capital Lecture 1
Estimation of Working Capital Lecture 2
Estimation of Working Capital Lecture 3
Retain this line
Money Market Lecture 254
Money Market Lecture 255
Money Market Lecture 256
International Trade Lecture 257
International Trade Lecture 258
International Trade Lecture 259B Some random text gndgnkdbkdlbkmdbmldbm
cat 1.txt

awk '
/Lecture/{
  found=1
}
found && NF{
  val=(val?val ORS:"")$0
}
END{
  if(val){
    match(val,/.*Lecture [0-9]+/)
    print substr(val,RSTART,RLENGTH)
  }
}'  1.txt
My Dashboard
Fnfjfjf. random test
00:50

1:01:56
My Notes
No data found.

                                
Change Language                                                                                                                  + English                                                          

Submit


Estimation of Working Capital Lecture 1

Estimation of Working Capital Lecture 2

Estimation of Working Capital Lecture 3
Retain this line 
Money Market Lecture 254

Money Market Lecture 255

Money Market Lecture 256

International Trade Lecture 257

International Trade Lecture 258

International Trade Lecture 259B Some random text gndgnkdbkdlbkmdbmldbm
Terms And Conditions
84749473837373
Random text fifjfofifofjfkfkf
Estimation of Working Capital Lecture 1
Estimation of Working Capital Lecture 2
Estimation of Working Capital Lecture 3
Retain this line
Money Market Lecture 254
Money Market Lecture 255
Money Market Lecture 256
International Trade Lecture 257
International Trade Lecture 258
International Trade Lecture 259B Some random text gndgnkdbkdlbkmdbmldbm
预期产出

awk '
/Lecture/{
  found=1
}
found && NF{
  val=(val?val ORS:"")$0
}
END{
  if(val){
    match(val,/.*Lecture [0-9]+/)
    print substr(val,RSTART,RLENGTH)
  }
}'  1.txt
My Dashboard
Fnfjfjf. random test
00:50

1:01:56
My Notes
No data found.

                                
Change Language                                                                                                                  + English                                                          

Submit


Estimation of Working Capital Lecture 1

Estimation of Working Capital Lecture 2

Estimation of Working Capital Lecture 3
Retain this line 
Money Market Lecture 254

Money Market Lecture 255

Money Market Lecture 256

International Trade Lecture 257

International Trade Lecture 258

International Trade Lecture 259B Some random text gndgnkdbkdlbkmdbmldbm
Terms And Conditions
84749473837373
Random text fifjfofifofjfkfkf
Estimation of Working Capital Lecture 1
Estimation of Working Capital Lecture 2
Estimation of Working Capital Lecture 3
Retain this line
Money Market Lecture 254
Money Market Lecture 255
Money Market Lecture 256
International Trade Lecture 257
International Trade Lecture 258
International Trade Lecture 259B Some random text gndgnkdbkdlbkmdbmldbm
现有脚本中的问题
它工作得几乎很好,但没有保留最后出现的字符串“讲座”行的内容(即最后一行结尾为国际贸易讲座259,而不是国际贸易讲座259B一些随机文本gndgnnkdbkdlbkmdbmldbm。我只希望awk脚本删除所有空行,并删除字符串“讲座”第一次出现之前和最后一次出现之后的所有行。)在不更改任何中间内容和保留任何非讲座行(否则我会使用grep)

的同时,请尝试以下内容。使用显示的示例编写和测试。此外,它的OP的现有代码我已经调整了它的正则表达式,以匹配字符串的最后一次出现
讲座

awk '
/Lecture/{
  found=1
}
found && NF{
  val=(val?val ORS:"")$0
}
END{
  if(val){
    match(val,/.*Lecture[^\n]*/)
    print substr(val,RSTART,RLENGTH)
  }
}'  Input_file

OP代码改进说明:因为OP一直在向名为
val
的变量添加行的值。OP的代码没有选择最后一行,所以我将正则表达式改为选择行,直到字符串
最后一次出现,直到新的行出现,以匹配OP提到的最后一行。

可以请尝试以下内容。使用显示的示例编写和测试。还有它的OP的现有代码,我已经调整了它的正则表达式,以匹配到最后一次出现的字符串
讲座

awk '
/Lecture/{
  found=1
}
found && NF{
  val=(val?val ORS:"")$0
}
END{
  if(val){
    match(val,/.*Lecture[^\n]*/)
    print substr(val,RSTART,RLENGTH)
  }
}'  Input_file

OP代码改进说明:因为OP一直在向名为
val
的变量添加行的值。OP的代码没有选择最后一行,所以我将正则表达式改为选择行,直到字符串
最后一次出现,直到新行出现,以匹配OP提到的最后一行。

alt可采用非平衡的解决方案

awk '
    /Lecture/ {
        seen = 1
        print buffer (buffer != "" ? ORS : "") $0
        buffer = ""
        next
    }
    
    seen && NF {
        buffer = buffer (buffer != "" ? ORS : "") $0
    }
' 1.txt

每当读到的行与
讲座匹配时,就会打印出累计行。可以使用另一种解决方案

awk '
    /Lecture/ {
        seen = 1
        print buffer (buffer != "" ? ORS : "") $0
        buffer = ""
        next
    }
    
    seen && NF {
        buffer = buffer (buffer != "" ? ORS : "") $0
    }
' 1.txt
每当读取的行与
课程
匹配时,就会打印出累计行