I need to delete the line that matches a word in the first file and also the second file in the sequence. How do I do?

Asked

Viewed 38 times

1

I need to delete an entire line through a word that I will define and at the same time delete another line as well, but now it must be in another file that corresponds to the line of the first file. As in the example below:

1º Arquivo - Nome.txt           2º Arquivo - Sobrenome.txt

Benedito Amaro                  Texeira
Paulo Souza da                  Cunha
Ricardo Ramos                   Faria
Carlos Eduardo                  Neto
Angelita Gusmões                Filho
Davi Onorato                    Favaro

Manually I would delete like this here:

sed -in '/^Ricardo/' Nome.txt ; sed -i '3d' Sobrenome.txt

What I need to know is a means to automate the second command sed, so I can delete the corresponding line that was deleted in the first.

That is, I need to delete two lines one in the first and the other in the second file, both, correspond in the same SEQUENCE and POSITION of the line between the file NAME and SURNAME.

1 answer

1


That is, I need to delete two lines one in the first and the other in the second file, both, correspond in the same sequence of the line between the file NAME and SURNAME.

1 - The ideal would be to take the position of the line that will be deleted from the first file and,

2 - move to a variable (for example: N="4" [number]) for the second command sed $Nd be able to recover the position and perform the task.

Follow the example of how I did:

Code - option: 1

awk - If I need to know the number of the line where the string was found.

C=`awk '{printf"linha ->%d : coluna ->%s\n",NR,index($0,"Ricardo")}' < Nome.txt`
N=`echo "$C" | awk -F '>' '{print $3}' | awk -F '0' '{print $1}'`
sed -in '/^Ricardo/d' Nome.txt ; sed -i "${N}d" Sobrenome.txt

It’s not the most beautiful code. but it’s done. It works.!

I don’t know what will happen in the case of more than one marriage on the same test line there and tell us in the comments. For the purpose of the question this solution is already valid.

Code - option: 2

grep - If I need to know the number of the line where the string was found.

N=`grep -n "Ricardo" Nome.txt | cut -d\: -f1`
sed -in '/^Ricardo/d' Nome.txt ; sed -i "${N}d" Sobrenome.txt

The latter is a minified code, clean and easy to decorate.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.