Home: Perl Programming Help: Beginner:
How to remove single quotes from file



regex2012
User

Apr 18, 2017, 10:11 AM


Views: 1874
How to remove single quotes from file

I have a file that looks like this:

'CORP-LOG-ACCT.txt' and I need to remove the single quotes around the filename. I have tried various regex, including tr but because this is not a string but a file, it does not appear to be working.

I have tried the following but since it is a file it probably won't work.

Code
$string =~ s/'/\\'/g;



The script is creating an empty file here and it puts single quotes around it like 'CORP-LOG-ACCT.txt' Here's that portion of the script:



Code
my %accthash; 
for (@listx)
{
unless (exists $accthash{$_})
{
my $file = "/tmp/accounting/" . $_ . '.txt';
open(my $fh, '>', $file);
#or die "Cannot open file '$file' for writing: $!";
$accthash{$_} = $fh;

}
}


The file 'CORP-LOG-ACCT.txt' is created empty as it should be - it will be filled with data later. But I can't figure out how to remove these single quotes around the file or create the file without them.
Any ideas?


BillKSmith
Veteran

Apr 18, 2017, 1:21 PM


Views: 1863
Re: [regex2012] How to remove single quotes from file

I think you mean that the directory /temp/accounting/ contains a filename which includes quote marks. Such files can be created when a program goes wrong. They may be impossible to access with shell commands because the quote is not a legal filename character. This should not be a problem for perl.

Write a perl script to fix it with perl's built-in rename function. Do not use variables. Use string constants for both the oldname and newname. Be very careful that all quotes mean what you intend.

I do not understand the purpose of your posted code. Do you think it is the culprit that caused the problem or is it your attempt to fix it? Either way, it does not help us any without the relevant hash key.
Good Luck,
Bill


Laurent_R
Veteran / Moderator

Apr 18, 2017, 11:33 PM


Views: 1854
Re: [regex2012] How to remove single quotes from file

I can't see how the program you are showing would create files with quote marks embedded within the name. This should not occur. But the first thing I would suggest is to uncomment the "or die ..." line (and to remove the single quotes around the $file word in it). You might at least get some information if file creation goes wrong.


regex2012
User

Apr 19, 2017, 10:15 AM


Views: 1838
Re: [Laurent_R] How to remove single quotes from file

I uncommented the "die" and it didn't show any error. Then I decided to do this:

Code
@listx; 
my %ndmphash;
for (@listx)
{
# unless (exists $ndmphash{$_})
{
my $file = "/tmp/norris/" . $_ . '.txt';
open(my $fh, '>', $file)
or die "Cannot open file '$file' for writing: $!";
my $name2 = $file;
$name2 =~ s/\CORP//g;
rename($file, $name2);
$ndmphash{$_} = $fh;
}

}

Just to see what would happen. This causes the CORP to be removed from the file, as expected. But when I change this regex to $name2 =~ s/'//g; nothing happens.
Also, I have noticed that I cannot see the single quotes in the file name. But I cannot cat the file without using single quotes.
So listx contains CORP-ACCTING-FIX and this file when the print command is used is CORP-ACCTING-FIX but when I go to cat this file I must do cat 'CORP-ACCTING-FIX' Normally with cat you just specify the filename. ls -la doesn't even show the single quote. Could this be a special character of some kind?


FishMonger
Veteran / Moderator

Apr 19, 2017, 12:16 PM


Views: 1832
Re: [regex2012] How to remove single quotes from file

Sounds like you have a problem with your shell and not the filename. The shell should only require the use of the quotes if you have spaces or some other special character in the filename.

Try specifying the absolute or relative path when issuing the cat command.

Code
cat ./CORP-ACCTING-FIX


How is @listx being populated? Based on what you've shown, I'd expect it to be empty.