I currently have a sed command that I want to act on the following type of text:
user:
ensure: 'present'
uid: '666'
gid: '100'
home: '/home/example'
comment: ''
password_max_age: '99999'
password_min_age: '0'
shell: '/bin/false'
password: ''
I can get the type of results I want with this command:
sed '/user:/!b;n;n;n;n;n;n;n;n;n;s/.*/\t\tpassword: \x27\!\!\x27/g'
user:
ensure: 'present'
uid: '666'
gid: '100'
home: '/home/example'
comment: ''
password_max_age: '99999'
password_min_age: '0'
shell: '/bin/false'
password: '!!'
The problem with that command is that user:
is static. I want to be able to iterate through a list of users and use a bash variable instead of a specific user in the sed command. To do that though, I need to use double quotes instead of single quotes. However, when I use this command:
sed "/user:/!b;n;n;n;n;n;n;n;n;n;s/.*/\t\tpassword: \x27\!\!\x27/g"
It complains about the '!' in the !b
sed command I'm using.(since bash is trying to interpret it) However, if I escape it like so:
sed "/user:/\!b;n;n;n;n;n;n;n;n;n;s/.*/\t\tpassword: \x27\!\!\x27/g"
Then I get this error:
sed: -e expression #1, char 6: unknown command: `\'
How can I get this to work?
while read -r i;do sed '/"$i":/!b;n;n;n;n;n;n;n;n;n;s/.*/\t\tpassword: \x27\!\!\x27/g'; done < user.txt
\`, and be careful that your
*` might now be a filename wildcard. A solution withawk
would probably be easier to read, if that's acceptable?/user:/!b;stuff;stuff
, couldn't you just do/user:/{stuff;stuff;}
?