【问题标题】:How do i remove a specific pattern from a specific lines using bash如何使用 bash 从特定行中删除特定模式
【发布时间】:2019-11-05 02:10:38
【问题描述】:

我想使用 bash 从特定行列表中删除模式 \textcolor{red}{ 并用 } 替换 }} 假设我有一个文件

猫 A.txt

 \ParallelLText{\jverse{5}\textcolor{red}{“Ahut isi inut chili kerik abang chili rik damlo. Alangli chili kerik ahut penang achili ke tovar akungsi klolo, lapen la aphan dongpon long-lo lapen nampi avo atum vangsi chongthok-et-lo.}}{\jverse{5}\textcolor{red}{“A sower went out to sow his seed; and as he sowed, some fell on the path and was trampled on, and the birds of the air ate it up.}}
 \ParallelLText{\jverse{6}\textcolor{red}{Lapen akaprek achili ke longpak athak klolo, lapen lake kechingjok pen mamat chekedulo, pima hadak longle kecham avedet-lo.}}{\jverse{6}\textcolor{red}{Some fell on the rock; and as it grew up, it withered for lack of moisture. }}
 \ParallelLText{\jverse{7}\textcolor{red}{Akaprek achili ke ingsu kethe arong ajosi klolo, lapen ingsu kethe arong la pen the rap-rapsi la aphan petthip-lo.}}{\jverse{7}\textcolor{red}{Some fell among thorns, and the thorns grew with it and choked it.}}
 \ParallelLText{\jverse{8}\textcolor{red}{Jisu halatum aphan thak-lo, “Arnam Arat akepatu alam kechiniji, lake nangtum aphan nangpitang-lo, bonta akaprek atum aphan ke lamlir pensi thanlo. Lasi latum thek longbom setta thek longledetji lapen arjubom setta chini nedetji.” }}{\jverse{8}\textcolor{red}{He said, “To you it has been given to know the secrets of the kingdom of God; but to others I speak in parables, so that‘looking they may not perceive, and listening they may not understand.’}}
 \ParallelLText{\jverse{9}\textcolor{red}{“Bonta Lamlir athe lahelo: Chili ke Arnam alamlo.}}{\jverse{9}\textcolor{red}{“Now the parable is this: The seed is the word of God.}}
 \ParallelLText{\jverse{10}\textcolor{red}{Tovar akung keklo achili ke karjulong atumlo; latum kroikredetsi kejok kelongledetji aphan Diabol vangsi halatum aning arlo pen alam pondet-lo.}}{\jverse{10}\textcolor{red}{The ones on the path are those who have heard; then the devil comes and takes away the word from their hearts, so that they may not believe and be saved.}}

假设我有一个要删除模式的行列表是第 1 行和第 3 行 所以删除和替换模式后我的最终输出将是

 \ParallelLText{\jverse{5}“Ahut isi inut chili kerik abang chili rik damlo. Alangli chili kerik ahut penang achili ke tovar akungsi klolo, lapen la aphan dongpon long-lo lapen nampi avo atum vangsi chongthok-et-lo.}{\jverse{5}“A sower went out to sow his seed; and as he sowed, some fell on the path and was trampled on, and the birds of the air ate it up.}
 \ParallelLText{\jverse{6}\textcolor{red}{Lapen akaprek achili ke longpak athak klolo, lapen lake kechingjok pen mamat chekedulo, pima hadak longle kecham avedet-lo.}}{\jverse{6}\textcolor{red}{Some fell on the rock; and as it grew up, it withered for lack of moisture. }}
 \ParallelLText{\jverse{7}Akaprek achili ke ingsu kethe arong ajosi klolo, lapen ingsu kethe arong la pen the rap-rapsi la aphan petthip-lo.}{\jverse{7}Some fell among thorns, and the thorns grew with it and choked it.}
 \ParallelLText{\jverse{8}\textcolor{red}{Jisu halatum aphan thak-lo, “Arnam Arat akepatu alam kechiniji, lake nangtum aphan nangpitang-lo, bonta akaprek atum aphan ke lamlir pensi thanlo. Lasi latum thek longbom setta thek longledetji lapen arjubom setta chini nedetji.” }}{\jverse{8}\textcolor{red}{He said, “To you it has been given to know the secrets of the kingdom of God; but to others I speak in parables, so that‘looking they may not perceive, and listening they may not understand.’}}
 \ParallelLText{\jverse{9}\textcolor{red}{“Bonta Lamlir athe lahelo: Chili ke Arnam alamlo.}}{\jverse{9}\textcolor{red}{“Now the parable is this: The seed is the word of God.}}
 \ParallelLText{\jverse{10}\textcolor{red}{Tovar akung keklo achili ke karjulong atumlo; latum kroikredetsi kejok kelongledetji aphan Diabol vangsi halatum aning arlo pen alam pondet-lo.}}{\jverse{10}\textcolor{red}{The ones on the path are those who have heard; then the devil comes and takes away the word from their hearts, so that they may not believe and be saved.}}

我试过这个命令,但它不起作用

VAR=\\texcolor{red}{
sed -i "1s/$VAR//;3s/$VAR//" A.txt

我知道如何从所有行中删除模式,但我不知道特定行。

【问题讨论】:

    标签: bash shell sed pattern-matching


    【解决方案1】:

    首先,将字符串存储在不带引号的变量中会导致 shell 在分配变量之前解析值,因此在分配变量时您的反斜杠将已经消失。 sed 需要两个反斜杠来匹配正则表达式中的文字反斜杠。但这里最简单的解决方案可能是根本不使用变量。

    sed 中无法处理多个不相邻的行;但一个简单的解决方法是枚举所有行,如果您在其中任何一行上,则跳转到子例程。

    sed -i '
        1ba 
        3ba
        # Any other line, we are done
        b
    :a
        s/\\textcolor{red}{//
        s/}}/}/' A.txt
    

    sed 语言中,: 声明一个字母数字标签,而b 标签 跳转到该标签。只需b 跳转到脚本的末尾。

    (像往常一样,如果您在 *BSD 平台上,包括 MacOS,您需要 -i '' 和一个强制但可选的空参数。)

    【讨论】:

    • 我使用的是 Ubuntu 14.04,我用你的命令试过它不起作用@tripleee
    • 对不起,现在试试;我从您的代码中复制/粘贴的模式有错字。此外,您的示例在第 2 行和第 4 行有此文本,而不是第 1 和第 3 行。演示:ideone.com/EL2TZT
    • 它正在删除其他替代行,请修改代码。 @tripleee
    • @BikiTeron 我很抱歉;我现在修好了 oug (b vs n)。
    • 非常感谢,现在一切正常了@tripleee
    【解决方案2】:

    尝试使用单引号代替双引号,如下所示:

    sed '1s/\\textcolor{red}{//;3s/\\textcolor{red}{//'
    

    单引号和双引号的规则不同:Difference between single and double quotes in Bash

    双引号将解释反斜杠字符,并在到达 sed 时将 \\ 替换为 \,因此 sed 将看到 \t 而不是 \\t

    $  echo '\\'
    \\
    $ echo "\\"
    \
    

    【讨论】:

    • 这是真的;但是变量中的错字导致它根本不起作用。如果 OP 坚持使用变量,他们应该在赋值中的值周围使用单引号,或者放(我认为)四个反斜杠。那么双引号将是必要的 - 变量不会被替换在单引号内。
    • 你说得对,我的错,感谢您指出这一点。在这种情况下,最佳做法是什么,我应该编辑我的答案还是删除它?
    猜你喜欢
    • 2017-01-04
    • 2016-12-19
    • 2018-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多