简体   繁体   中英

Handling tabs when using sed to edit a file

Continuing to learn bash so bare with me. I am using sed to edit a tftp configuration file for a server within a bash script.

I have added the following line to the bash script

"sed -i -e 's|        server_args             = -v  -s /tftpboot|        server_args             = -v  -s /tftpboot --verbose' /etc/xinetd.d/tftp"

I have replaced the / after 's with | but I am thinking the spaces are creating a problem. When I run the script I get the following error:

-bash: sed -i -e 's|        server_args             = -v  -s /tftpboot|        server_args             = -v  -s /tftpboot --verbose' /etc/xinetd.d/tftp: No such file or directory

The file is there so I was wondering if the spaces may be creating my issue? Again, in a learning mode with Bash. I appreciate the help in advance!

Well I changed the statement to read

sed -i -e 's|        server_args             = -v  -s /tftpboot|        server_args             = -v  -s /tftpboot --verbose|' /etc/xinetd.d/tftp

But when I run it, it does not make a change to the file.

I strongly recommend you don't use -i while learning sed and figuring out your script. Also, if you're going to use some character other than / as the delimiter, don't use a regexp (or backreference) metacharacter like | , use some character that's always literal within a sed command such as : or # to avoid surprises and make your code clearer.

Try this (note that's an upper-case E to enable EREs):

sed -E 's:\s+server_args\s+=\s+-v\s+-s\s+/tftpboot:& --verbose:' /etc/xinetd.d/tftp

If that doesn't work then either the regexp is wrong or you aren't using GNU sed so let us know the output of sed --version .

Once the command produces the output you expect THEN add the `-i argument (if you're using GNU or BSD sed) to update the input file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM