Linux - Edit multiple files using another file as source

1

I have a file (listname.txt) with multiple names, one per line.

I need to edit or remove all names that are in listanome.txt from various other files

With I guess which files should be dictated, but are many.

grep -Ff listanome.txt /caminho

I'm not able to use the or

    
asked by anonymous 18.07.2018 / 20:03

2 answers

1

Use your index file listanome.txt to make a script to be used by sed to remove rows:

For a listanome.txt as below ...

$ cat listanome.txt 
ze
jao
juca

... the following sed transforms into an exclude script for each name in the list:

$ sed 's/^/\//; s/$/\/d/' listanome.txt
/ze/d
/jao/d
/juca/d

So, pass this command sed to the -f flag of a new sed , which will execute that script on all files to be affected, supposedly contained in the caminho directory:

$ sed --in-place=.bak -f <(sed 's/^/\//; s/$/\/d/' listanome.txt) /caminho/*

This will delete all rows from all files in the caminho directory containing names of listanome.txt , making backups with the extension ".bak "for each changed file.

If your requirement changes, change the first sed according to what you want, for example:

  • To delete all lines identical to the index:

    $ sed 's/^/\/^/; s/$/$\/d/' listanome.txt
    /^ze$/d
    /^jao$/d
    /^juca$/d
    
  • To replace the contents of the string with the "REMOVE" string:

    $ sed 's/^/\/^\.\*/; s/$/\.\*$\/REMOVER\/g/' listanome.txt
    /^.*ze.*$/REMOVER/g
    /^.*jao.*$/REMOVER/g
    /^.*juca.*$/REMOVER/g
    
20.07.2018 / 00:57
0

You can use xargs to accomplish the task you want, here's an example where I've ready a directory and get all the .cfg files and insert a line into the found files:

ls *.cfg | xargs sed -i"23i#####################################################"

understanding the above command better:

ls *.cfg - > lists all files that end with the extension .cfg

| xargs - > takes the data that was displayed in the previous command to apply the following command.

sed -i"23i#####################################################" - > inserts a line in each file found in the command ls , in which case I am inserting in line 23 of each file.

I believe that in your need the ideal would be to create a for you to read each line of your first file and after that apply the necessary action.

    
19.07.2018 / 19:50