search file for a string (taken from file) then replace any instance of a different string that falls within the first match

Jdubyas

This one liner will search for Name: 0602 then replace any instance of Type### with type 700 until next Name is reached.

sed '/Name: R0602/,/Name.*$/ s/type .*;/type 700;/' file2

I am looking to use something similar to this one liner, but I want to use list of names in file1 to search file2. Once a string form file1 is found, it then replaces any instance of string2 until it reaches the end of that name's section, then does the same for the next line in file1.

File1

Name: 0602
Name: 0603
Name: 0604

file2:

# Name: R0601
   Container 4 {
  row 12 type 2 {
     set 1 10 {
         name "C4";
         type 300;
     }
     set 11 20 {
         name "C5";
         type 100;
     }
  set 21 20 {
         name "C6";
         type 300;
     }
  set 31 40 {
         name "C7";
         type 200;
     }
      set 31 40 {
         name "C7";
         type 1200;
     }
  }
}

 # Name: R0602
   Container 5 {
  row 12 type 2 {
     set 1 10 {
         name "C4";
         type 300;
     }
     set 11 20 {
         name "C5";
         type 100;
     }
  set 21 20 {
         name "C6";
         type 300;
     }
  set 31 40 {
         name "C7";
         type 300;
     }
      set 31 40 {
         name "C7";
         type 1100;
     }
  }
}


 # Name: R0603
   Container 6 {
  row 12 type 2 {
     set 1 10 {
         name "C4";
         type 200;
     }
     set 11 20 {
         name "C5";
         type 100;
     }
  set 21 20 {
         name "C6";
         type 300;
     }
  set 31 40 {
        name "C7";
        type 500;
     }
      set 31 40 {
        name "C7";
        type 1100;
     }
  }
}
 # Name: R0604
   Container 6 {
  row 12 type 2 {
     set 1 10 {
         name "C4";
         type 200;
     }
     set 11 20 {
         name "C5";
         type 100;
     }
  set 21 20 {
         name "C6";
         type 300;
     }
  set 31 40 {
        name "C7";
        type 500;
     }
      set 31 40 {
        name "C7";
        type 1100;
     }
  }
}
 # Name: R0605
   Container 6 {
  row 12 type 2 {
     set 1 10 {
         name "C4";
         type 200;
     }
     set 11 20 {
         name "C5";
         type 100;
     }
  set 21 20 {
         name "C6";
         type 300;
     }
  set 31 40 {
        name "C7";
        type 500;
     }
      set 31 40 {
        name "C7";
        type 1100;
     }
  }
}
glenn jackman

A short awk script

awk '
  NR == FNR               { names["R"$2]; next }
  $2 == "Name:"           { replace = ($3 in names) }
  $1 == "type" && replace { sub(/type .*/, "type 700;") }
  1
' file1 file2

NR and FNR are builtin awk variables. NR counts the total number of lines seen so far. FNR is the number of lines in the current file seen so far. NR == FNR is an awk idiom that means "I'm working in the first data file" -- the only file for which the current record number would equal the total record number.

So, reading the first file, we want to store the "keys", which are in the 2nd column. Storing them as the key of the "names" associative array is a handy place, given the in operator we use later. I'm adding the letter "R" to the key because the 2nd file has that.

When$2 == "Name:", we are at the top of a section. We want to replace the type values if the 3rd word on this line was seen in the first file. ($3 in names) checks if the 3rd word appears as a key in the names associative array. If it's there, then we will perform the replacements for any subsequent lines where the 1st word is "type".

The last line of the script is interesting. 1 is another idiomatic shorthand to instruct awk to print the current line. awk programs are a series of condition {action} pairs: if the condition is met, perform the given actions. The condition can be omitted, in which case the action is performed for every line. The condition can be given without an action block, in which case the default action is to print the current line. awk treats empty strings and zero as false, so 1 is a condition that is always true. When I'm feeling more verbose, I write {print} instead of 1 to make it more obvious.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Assign values to variable from text file. Search for the string in file. Replace the string in file?

분류에서Dev

How to replace the first blank space, within a string, with a different character?

분류에서Dev

If the first column of file1 matches any string in file2, then replace it with the second column of file1

분류에서Dev

Laravel - Get instance of file from string path

분류에서Dev

Program in Java to read any file and find a substring, and replace it with another string

분류에서Dev

Cannot match string from information parsed from a file

분류에서Dev

Replace string with multiline file content

분류에서Dev

replace a string in the file is overwriting everything

분류에서Dev

Comparing string with string from file

분류에서Dev

How to remove any string from a file via shell scripts?

분류에서Dev

Search a string between two timestamps starting from bottom of log file

분류에서Dev

vim: use string from search in replace command

분류에서Dev

Comparing a number from one file and match it in in a file when it comes inside a string in shell script

분류에서Dev

perl program to replace a string in a file multiple times

분류에서Dev

Replace string in the first column

분류에서Dev

Separate string from dat file

분류에서Dev

Split string from text file

분류에서Dev

How to match strings in file names and rename file according to string?

분류에서Dev

Replace a middle string in .bat file using AutoHotKey without deleting file

분류에서Dev

Search for character in string, within an loop

분류에서Dev

Efficient algorithm to search a buffer for any string from a list

분류에서Dev

Search and replace a word in a string in Scala

분류에서Dev

Replace text in one file and match in second file

분류에서Dev

Shortcut to finding first match in file

분류에서Dev

Replace number in a template file with numbers from a list and output to different files

분류에서Dev

Inject script to play audio file if InnerText does not match string

분류에서Dev

Python regex: How to match a string at the end of a line in a file?

분류에서Dev

what can I use sed to search for a string and append /sub1 and some file ../ before search string

분류에서Dev

Replace the part of the path by string in specific file type in a folder

Related 관련 기사

  1. 1

    Assign values to variable from text file. Search for the string in file. Replace the string in file?

  2. 2

    How to replace the first blank space, within a string, with a different character?

  3. 3

    If the first column of file1 matches any string in file2, then replace it with the second column of file1

  4. 4

    Laravel - Get instance of file from string path

  5. 5

    Program in Java to read any file and find a substring, and replace it with another string

  6. 6

    Cannot match string from information parsed from a file

  7. 7

    Replace string with multiline file content

  8. 8

    replace a string in the file is overwriting everything

  9. 9

    Comparing string with string from file

  10. 10

    How to remove any string from a file via shell scripts?

  11. 11

    Search a string between two timestamps starting from bottom of log file

  12. 12

    vim: use string from search in replace command

  13. 13

    Comparing a number from one file and match it in in a file when it comes inside a string in shell script

  14. 14

    perl program to replace a string in a file multiple times

  15. 15

    Replace string in the first column

  16. 16

    Separate string from dat file

  17. 17

    Split string from text file

  18. 18

    How to match strings in file names and rename file according to string?

  19. 19

    Replace a middle string in .bat file using AutoHotKey without deleting file

  20. 20

    Search for character in string, within an loop

  21. 21

    Efficient algorithm to search a buffer for any string from a list

  22. 22

    Search and replace a word in a string in Scala

  23. 23

    Replace text in one file and match in second file

  24. 24

    Shortcut to finding first match in file

  25. 25

    Replace number in a template file with numbers from a list and output to different files

  26. 26

    Inject script to play audio file if InnerText does not match string

  27. 27

    Python regex: How to match a string at the end of a line in a file?

  28. 28

    what can I use sed to search for a string and append /sub1 and some file ../ before search string

  29. 29

    Replace the part of the path by string in specific file type in a folder

뜨겁다태그

보관