AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / unix / 问题 / 687685
Accepted
Ramón Wilhelm
Ramón Wilhelm
Asked: 2022-01-25 02:20:40 +0800 CST2022-01-25 02:20:40 +0800 CST 2022-01-25 02:20:40 +0800 CST

通过使用带有 SED 行号的列表在某些行中附加单词

  • 772

我有一个包含多行的文件 example.txt:

Larry is funny!
Funny, I've no glue!
Look here!
Tom has no pants.
The underpants are in the drawer.
Pants are in the closet!

创建具有 4 个随机行号的文件后

sed -n '=' example.txt | shuf | head -3 > line_numbers.txt

假设 line_numbers.txt 中的行号包含

1
3
6

我想通过在 line_numbers.txt 的每一行附加单词 WORD 来编辑 example.txt,其中包含完整的单词“pants”(不是像“underpants”这样的部分单词)。

我怎样才能做到这一点?

我希望 example.txt 看起来像这样

Larry is funny!
Funny, I've no glue!
Look here!
Tom has no pants.
The underpants are in the drawer.
Pants are in the closet!WORD

编辑:

要仅查找完整的单词,您必须将 source_word 写为\<source_word\>.

其他可能的例子:

我有另一个文件,其中包含这些行:

I love apples.
You hate pineapples.
Apple pie is delicious.
Why do you do not like eating an apple?
We prefer pears to apples.
How many apples do you want to eat?
I have to bake three apple pies for sunday.

我有一个包含三个随机行号的列表

6
2
4

我只想在每行末尾添加--OK,如果该行包含完整的单词apples。

输出必须如下所示:

I love apples.
You hate pineapples.
Apple pie is delicious.
Why do you do not like eating an apple?
We prefer pears to apples.
How many apples do you want to eat?--OK
I have to bake three apple pies for sunday.
text-processing sed
  • 4 4 个回答
  • 241 Views

4 个回答

  • Voted
  1. Best Answer
    terdon
    2022-01-25T02:43:25+08:002022-01-25T02:43:25+08:00

    我希望也有办法sed,但我个人发现awk这种更复杂的操作更容易:

    awk 'NR==FNR{a[$1]++; next} 
        { 
            s=tolower($0); 
            if(FNR in a && s~/pants/){$0=$0"WORD"}
        }1' line_numbers.txt examples.txt 
    Larry is funny!
    Funny, I've no glue!
    Look here!
    Tom has no pants.
    The underpants are in the drawer.
    Pants are in the closet!WORD
    

    如果你有 GNU awk(gawkLinux 系统上的默认值),你可以这样做来编辑文件:

    gawk -i inplace 'NR==FNR{a[$1]++; print; next} 
                    { 
                        s=tolower($0); 
                        if(FNR in a && s~/pants/){$0=$0"WORD"}
                    }1' line_numbers.txt examples.txt 
    

    或者,如果您不介意丢失以下内容,则稍微简单一些line_numbers.txt:

    gawk -i inplace 'NR==FNR{a[$1]++; next} 
                    { 
                        s=tolower($0); 
                        if(FNR in a && s~/pants/){$0=$0"WORD"}
                    }1' line_numbers.txt examples.txt 
    
    • 4
  2. they
    2022-01-25T02:45:35+08:002022-01-25T02:45:35+08:00

    以下管道是对您的修改,输出sed执行您的编辑的脚本:

    sed -n '=' file | shuf -n 3 | sed 's,$, { /\\<pants\\>/ s/$/WORD/; },'
    

    或者,等效地,

    awk '{ printf "%d { /\\<pants\\>/ s/$/WORD/; }\n", NR }' file | shuf -n 3
    

    例如,这可以生成

    6 { /\<pants\>/ s/$/WORD/; }
    5 { /\<pants\>/ s/$/WORD/; }
    1 { /\<pants\>/ s/$/WORD/; }
    

    如果该模式出现在任何随机选择的行上,则此脚本应用替换,该替换将添加WORD到与该模式匹配的每一行的末尾。pants

    运行它是一个阅读它的问题sed -f:

    sed -n '=' example.txt | shuf -n 3 | sed 's,$, { /\\<pants\\>/ s/$/WORD/; },' |
    sed -i -f /dev/stdin example.txt
    

    或者,使用awk基于我的变体:

    awk '{ printf "%d { /\\<pants\\>/ s/$/WORD/; }\n", NR }' example.txt | shuf -n 3 |
    sed -i -f /dev/stdin example.txt
    

    不需要中间文件。

    替换pants为apples和WORD以--OK解决您更新的查询。

    • 3
  3. user492570
    2022-01-25T07:18:33+08:002022-01-25T07:18:33+08:00

    这是一个oneliner(虽然有点长!)。

    假设您的文本文件是“tfile”并且索引文件是 ifile,那么:

    awk 'BEGIN{i=0;p=0}{if(FNR==NR){a[i++]=$1} else {if(a[p]==FNR){p++;g="";if(index($0,"Pants")){g="WORD"};  print $0,g}else{print $0}}}' ifile tfile
    

    你会得到:

    Larry is funny! 
    Funny, I've no glue!
    Look here! 
    Tom has no pants.
    The underpants are in the drawer.
    Pants are in the closet! WORD 
    
    • 1
  4. guest_7
    2022-01-26T01:13:35+08:002022-01-26T01:13:35+08:00

    使用 GNU sed,我们可以从行号文件构造 sed 代码并将它们应用于数据文件:

    sed -f - <<\eof line_numbers.txt |\
    sed -f - example.txt
      1i /pants/I!b
      s/$/ba/
      $a b;:a;s/$/WORD/
    eof
    

    另一种方法是我们为每个行号转录一个 sed 命令

    sed -e 's:.*:&s/pants.*/\&WORD/I;t:' line_numbers.txt |
    sed -f /dev/stdin example.txt
    
    • 1

相关问题

  • 如何删除两行之间的单行

  • 重新排列字母并比较两个单词

  • 在awk中的两行之间减去相同的列

  • 多行文件洗牌

  • 如何更改字符大小写(从小到大,反之亦然)?同时[重复]

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    模块 i915 可能缺少固件 /lib/firmware/i915/*

    • 3 个回答
  • Marko Smith

    无法获取 jessie backports 存储库

    • 4 个回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    user12345 无法获取 jessie backports 存储库 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl 为什么大多数 systemd 示例都包含 WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve