
Ich möchte eine Variable hinzufügen zuEndeeinesSpezifischZeile in der Datei file.txt
. Mein Code bisher:
#!/bin/bash
read -p "What is the path of the repo? > " input
echo :$input >> file.txt
Ich möchte es hinzufügen zudas EndevonZeile 2. Zum Beispiel,
file.txt
--------
before -
1.stuff
2./home/retep/awesome
after -
1.stuff
2./home/retep/awesome:/home/retep/cool
Antwort1
Es ist ein weiterer Einzeiler für awk:
#!/bin/bash
read -p "What is the path of the repo? > " input
awk -v addition="$input" -v targetline=2 '
NR==targetline { print $0 ":" addition; next}
{print }' file.txt > file.tmp
mv file.tmp file.txt