Visit Our UNIX and Linux User Community


perl cmd to remove the control-Z character at end of 10GB file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting perl cmd to remove the control-Z character at end of 10GB file
# 1  
Old 09-29-2011
perl cmd to remove the control-Z character at end of 10GB file

In a 10-50GB file , at end of file there is Control-z character
tried the below options,
1. perl -p -i -e 's/^Z//g' new.txt
2. perl -0777lwi -032e0 new.txt
and Sed command, dos2unix etc

it takes more time to remove the control-z. need a command or perl program to GO TO LAST LINE OF FILE DIRECTLY AND REMOVE CTRL-Z CHARACTER . we need tat to be removed in less than a second.

it takes in minutes to remove the control-z in 1GB file.
Please help asap. Thanks in advance.
# 2  
Old 09-29-2011
If it's the last byte in the file, just use the truncate system call. Perl provides a wrapper for it. http://perldoc.perl.org/functions/truncate.html.

Regards,
Alister
# 3  
Old 09-29-2011
can anyone help

Can anyone give the entire code(using truncate) to achieve this please.
If any other solution also please let me know thanks.
# 4  
Old 09-30-2011
try this
Code:
 
perl -i -pe '$_=~s/^Z// if eof' filename

# 5  
Old 09-30-2011
See
Code:
perldoc -f truncate

# 6  
Old 09-30-2011
Be careful, it may destroy your file:
Code:
dd bs=filesize-1 seek=1 if=/dev/null of=file

# 7  
Old 09-30-2011
is anything wrong in this?

Code:
#!/usr/bin/perl
use strict;
my $abs;
open(FILE ,"tt.txt");
seek(FILE,-2,2);
read(FILE,$abs,1);
print $abs;
$abs=~s/^Z//g;
print FILE "$abs";
print $abs;
close(FILE);


I want to directly go to end of the file (without reading whole big file)
and remove the ctrl-Z character and write back to file.
No need of one-liners(since it works but takes long time for big file)
Update me please

Last edited by Franklin52; 10-01-2011 at 06:00 AM.. Reason: Corrected code tags

Previous Thread | Next Thread
Test Your Knowledge in Computers #162
Difficulty: Easy
Early versions of MINIX were created by Andrew S. Tanenbaum for commercial purposes.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to Remove comma as last character in end of last line of file?

how to Remove comma as last charector in end of last line of file: example: input file --------------- aaaaaa, bbbbbb, cccc, 12345, ____________ output file : ----------- aaaaaa, bbbbbb, (6 Replies)
Discussion started by: RahulJoshi
6 Replies

2. Shell Programming and Scripting

Remove 3rd character from the end of a random-length string

Hi, I hope someone can share there scripting fu on my problem, I would like to delete the 3rd character from a random length of string starting from the end Example Output Hope you can help me.. Thanks in advance.. (3 Replies)
Discussion started by: jao_madn
3 Replies

3. Shell Programming and Scripting

How to remove new line character at end of file.

I need to remove new line character from end of file. Suppose here are content. a|b|c|d|r a|b|c|d|r a|b|c|d|r <new line> that means file contains 4 lines but data is there in 3 lines. so I want that only 3 lines should be there in file. Please help (20 Replies)
Discussion started by: varun940
20 Replies

4. Shell Programming and Scripting

control M character in unix file

in a file we are getting control character in a file , is there any way that they can be removed once we have the file for eg. BEGIN-PROCEDURE INITIALIZE ^M LET #row_count = 0^M ^M ^M (2 Replies)
Discussion started by: lalitpct
2 Replies

5. Shell Programming and Scripting

Perl use split and remove specific character

i want to split the input by a space and remove specific characters like full stop, comma...... etc. and then save each word in an array. i got something below, but it didn't work. can anyone please help me? Thank you #!/usr/bin/perl -w while (<>) { $line = <>; @word = split(' ',... (6 Replies)
Discussion started by: mingming88
6 Replies

6. Shell Programming and Scripting

How to remove a newline character at the end of filename

Hi All, I have named a file with current date,time and year as follows: month=`date | awk '{print $2}'` date=`date | awk '{print $3}'` year=`date | awk '{print $6}'` time=`date +%Hh_%Mm_%Ss'` filename="test_"$month"_"$date"_"$year"_"$time".txt" > $filename The file is created with a... (2 Replies)
Discussion started by: amio
2 Replies

7. Shell Programming and Scripting

Remove box like special character from end of string

Hi All, How to remove a box like special character which appears at the end of a string/line/record. I have no clue what this box like special character is. It is transparent square like box. This appears in a .DAT file at the end of header. I'm to compare a value in header with a parameter.... (16 Replies)
Discussion started by: Qwerty123
16 Replies

8. Shell Programming and Scripting

Remove parenthesis character (Perl)

Hello, i'm unable to remove the parenthesis character. With $parsed_AsciiName =~ s/\(//; the string is the same And with $parsed_AsciiName =~ s/(//; i retrieve "Unmatched ( in regex; marked by <-- HERE in m/( <-- HERE" Any ideas, please? thank you in advanced. (4 Replies)
Discussion started by: aristegui
4 Replies

9. Shell Programming and Scripting

Perl RegExp to remove last character from strings

I use SAS (a statistical software) and have to remove last character or the last 1/2 numbers that appear after characters from the string using Perl Regular Expression (which is recognized by SAS). Input: f183ii10 f183ii2 f182ii1 f182ii2 f183iim f22ii f22ii11 f22ii12 pmh4 pmhm Desired... (2 Replies)
Discussion started by: ospreyeagle
2 Replies

10. UNIX for Dummies Questions & Answers

Control character in a file

Hi All, I am looking for a solution to capture any ASCII control character in a file ( where the ASCII control character is in decimal value from 0 to 31 and 127 ( Hex value from 00 to 1F and 7F ) ) by returning any affected lines. The intended good file should contain "ASCII printable... (5 Replies)
Discussion started by: cursive
5 Replies

Featured Tech Videos