Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
415 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 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...