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
411 views
in Technique[技术] by (71.8m points)

bash - Cannot paste text variable at line 1 of file using sed on both mac and linux

Out of all the answers to this question I cannot make any of them work.

I need to paste a string at line one of another file.

Here is the code I have tried and the errors I get:

current_date=`date`
version='Version:'

change_details="$version Build number $current_date"

raw_changelog_update=$(cat $short_feature_branch_name)

composite_changelog_update=$(echo -e "

$change_details

==========================================================

$raw_changelog_update

")

echo $composite_changelog_update

sed -i.bak '1i
$composite_changelog_update 
' CHANGELOG.md
# no error but does not paste

sed -i "1i
$composite_changelog_update" CHANGELOG.md
# ERROR: sed: 1: "CHANGELOG.md": invalid command code C

sed -i.bak -e "1i$composite_changelog_update" CHANGELOG.md
# ERROR: Version: Build numb ...": command i expects  followed by text

sed -i -e '1i'$composite_changelog_update'   '$1'' CHANGELOG.md
# sed: 1: "1i": command i expects  followed by text

This needs to work on both OS X and Linux. Also, the new lines in composite_changelog_update are not being respected when I echo this variable.

How can I make this work?

question from:https://stackoverflow.com/questions/65843990/cannot-paste-text-variable-at-line-1-of-file-using-sed-on-both-mac-and-linux

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

1 Reply

0 votes
by (71.8m points)
Waitting for answers

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

...