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:
Removing a block of text in a text file

 



pbs401
New User

Oct 5, 2015, 12:24 PM

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

Hi all,

I have a file that contains the following line:

<dict>
<key>DbName</key>
<string>/Library/Sophos Anti-Virus/Sophos.keychain</string>
<key>GUID</key>
<string>{87191ca3-0fc9-11d4-849a-000502b52122}</string>
<key>SubserviceType</key>
<integer>6</integer>
</dict>


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>
<key>DbName</key>
<string>/Library/Sophos Anti-Virus/Sophos.keychain</string>
<key>GUID</key>
<string>{87191ca3-0fc9-11d4-849a-000502b52122}</string>
<key>SubserviceType</key>
<integer>6</integer>
</dict>
/ /g' ~/Desktop/com.apple.security-common.plist



Any advise,

Thanks in advance,

Abi


BillKSmith
Veteran

Oct 5, 2015, 3:05 PM

Post #2 of 2 (820 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,
Bill

 
 


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

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