Find and replace a text line within a sed file

Asked

Viewed 1,446 times

0

I have a problem that seems simple but I have problem in the regular expression, I have a file in format ppd that contains settings of a printer installed in my sector, I need to change a row between file tooth by another with the command sed, the biggest problem is that this line that I try to exchange inside the file has special characters of a command, so summarizing I have to replace the whole line by another inside the file, follows below the expression that I tried:

excerpt from the file in which a line needs to be changed:

*DefaultLockedPrintPassword: None

*LockedPrintPassword None/None: ""

*LockedPrintPassword 4001/4001: "%% FoomaticRIPOptionSetting: 

LockedPrintPassword=4001"

*FoomaticRIPOptionSetting LockedPrintPassword=4001: "mark\n&&

(&user;) (20`date +%y%m%d%R | sed 's/://'`) (4001) {secureprint} stopped\n&&

cleartomark\n"
*End

I need to change this line:

(&user;) (20`date +%y%m%d%R | sed 's/://'`) (4001) {secureprint} stopped\n&&

For this:

(&userid;) (20`date +%y%m%d%R | sed 's/://'`) (9999) {secureprint} started\n&&

It would have to do this on several computers, the worst thing that is added to the sed command to change this line it confuses because it treats the line I want to exchange with another command because it is a command that the printer file executes, this file and a linux printer configuration file.

  • You could put 2 sed commands to run in the file. A sed that replaces user with userid and another that replaces 4001 with 9999 is not necessary to create the whole expression. If you really need to modify the whole expression you have to put '' (backslash) before all the special characters.

1 answer

0

Escape of special characters such as bars, crases, & etc.

The following sed command replaces lines starting with (&user) and inserts the new given line:

sed -i "s/^(&user;).*/(\&userid;) (20\`date +%y%m%d%R | sed 's\/:\/\/'\`) (9999) {secureprint} started\\\n\&\&/" /pasta/nome_do_arquivo

obs. : it is necessary to inform the path to the file to be changed.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.