1

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!

4

4 に答える 4

1

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.

于 2012-11-01T13:27:45.327 に答える
1

「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」があります)

于 2012-11-01T13:34:29.603 に答える
0

これは、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
于 2012-11-01T13:28:52.870 に答える
0
awk 'NR==1{print $0, "Foo"}NR==5{print $0, "Bar"}'
于 2012-11-01T13:33:40.657 に答える