0

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.

user000001
  • 32,226
  • 12
  • 81
  • 108

3 Answers3

3

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.

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
0

Using awk:

awk '/.subckt test xxx/ {f=1;print;next} f && $2=="4b" {$2="8b"} 1' orgfile >tmpfile ; mv tmpfile orgfile
slm
  • 15,396
  • 12
  • 109
  • 124
Jotne
  • 40,548
  • 12
  • 51
  • 55
0
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.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185