
Я хочу добавить переменную вконецизспецифическийстрока в файле file.txt
. Мой код на данный момент:
#!/bin/bash
read -p "What is the path of the repo? > " input
echo :$input >> file.txt
Я хочу, чтобы это было добавлено вконецизстрока 2. Например,
file.txt
--------
before -
1.stuff
2./home/retep/awesome
after -
1.stuff
2./home/retep/awesome:/home/retep/cool
решение1
Это еще одна однострочная команда для 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