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

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
Removing a block of text in a text file


New User

Oct 5, 2015, 12:24 PM

Post #1 of 2 (920 views)
Removing a block of text in a text file Can't Post

Hi all,

I have a file that contains the following line:

<string>/Library/Sophos Anti-Virus/Sophos.keychain</string>

I like to remove the entire line and save the file.

I tried to use the following perl but it gets error:

perl -pi -e 's/<dict>
<string>/Library/Sophos Anti-Virus/Sophos.keychain</string>
/ /g' ~/Desktop/

Any advise,

Thanks in advance,



Oct 5, 2015, 3:05 PM

Post #2 of 2 (913 views)
Re: [pbs401] Removing a block of text in a text file [In reply to] Can't Post

All of your error messages are the result to a single simple mistake. The slashes used to separate the fields of your substitute command conflict with the slashes in your regular expression. Change your s/.../.../g to s!...!...!g.

Your program will then compile without errors, but still will not do what you intend. This problem is much harder to understand and to fix.

You seem to misunderstand the meaning of the word 'line'. The text that you wish to remove is actually spread over eight 'lines'. You need to remove all of them. This is not a good use for the -p runtime option.

I recommend that you write a new program in a .pl file. It should open your input file. Slurp (undefine $/ or use the module File::Slurp) the entire file into a single string. Remove the unwanted text with the substitute command that you already have. Open the output file. Print the remaining text to it.
Good Luck,


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

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