0

I have files with several lines like:

 ) extent size 256 next size 512 lock mode row;
  ) extent size 512 next size 512 lock mode row;
  ) extent size 256 next size 512 lock mode row;
  ) extent size 512 next size 512 lock mode row;
  ) extent size 256 next size 512 lock mode row;
  ) extent size 512 next size 512 lock mode row;
  ) extent size 512000 next size 48 lock mode row;
  ) extent size 512 next size 512 lock mode row;

I would like to change all this lines to:

) extent size 16 next size 16 lock mode row;

I can get all with:

cat file | grep "extent size" 

But I don't know how to change variable text

Thanks for any help,

SP

  • `grep` can only extract matched lines, it cannot modify them. You can use `sed` as mentioned in the tags, see https://stackoverflow.com/tags/sed/info for resources to get started – Sundeep Nov 27 '19 at 11:43
  • thanks for reply, yes I have used sed on other scripts for replacement with variables, my problem here is how to replace the numbers as they have different values. – Sergio Peres Nov 27 '19 at 17:02

2 Answers2

1

You want to replace all digit chunks with some other number on lines that contain extent size.

You may use the following sed solution:

val=16;
sed "/extent size/{s/[0-9]\{1,\}/$val/g}" file > newfile

Details

  • /extent size/ - finds lines that contain this text
  • {s/[0-9]\{1,\}/$val/g} - on the found line, replaces one or more digits with the val contents (assuming it will be a simple number, no additional processing is required).

See the online sed demo.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
0

If comfortable with awk solution. Try this:-

grep "extent size" filename | awk '$4=$7="16"' 

Explanation

grep "extent size" => Grep the line contain words **"extent size"** 
awk '$4=$7="16"'   => change the value of **column 4 and 7** to 16.
Ravi Saroch
  • 934
  • 2
  • 13
  • 28