How to remove "~" in those files

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)

LINEDIA000005000000570570~
LIN
EDIA000006000000570570~
LINEDIA000007000000570*570~ // here i want to remove ~ in the looping
procees

sample 2:

LINEDIA000008000000570570~
LIN
EDIA000009000000570570~
LINEDIA000002000000570*570~ // here i want to remove ~ in the looping
procees

for sample1 records:(this is one file)

LINEDIA000005000000570570~
LIN
EDIA000006000000570570~
LINEDIA000007000000570*570~ // here i want to remove ~ in the looping
procees

use gsub ( class String - RDoc Documentation )
with a pattern or whatever will suit u’r needs

procees
Posted via http://www.ruby-forum.com/.

That’s a simple one liner, unless you left out details of the
specification:

$ cat > test.in
abcde~
fghij~
$ ruby -i.bak -ne ‘puts $_.sub(/~$/, “”)’ test.in

-i.ext edits the file in place, making a backup copy of the input files,
appending .ext to the filename. -n loops over the input (in this case
the
file test.in), -e executes a program specified directly on the command
line.
The program itself simply substitutes trailing ~ in the current input
line
($_) with nothing and prints the results.

$ cat test.in
abcde
fghij
$ cat test.in.bak
abcde~
fghij~

However, Ruby really isn’t necessary for this at all:

$ sed ‘s/~$//’ test.in.bak
abcde
fghij
$

HTH,

Felix

On Oct 16, 2007, at 8:50 PM, Ben G. wrote:

Do you want the output to be like the following?

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’

On Oct 17, 2007, at 00:05, John J. wrote:

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

On Oct 16, 2007, at 11:12 PM, Ben G. wrote:

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.

On Oct 16, 2007, at 08:59, Vidya V. wrote:

LINEDIA000008000000570570~
LIN
EDIA000009000000570570~
LINEDIA000002000000570*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?

LINEDIA000005000000570570~
LIN
EDIA000006000000570570~
LINEDIA000007000000570*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

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

Vidya V. wrote:

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.

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

7stud – wrote:

This line of code:

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

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.

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
“~”?

Vidya V. wrote:

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”]

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")