Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
948 views
in Technique[技术] by (71.8m points)

awk - Replace string containing _ and double quotes inside a file

Might be a repeat question, but I am kind of stuck: I have a file test.txt with the following contents:

# To push each commit to a remote, put the name of the remote here.
# (eg, "origin" for git). Space-separated lists of multiple remotes
# also work (eg, "origin gitlab github" for git).
PUSH_REMOTE=""

I simply want to replace the line in the file that contains PUSH_REMOTE="" with PUSH_REMOTE="origin". Was looking for a sed or awk syntax to achieve the same. I tried escaping the double quotes using sed -i 's#PUSH_REMOTE=""#"PUSH_REMOTE="origin"#g' test.txt, But I keep getting the error sed: 1: "test.txt": undefined label 'est.txt'.

The desired output (file contents of test.txt) is as follows:

# To push each commit to a remote, put the name of the remote here.
# (eg, "origin" for git). Space-separated lists of multiple remotes
# also work (eg, "origin gitlab github" for git).
PUSH_REMOTE="origin"

Thanks again in advance for your time to address this somewhat basic issue of mine!

Here are my os-release details, in case it helps:

NAME="Red Hat Enterprise Linux"
VERSION="8.3 (Ootpa)"
ID="rhel"
ID_LIKE="fedora"
VERSION_ID="8.3"
PLATFORM_ID="platform:el8"
PRETTY_NAME="Red Hat Enterprise Linux 8.3 (Ootpa)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:redhat:enterprise_linux:8.3:GA"
HOME_URL="https://www.redhat.com/"
BUG_REPORT_URL="https://bugzilla.redhat.com/"

REDHAT_BUGZILLA_PRODUCT="Red Hat Enterprise Linux 8"
REDHAT_BUGZILLA_PRODUCT_VERSION=8.3
REDHAT_SUPPORT_PRODUCT="Red Hat Enterprise Linux"
REDHAT_SUPPORT_PRODUCT_VERSION="8.3"
question from:https://stackoverflow.com/questions/65924633/replace-string-containing-and-double-quotes-inside-a-file

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The error message sed: 1: "test.txt": undefined label 'est.txt' probably means you're using a sed that doesn't have a -i option or, more likely, whose -i option requires a temp file name after it and so it's treating your script as that temp file name and then treating your file name as the script. Either that or you just have a plain old syntax error in your sed script.

This will do what you want using any sed in any shell on every Unix box:

$ sed 's/(PUSH_REMOTE=)""/1"origin"/' file
# To push each commit to a remote, put the name of the remote here.
# (eg, "origin" for git). Space-separated lists of multiple remotes
# also work (eg, "origin gitlab github" for git).
PUSH_REMOTE="origin"

Do whatever you like to update the input file with that output, e.g. either of these:

  • sed -i '...' file, or
  • sed -i '' '...' file, or
  • sed '...' file > tmp && mv tmp file

or whatever.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...