Reputation: 1
I have a bunch of files look like this:
1x 4b
2x 4b
3x 4b
.subckt test xxx
1t 4b
2t 4b
2x 4b
So my question is that how can I replace the 4b
after ".subckt test xxx
" with 8b
and overwrite the original file. And recursively do it for all files in that folder.
Upvotes: 0
Views: 2112
Reputation: 41460
Using awk
:
awk '/.subckt test xxx/ {f=1;print;next} f && $2=="4b" {$2="8b"} 1' orgfile >tmpfile ; mv tmpfile orgfile
Upvotes: 0
Reputation: 204578
find /dir -type f -print |
while IFS= read -r file
do
awk 'f{$2="8b"} /\.subckt test xxx/{f=1} 1' "$file" > /usr/tmp/tmp$$ &&
mv /usr/tmp/tmp$$ "$file"
done
The above won't work for file names with newlines in them so if you have that situation let us know to get a different solution (hint: xargs).
With recent releases of GNU awk you can use -i inplace
to avoid explicitly specifying a temp file name if you prefer.
Upvotes: 0
Reputation: 172758
I would do it like this:
$ sed -e '1,/^\.subckt test xxx$/b' -e 's/ 4b/ 8b/'
This skips (b
) further processing until the characteristic line is encountered. After that, the s
command will perform the substitution in the remaining lines.
To overwrite the original file, use -i
, optionally with a backup file suffix: -i.bak
.
You can pass multiple files (in a folder) to sed; if you want recursion into subdirectories, search for solutions using e.g. find
and xargs
.
Upvotes: 3