我想使用bash从特定行列表中删除模式\textcolor{red}{
并将}}
替换为}
假设我有一个文件
cat 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
我知道如何从所有行中删除模式,但我不知道特定行。
答案 0 :(得分:2)
首先,将字符串存储在未加引号的变量中会导致外壳程序在分配变量之前解析该值,因此在分配变量时,您的一个反斜杠就已经消失了。 sed
需要两个反斜杠以匹配正则表达式中的文字反斜杠。但是,这里最简单的解决方案可能是根本不使用变量。
sed
中无法解决多个不相邻的行;但是一个简单的解决方法是枚举所有行,如果您在任何这些行中,则跳至子例程。
sed -i '
1ba
3ba
# Any other line, we are done
b
:a
s/\\textcolor{red}{//
s/}}/}/' A.txt
在sed
语言中,:
声明一个字母数字标签,而b
label 跳至该标签。只是b
跳到脚本的结尾。
(与往常一样,如果您使用的是* BSD平台,包括MacOS,则需要-i ''
带有一个必填但可以为空的参数。)
答案 1 :(得分:1)
尝试使用单引号而不是双引号:
sed '1s/\\textcolor{red}{//;3s/\\textcolor{red}{//'
单引号和双引号的规则不同:Difference between single and double quotes in Bash
双引号将解释反斜杠字符,并在到达sed时将\\替换为\,因此sed将看到\t
而不是\\t
。
$ echo '\\'
\\
$ echo "\\"
\