sh - sed command/shell script to read a specific line and update it if needed -
i have file contents similar below.
name: myname age: 25 subject: math
this file needs updated :
name: myname age: "25" subject: math
but condition is, sed command/ shell script can run multiple times. but, double quotes must added once.
i wrote script , works. want find simpler solution.
#!/bin/bash file="myfile" while ifs='' read -r line || [[ -n "$line" ]]; if [[ $line =~ 'age:' ]] if ! [[ $line =~ 'age: "' ]] sed 's/\(age:[[:blank:]]*\)\(.*\)/\1"\2"/' -i $file fi fi done < $file
you can run sed command line altered regex, , have same effect script
sed -i 's/\(age:[[:blank:]]\+\)\([^"].*\)/\1"\2"/' file
it won't match if first character after blank space double quote, script checks for.
tested , works me.
Comments
Post a Comment