2011-08-18 3 views

답변

0

there과 비슷한 질문을 던지면 스크립트를 함께 사용하는 것이 훨씬 간단합니다.

file_names = ['file1.sql', 'file2.sql'] 

file_names.each do |file_name| 
    text = File.read(file_name) 
    File.open(file_name, 'wb') do 
    |file| 
    file.write(text.gsub(/\s*AUTO_INCREMENT\s*(\=\s*[0-9]+)?/, "")) 
    end 
end 
+0

고마워요! 그것은 완벽하게 작동했습니다! – andrew

0

이것을 위해 Regex를 사용해 보셨나요? 전체 줄을 제거하려면^AUTO_INCREMENT =. + $와 간단히 일치시켜 빈 문자열로 바꿉니다. 그 패턴은 AUTO_INCREMENT로 시작하는 전체 라인과 일치해야합니다. 당신을 위해 작동

Here's a good site to learn Regex if you aren't familiar with it:

희망.

0

사용할 수있는 방법에 대한 자세한 내용은 IO, String, Array을 참조하십시오. 여기

당신이 읽고 수정 한 파일의 내용을 저장할 수있는 방법은 다음과 같습니다 올바른 정규 표현식 주어진 (구문 주어진 가장 올바른을하지 않을 수 있습니다 아래의 하나), 그리고 대답을 감안할 때

# Opens a file for reading. 
file = File.open("file1.txt") 

# Reads all the contents into the string 'contents'. 
contents = file.read 
file.close 

# Splits contents into an array of strings, one for each line. 
lines = contents.split("\n") 

# Delete any lines that start with AUTO_INCREMENT= 
lines.reject! { |line| line =~ /^AUTO_INCREMENT=/ } 

# Join the lines together into one string again. 
new_contents = lines.join("\n") 

# Open file for writing. 
file = File.open("file1.txt", "w") 

# Save new contents. 
file.write(new_contents) 
file.close 
관련 문제