How to remove "~" in those files

V

Vidya Vidya

Hi all,

How to remove at end of the record "~", i am trying this, but i
couldn't remove, please let me know if any body knows.
for example , i have 2 files name sample1, sample2,

for sample1 records:(this is one file)

LIN*EDIA000005*000000570*570~
LIN*EDIA000006*000000570*570~
LIN*EDIA000007*000000570*570~ // here i want to remove ~ in the looping
procees

sample 2:


LIN*EDIA000008*000000570*570~
LIN*EDIA000009*000000570*570~
LIN*EDIA000002*000000570*570~ // here i want to remove ~ in the looping
procees
 
B

Ben Giddings

for sample1 records:(this is one file)

LIN*EDIA000005*000000570*570~
LIN*EDIA000006*000000570*570~
LIN*EDIA000007*000000570*570~ // here i want to remove ~ in the
looping
procees

sample 2:


LIN*EDIA000008*000000570*570~
LIN*EDIA000009*000000570*570~
LIN*EDIA000002*000000570*570~ // here i want to remove ~ in the
looping
procees

Do you want to remove all the tilde (~) characters, or just the last
one before "procees"?

Do you want the output to be like the following?

LIN*EDIA000005*000000570*570~
LIN*EDIA000006*000000570*570~
LIN*EDIA000007*000000570*570
procees

If so, and if you have enough space for two of the files, the easiest
way is probably to open an "output" file and read from the input
file, but read ahead slightly, so you know when you're at the next
record, something like:

prev_record = nil
File.open("output.txt", "w") do |out|
File.foreach("records.txt") do |record|
if prev_record
if /procees/.match(line)
out.puts prev_record.gsub(/~$/, "")
else
out.puts prev_record
end
end
prev_record = record
end
out.puts prev_record # since we were one-line behind at the end
end

FileUtils.mv("output.txt", "records.txt")

Ben
 
J

John Joyce

Do you want to remove all the tilde (~) characters, or just the
last one before "procees"?

Do you want the output to be like the following?

LIN*EDIA000005*000000570*570~
LIN*EDIA000006*000000570*570~
LIN*EDIA000007*000000570*570
procees

If so, and if you have enough space for two of the files, the
easiest way is probably to open an "output" file and read from the
input file, but read ahead slightly, so you know when you're at the
next record, something like:

prev_record = nil
File.open("output.txt", "w") do |out|
File.foreach("records.txt") do |record|
if prev_record
if /procees/.match(line)
out.puts prev_record.gsub(/~$/, "")
else
out.puts prev_record
end
end
prev_record = record
end
out.puts prev_record # since we were one-line behind at the end
end

FileUtils.mv("output.txt", "records.txt")

Ben
chop could be useful here.
2 chops and a +

as you iterate each line:
line.chop!.chop! + "\n"

(assuming you still want the newline on each line)

Of course this is a quicka and dirty approach, assuming that every
record ends with '~\n'
 
B

Ben Giddings

as you iterate each line:
line.chop!.chop! + "\n"

You don't want to use chop! unless you're trying to modify line. If
you really wanted to use chop! you'd use it like:

line.chop!
line.chop!
line =+ "\n"

If you want to chain them, you probably want line.chop.chop + "\n".

In any case, it's safer to use the regexp because you don't know if
there's extra whitespace, what the end-of-line chars look like, etc.

Ben
 
J

John Joyce

You don't want to use chop! unless you're trying to modify line.
If you really wanted to use chop! you'd use it like:

line.chop!
line.chop!
line =+ "\n"

If you want to chain them, you probably want line.chop.chop + "\n".

In any case, it's safer to use the regexp because you don't know if
there's extra whitespace, what the end-of-line chars look like, etc.

Ben
Uh, yeah, that's why you should test it.
Regex is great if you want to do all of that. Tend to be more intensive.

Pretty easy to determine EOL characters... that's what's determining
the lines already, OP did refer to lines as 'records'

OP wants to modify the lines.

Lines of something like a log file will be consistent..
but like I said, quick and dirty.
 
V

Vidya Vidya

thanks for reply, actually i want to remove only end of the last record
"~", to each file, here this is my coding, please let me, in this i
should remove the last "~" only.

Dir["#{dirname}/**/**"].each do | thisfile |
@m=@m+1
thisfile.gsub! ( /\// , '\\' )
results.push ( thisfile )
f = File.open(thisfile,"r")
@read_file=f.read()
@split_record=@read_file.split("~") // i am spiliting the
records
@split_record.each do |v|
@split_data=v.split("\*")
// here my inner process
end

end

but i want to remove last sysmbol at the end of the each file, if i
don't remove the "~" means , i couldn't get output as right, otherwise
if you have any idea please let me know
 
7

7stud --

Vidya said:
thanks for reply, actually i want to remove only end of the last record
"~", to each file, here this is my coding, please let me, in this i
should remove the last "~" only.

Dir["#{dirname}/**/**"].each do | thisfile |
@m=@m+1
thisfile.gsub! ( /\// , '\\' )
results.push ( thisfile )
f = File.open(thisfile,"r")
@read_file=f.read()
@split_record=@read_file.split("~")

The last line removes all "~" from the data, so there isn't a '~ ' on
the last line or any other line.
 
V

Vidya Vidya

sorry, i couldn't understood, what you are telling, could you pleas can
tell me clearly?
 
7

7stud --

7stud -- wrote:

This line of code:

removes all '~' characters from @read_file. So, when you ask how to
remove the '~' from the last line of your input, it doesn't make any
sense.

Are you asking how to rewrite the file itself, so that the last line
doesn't end in a '~'? If you want to rewrite the file just so that
split() will return different values, there is a 100.1% chance that
doing that is the wrong solution.
 
V

Vidya Vidya

However, i should remove the "~" at the end of the records, otherwise
its not storing in to the database properly, is there any way to avoid
"~"?
 
7

7stud --

Vidya said:
However, i should remove the "~" at the end of the records, otherwise
its not storing in to the database properly, is there any way to avoid
"~"?

str = "AAA~\nBBB~\nCCC~\nprocees"
arr = str.split("~")

p arr

--output:--
["AAA", "\nBBB", "\nCCC", "\nprocees"]
 
V

Vidya Vidya

Hi, thanks for all your help, i got the answer, just i put \n beside
"~", it worked, really i am wondering how it worked, but i got the
expected answer.

i did like this:

@split_record=@read_file.split("~\n")
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top