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;
}
}
}
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] 삭제
몇 마디 만하겠습니다