gpt4 book ai didi

linux - 如何在一行中搜索模式并打印该行中该模式之后存在的数据

转载 作者:太空宇宙 更新时间:2023-11-04 09:01:24 25 4
gpt4 key购买 nike

我有如下不均匀线条的文件:

jacktyutu/ABC/uyiyi/yyuiyiu/1.java
adhjasgdhg/gsdjgf/ABC/sdfhgsdfj/sdjfhsd/sdfkjhsdkf/2.java
adhjasgdhg/01/ABC/sdfhgs/213/j/sdjfhsd/sdfkjhsdkf/3.java
sd/asd/asd/ABC/sdjg/76/987/4.java

从上面的文件中,我想从每一行中搜索模式 ABC 并打印该行中的其余数据以用于 ex

我的输出应该如下:

uyiyi/yyuiyiu/1.java
sdfhgsdfj/sdjfhsd/sdfkjhsdkf/2.java
sdfhgs/213/j/sdjfhsd/sdfkjhsdkf/3.java
sdjg/76/987/4.java

如何在 shell 脚本中或使用 awksed 实现这一点?

最佳答案

使用GNU Grep:

$ grep -oP "(?<=ABC/).*" file
uyiyi/yyuiyiu/1.java
sdfhgsdfj/sdjfhsd/sdfkjhsdkf/2.java
sdfhgs/213/j/sdjfhsd/sdfkjhsdkf/3.java
sdjg/76/987/4.java

使用awk:

$ awk -F'ABC/' '{print $2}' file
uyiyi/yyuiyiu/1.java
sdfhgsdfj/sdjfhsd/sdfkjhsdkf/2.java
sdfhgs/213/j/sdjfhsd/sdfkjhsdkf/3.java
sdjg/76/987/4.java

使用sed:

$ sed 's%.*ABC/%%' file
uyiyi/yyuiyiu/1.java
sdfhgsdfj/sdjfhsd/sdfkjhsdkf/2.java
sdfhgs/213/j/sdjfhsd/sdfkjhsdkf/3.java
sdjg/76/987/4.java

关于linux - 如何在一行中搜索模式并打印该行中该模式之后存在的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20743760/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com