How to add new line in a file

takuma

I want to add newline character below. But the result is wrong. Teach me what is wrong.

test.txt(before)

------------------
2014-09
2014-10
2014-11
------------------

test.txt(after)

------------------
2014-09
2014-10

2014-11
------------------

I make a ruby script below, but the result is wrong.

f = File.open("test.txt","r+")
f.each{|line|
  if line.include?("2014-10")
    f.puts nil 
  end
}
f.close

the result

------------------
2014-09
2014-10

014-11
------------------
Patrick Oscity

Reading and writing a file at the same time can get messy, same thing with other data structures like arrays. You should build a new file as you go along.

Some notes:

  • you should use the block form of File.open because it will stop you from forgetting to call f.close
  • puts nil is the same as puts without arguments
  • single quotes are preferred over double quotes when you don’t need string interpolation
  • you should use do ... end instead of { ... } for multi-line blocks
  • File.open(...).each can be replaced with File.foreach
  • the intermediate result can be stored in a StringIO object which will respond to puts etc.

Example:

require 'stringio'

file = 'test.txt'
output = StringIO.new

File.foreach(file) do |line|
  if line.include? '2014-10'
    output.puts
  else
    output << line
  end
end

output.rewind

File.open(file, 'w') do |f|
  f.write output.read
end

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

add a new line to a delimited file

分類Dev

Read file line by line and add something new to each one

分類Dev

How to add new line in Markdown presentation?

分類Dev

How to add a new line after an expression

分類Dev

How to write in new line in file in Python

分類Dev

How can add the line into etc/sudoers file?

分類Dev

How to add the text before the line in whole file?

分類Dev

How to add interative value to each line of a file?

分類Dev

How to add a new command line option to symfony console

分類Dev

How do you add a new line in an xpath expression?

分類Dev

Yup schema: How to add new line in error message

分類Dev

How to add line numbers to a text file in functional programming (F#)?

分類Dev

How to add an attribute in a tag in XML file by command-line?

分類Dev

How to add new header to jersey client to upload multipart file

分類Dev

Write new line at the end of a file

分類Dev

Every new line add a numbered list UITextView

分類Dev

How to configure code Formatter in Eclipse to add new line after each element in enum?

分類Dev

How can I add a code to copy the first line of excel sheet and paste it on new email body

分類Dev

how to add a line after nth occurrence of a keyword from file 1 to file 2

分類Dev

sed insert line command to add text with new line OSX

分類Dev

How to replace comma with new line

分類Dev

how to create the new line character

分類Dev

How do I insert new line in EditText field while reading the text from a file

分類Dev

How to read data with BufferedReader from a flat file with values seperated by a new line?

分類Dev

How to add line number into each line?

分類Dev

How to add a line break in JavaScript?

分類Dev

How to add HTML tags automatically to different line in a .txt file in sublime text?

分類Dev

add hash to a file using command line

分類Dev

New to java. How to add the item to specific repositories using a text file

Related 関連記事

  1. 1

    add a new line to a delimited file

  2. 2

    Read file line by line and add something new to each one

  3. 3

    How to add new line in Markdown presentation?

  4. 4

    How to add a new line after an expression

  5. 5

    How to write in new line in file in Python

  6. 6

    How can add the line into etc/sudoers file?

  7. 7

    How to add the text before the line in whole file?

  8. 8

    How to add interative value to each line of a file?

  9. 9

    How to add a new command line option to symfony console

  10. 10

    How do you add a new line in an xpath expression?

  11. 11

    Yup schema: How to add new line in error message

  12. 12

    How to add line numbers to a text file in functional programming (F#)?

  13. 13

    How to add an attribute in a tag in XML file by command-line?

  14. 14

    How to add new header to jersey client to upload multipart file

  15. 15

    Write new line at the end of a file

  16. 16

    Every new line add a numbered list UITextView

  17. 17

    How to configure code Formatter in Eclipse to add new line after each element in enum?

  18. 18

    How can I add a code to copy the first line of excel sheet and paste it on new email body

  19. 19

    how to add a line after nth occurrence of a keyword from file 1 to file 2

  20. 20

    sed insert line command to add text with new line OSX

  21. 21

    How to replace comma with new line

  22. 22

    how to create the new line character

  23. 23

    How do I insert new line in EditText field while reading the text from a file

  24. 24

    How to read data with BufferedReader from a flat file with values seperated by a new line?

  25. 25

    How to add line number into each line?

  26. 26

    How to add a line break in JavaScript?

  27. 27

    How to add HTML tags automatically to different line in a .txt file in sublime text?

  28. 28

    add hash to a file using command line

  29. 29

    New to java. How to add the item to specific repositories using a text file

ホットタグ

アーカイブ