Find and replace matching multiple parameters from list (or another file)

John Smith

I wrote a simple shell script that works but it's terribly inefficient. It takes too long to run on larger files. I'm looking for a faster solution.

Sample input files:

data.csv:

1,data,data
3,data,data
4,data,data
9,data,data
...

matches.txt:

3
9
16
17
...

The script I wrote iterates through each item in matches.txt. It uses sed to match the beginning of the lines in the csv file and comments them out by prepending **.

#!/bin/bash

IFS=$'\r\n' GLOBIGNORE='*' :; XYZ=$(<matches.txt)
for id in ${XYZ[@]}
do
  sed -i '' "${id}s/^**//" data.csv
done

I'm using OS X so sed parameters are slightly different.

anubhava

Rather than calling sed in a loop you can use this awk:

awk -F ',' 'FNR==NR{a[$1]++; next} $1 in a{$0 = "**" $0} 1' matches.txt data.csv
1,data,data
**3,data,data
4,data,data
**9,data,data

To save output from awk:

awk -F ',' 'FNR==NR{a[$1]++; next} $1 in a{$0 = "**" $0} 1' matches.txt data.csv > _tmp
mv _tmp data.csv

Explanation:

  • -F ',' - Use field separator as comma
  • FNR==NR - Execute this block for first file
  • {a[$1]++; next} - Create an array with key as $1 from first file and move to next line
  • $1 in a{$0 = "**" $0} - For the 2nd file if 1st field is in array a then prepend ** in current line.
  • 1 - default awk action (print the line)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Find and replace each instance of a single string with another from a list or file

From Dev

Replace strings in a file with another matching string from another file

From Dev

How to find a matching value in a file and than replace that value in another file?

From Dev

Bash: replace matching regex with nth line from another file

From Dev

Replace lines matching a pattern with lines from another file in order

From Dev

AWK: Find a sentence in one file and replace it with a sentence from another file

From Dev

Find and replace lines in text file with output from another file

From Dev

Script to find particular value from one file and replace it in another file

From Dev

I'd'd like to take a list of strings, find them in another csv file and print a field from the matching line

From Dev

How to replace a matching string in one file with matching string in another file?

From Dev

XSLT "replace" values with another file matching by attribute

From Dev

Replace Matching Row String From Another Column

From Dev

Search for multiple strings (from a file) in another file and list the missing strings

From Dev

Multiple line Find & replace - Notepad++ , regex, copy a text from a particular line and replace in another line

From Dev

Matching lines in file from list

From Dev

Find matching value in nested list from list

From Dev

regex - replace ordered list of lines matching a string with another list

From Dev

Replace SYMBOL in file with SYMBOL in another file based on matching string

From Dev

How to replace matching words defined in one file on another file?

From Dev

Take array of indices, find matching indices in another array and replace values

From Dev

Using sed to find-and-replace in a text file using strings from another text file

From Dev

linux command to find files and replace with another file

From Dev

How to find and replace a list of filenames from a list

From Dev

Find items from List based on matching property

From Dev

Find Nearly Matching Phrase from a List of String

From Dev

find a replace multiple strings in a matching code block using sed

From Dev

Replace text in a file from another file

From Dev

Find and replace with multiple string values in Python List

From Dev

awk replace field value based on matching value in another file

Related Related

  1. 1

    Find and replace each instance of a single string with another from a list or file

  2. 2

    Replace strings in a file with another matching string from another file

  3. 3

    How to find a matching value in a file and than replace that value in another file?

  4. 4

    Bash: replace matching regex with nth line from another file

  5. 5

    Replace lines matching a pattern with lines from another file in order

  6. 6

    AWK: Find a sentence in one file and replace it with a sentence from another file

  7. 7

    Find and replace lines in text file with output from another file

  8. 8

    Script to find particular value from one file and replace it in another file

  9. 9

    I'd'd like to take a list of strings, find them in another csv file and print a field from the matching line

  10. 10

    How to replace a matching string in one file with matching string in another file?

  11. 11

    XSLT "replace" values with another file matching by attribute

  12. 12

    Replace Matching Row String From Another Column

  13. 13

    Search for multiple strings (from a file) in another file and list the missing strings

  14. 14

    Multiple line Find & replace - Notepad++ , regex, copy a text from a particular line and replace in another line

  15. 15

    Matching lines in file from list

  16. 16

    Find matching value in nested list from list

  17. 17

    regex - replace ordered list of lines matching a string with another list

  18. 18

    Replace SYMBOL in file with SYMBOL in another file based on matching string

  19. 19

    How to replace matching words defined in one file on another file?

  20. 20

    Take array of indices, find matching indices in another array and replace values

  21. 21

    Using sed to find-and-replace in a text file using strings from another text file

  22. 22

    linux command to find files and replace with another file

  23. 23

    How to find and replace a list of filenames from a list

  24. 24

    Find items from List based on matching property

  25. 25

    Find Nearly Matching Phrase from a List of String

  26. 26

    find a replace multiple strings in a matching code block using sed

  27. 27

    Replace text in a file from another file

  28. 28

    Find and replace with multiple string values in Python List

  29. 29

    awk replace field value based on matching value in another file

HotTag

Archive