CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



regex2012
User

Apr 18, 2017, 10:11 AM

Post #1 of 5 (859 views)
How to remove single quotes from file Can't Post

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

Post #2 of 5 (848 views)
Re: [regex2012] How to remove single quotes from file [In reply to] Can't Post

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

Post #3 of 5 (839 views)
Re: [regex2012] How to remove single quotes from file [In reply to] Can't Post

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

Post #4 of 5 (823 views)
Re: [Laurent_R] How to remove single quotes from file [In reply to] Can't Post

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

Post #5 of 5 (817 views)
Re: [regex2012] How to remove single quotes from file [In reply to] Can't Post

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.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives