Taking a file with DHCP host definitions as input I need to transform entries like:
host mx {
fixed-address 10.0.1.161;
into entries like this:
host mx - fixed-address 10.0.1.161;
(obviously I need to output this to stdout
, not replace those entries in place)
sed
doesn't work because it basically doesn't allow replacing newlines.
CodePudding user response:
sed
is able to delete the newline:
$ printf 'host mx {\n fixed-address 10.0.1.161;\n' | sed '/{$/{N; s/{\n */- /; }'
host mx - fixed-address 10.0.1.161;
This command deletes the final {
and the newline on all lines that end in {
, which may not be quite what you want. If the mx
records you are trying to change are multi-line, you'll need to add more logic, and perhaps you want to limit this further with /host mx {^/ ...
, but you would need to add more detail to the question.
CodePudding user response:
With a very short Perl one liner:
$ perl -pe 's/{\n/-/ms' file
host mx - fixed-address 10.0.1.161;
CodePudding user response:
Here is an awk to do what is described:
printf 'host mx {\n fixed-address 10.0.1.161;\n' | awk -F "[ \t]*\\\{" '/^host mx/{
getline l2
sub(/^[ \t]*/,"",l2)
printf "%s - %s\n", $1, l2
}'
Prints:
host mx - fixed-address 10.0.1.161;