i have a question. I need to add a text line into a txt file. This is my file:
- 000
- 001 test1
- 002 test2
- 003 test3
- 004
- 005 test4
- 006 test5
- 007 test6
I need with bash scripting to add text in line 000 and 004.
How can i do? Thanks to all!
i have a question. I need to add a text line into a txt file. This is my file:
I need with bash scripting to add text in line 000 and 004.
How can i do? Thanks to all!
That's what the sed
and ed
utilities are for. They use the same set of commands with some minor differences. Major difference is that ed
edits a file and takes commands on standard input while sed
takes commands on command-line and edits standard input to standard output.
Using sed
is usually more convenient except when you hit something that it can't do due to it's streaming nature like moving text around.
「sed」ツールを使用して、目標を達成できます。ファイルの操作には非常に強力です。次のようなコマンドを使用できます。
sed -i /your/file.txt -e "s/000/000'\n'YOUR_NEW_LINE/"
sed -i /your/file.txt -e "s/004/004'\n'YOUR_NEW_LINE/"
(私が正しく理解していれば、ファイルの最初の行の先頭に「000」があり、5番目の行に「004」があります)
これは、while/readループを使用してネイティブbashで実行できます。
while read num cmt; do
if [[ -z cmt ]] ; then
printf '%s test%s\n' "$num" "$num"
else
echo "$num $num"
fi
done <infile.txt >outfile.txt
awk 'NR==1{print $0, "Foo"}NR==5{print $0, "Bar"}'