#!/bin/bash
##
# Test files...
##
cat <<-'EOF' > file_out_of_date_version__value_modified_inplace
some text
firmware_version 12.3 # This comment is preserved
some more text ...
EOF
cat <<-'EOF' > file_missing_version__value_appended_to_end
some text
some more text ...
EOF
cat <<-'EOF' > file_empty
EOF
cat <<-'EOF' > file_tiny_edge_case__see_if_output_works_for_you
some text
[ Support Matrix ]
firmware_version | 12.* # this line is corrupted, but why would your configuration look like this in the first place?
other_data | "foo"
# actual settings
firmware_version 12.3
some more text ...
EOF
for f in file_does_not_exist file*; do
touch "$f"
awk -i inplace \
-v 'k=firmware_version' \
-v 'v=12.4' \
'$1 == k{f=1;$2=v} 1; ENDFILE {if(!f) print k, v}' "$f"
done
tail -n +1 file*