Skip to content

Instantly share code, notes, and snippets.

@rumaniel
Created August 31, 2023 01:57
Show Gist options
  • Save rumaniel/2291d985472a2d9424dda4623dec457e to your computer and use it in GitHub Desktop.
Save rumaniel/2291d985472a2d9424dda4623dec457e to your computer and use it in GitHub Desktop.
Shell Script for Replace yaml value.
#!/bin/bash
if [ $# -ne 3 ]; then
echo "Usage: $0 <file_path> <field_name> <new_value>"
exit 1
fi
file_path="$1"
field_name="$2"
new_value="$3"
# Find the line number where the field is located
line_number=$(grep -n "$field_name:" "$file_path" | cut -d ':' -f 1)
# Read the file using cat and pipe it to sed to update the value of the field
cat "$file_path" | sed "${line_number}s/:[[:space:]]*.*/: ${new_value}/" > tmp_file.yaml
# Replace the original file with the modified content
mv tmp_file.yaml "$file_path"
echo "File '$file_path' updated with new '$field_name' value with '$new_value'."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment