Biki Teron
Biki Teron

Reputation: 238

How do i remove a specific pattern from a specific lines using bash

I want to remove a pattern \textcolor{red}{ and replace }} by } from list of specific lines using bash Let say i have a file

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.}}

Let say i have a list of lines to remove the pattern is line 1 and line 3 so my final output after removing and replacing the pattern will be

 \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.}}

I tried with this command but it is not working

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

I know how to delete a pattern from all the lines but i don't know for a specific line.

Upvotes: 0

Views: 66

Answers (2)

tripleee
tripleee

Reputation: 189387

First off, storing the string in an unquoted variable causes the shell to parse the value before assigning the variable, so one of your backslashes will be gone already by the time the variable is assigned. sed needs two backslashes to match a literal backslash in a regex. But the simplest solution here is probably not to use a variable at all.

There is no way in sed to address multiple non-adjacent lines; but a simple workaround is to enumerate all the lines and jump to a subroutine if you are on any of those lines.

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

In the sed language, : declares an alphanumeric label, and b label jumps to that label. Just b jumps to the end of the script.

(As usual, if you are on a *BSD platform, including MacOS, you need -i '' with a mandatory but optionally empty argument.)

Upvotes: 2

Mark
Mark

Reputation: 989

Try using single quotes instead of double quotes like this:

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

The rules for single and double quotes are different: Difference between single and double quotes in Bash

Double quotes will interpret the backslash character and will replace \\ with \ by the time it gets to sed, so sed will see \t instead of \\t.

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

Upvotes: 1

Related Questions