0

from this aws config file below, I want to replace the aws region only in default section:

[proj1]
output = json
region = aws_region

[default]
output = json
region = us-east-1

[proj2]
output = json
region = us-east-1

[proj2-frankfurt]
output = json
region = eu-central-1

from this command, I reach to retrieve the line number position of [default] section:

grep -n "default" ~/.aws/config | awk -F ":" '{print $1}'

the result is: 5 (as expected)

so, from this output (line number), I want to replace with sed the (only) first occurrence instead of "aws_region" by "eu-central-1" for example by doing something like that:

sed -i '7,/aws_region/s//eu-central-1/' ~/.aws/config

where 7 is my output from the previous command

How to do this in one operation?

1 Answers1

2

With GNU sed:

sed '/^\[default\]/,/^$/{ s/region = .*/region = foobar/ }' file

Output to stdout:

[proj1]
output = json
region = aws_region

[default]
output = json
region = foobar

[proj2]
output = json
region = us-east-1

[proj2-frankfurt]
output = json
region = eu-central-1

If you want to edit your file "in place" use sed's option -i.


See: man sed and The Stack Overflow Regular Expressions FAQ

Cyrus
  • 69,405
  • 13
  • 65
  • 117