LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash script for editing contents of one file according to other (https://www.linuxquestions.org/questions/linux-newbie-8/bash-script-for-editing-contents-of-one-file-according-to-other-623687/)

kkpal 02-25-2008 08:26 AM

Bash script for editing contents of one file according to other
 
hi
all

let suppose I have two files: test1 and test2.
I am giving you some part of that files.
test1
<DefaultPrinter kkp>
Info
Location
DeviceURI ipp://192.168.10.22:9100
state Idle
........
.......

and test2
...some text
...... some text....
default_printer pal
printer "pal" {
location
model
......
...
Interface_args {"REMOTE_PORT" = "9100", "REMOTE_HOST" = "192.168.10.251"
}

I want to read next string from default_printer and next string from REMOTE_HOST from test2 file and edit test1 file (by replacing next string from DefaultPrinter and next string from ipp://.

How can I do these things?

slakmagik 02-25-2008 10:06 AM

Code:

#!/bin/bash
dp=$(awk '/default_printer/{ print $2 }' file2)
rh=$(awk -vFS='=' '/REMOTE_HOST/{ gsub(/"/,""); print $3 }' file2)
sed "s/<DefaultPrinter kkp>/$dp/;/DeviceURI/s,ipp://192.168.10.22:9100,ipp:$rh," file1

It uses awk to pull out the two strings wanted and then plugs them into a sed expression which will print them out by substituting parts of the other file.

I wasn't entirely clear on which parts you wanted where, but I think that's it and the principle should work, whatever the details.

slakmagik 02-25-2008 10:29 AM

It occurs to me that posting some output might help let you know if I was in the ballpark or not. (Red stuff is changed.)
Code:

pal
Info
Location
DeviceURI ipp: 192.168.10.251
state Idle
........
.......


kkpal 02-26-2008 01:03 AM

hi

It gives me error:
sed: -e expression #1, char 28: unterminated `s' command

and How to extract "}" from rh.
and I want o/p like:
<DefaultPrinter pal>
DeviceURI ipp: 192.168.10.251:9100

Thanks
KKPal

slakmagik 02-26-2008 01:33 AM

Quote:

Originally Posted by kkpal (Post 3069870)
hi

It gives me error:
sed: -e expression #1, char 28: unterminated `s' command

and How to extract "}" from rh.
and I want o/p like:
<DefaultPrinter pal>
DeviceURI ipp: 192.168.10.251:9100

Thanks
KKPal

I get no error. Sure there wasn't a pasting error?

I'm not sure what you mean about 'extract "}"' but the following changes the '<DefaultPrinter' bit like you say. The DeviceURI you list (192.168.10.251:9100) is what was in the original. If that's what you want, just don't do anything to it.

Code:

:cat kkpal
#!/bin/bash
dp=$(awk '/default_printer/{ print $2 }' file2)
rh=$(awk -vFS='=' '/REMOTE_HOST/{ gsub(/"/,""); print $3 }' file2)
sed "/<DefaultPrinter.*>/s/kkp/$dp/;/DeviceURI/s,ipp://192.168.10.22:9100,ipp:$rh," file1

:./kkpal
<DefaultPrinter pal>
Info
Location
DeviceURI ipp: 192.168.10.251
state Idle
........
.......

Oh - if you mean you want *only* the output of the changed lines, change it to this:

Code:

:cat kkpal
#!/bin/bash
dp=$(awk '/default_printer/{ print $2 }' file2)
rh=$(awk -vFS='=' '/REMOTE_HOST/{ gsub(/"/,""); print $3 }' file2)
sed -n "/<DefaultPrinter.*>/s/kkp/$dp/p;/DeviceURI/s,ipp://192.168.10.22:9100,ipp:$rh,p" file1

:./kkpal
<DefaultPrinter pal>
DeviceURI ipp: 192.168.10.251


kkpal 02-26-2008 02:11 AM

hi Digiot

Thanks for quick reply.

I am giving you full details what I want to do.

I am writing a code for installing printer. For that I am I run "xpdq" and configure my printer. xpdq stores the configuration in its "printrc" file.
According to printrc file I have to edit /etc/cups/printers.conf file.
First I have to read default_printer name and IP address from "printrc" file. And edit the printers.conf file. I have to change DefaultPrinter name and IP address.
I giving you complete files
printers.conf

# Printer configuration file for CUPS v1.1.23
# Written by cupsd on Wed Jul 11 13:08:38 2007
<DefaultPrinter kkp>
Info
Location
DeviceURI ipp://192.168.10.22:9100
State Idle
Accepting Yes
JobSheets none none
QuotaPeriod 0
PageLimit 0
KLimit 0
</Printer>


printrc

# Redefinitions are silently ignored. Be careful to define last the
# choices you want. The order of processing is /etc/pdq/printrc and
# then ~/.printrc


########################################################################
#
# Configurable options
#
#

# Directory to store jobs
#job_dir "~/.printjobs"

# Time (in seconds) for which job files will be saved.
# Jobs files will be cleaned up after new jobs finish.
#job_history_duration 259200
job_history_duration 3600

# Maximum number of times to try to connect to the printer.
#max_send_tries 30

# Delay (in seconds) between attempting to resend
#delay_between_tries 10

# Default printer definition
#default_printer pokey

# Path that gets passed to driver scripts
driver_command_path "/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin"

# Path that gets passed to interface scripts
interface_command_path "/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin"



########################################################################
#
# Extra pieces of this config file, that define drivers and interfaces.
#
#

try_include "/etc/pdq/interfaces/*"
try_include "/etc/pdq/drivers/*/*"

default_printer komalpal




printer "niv" {
# Added by the wizard on Mon Feb 25 21:02:18 2008
location "nivio"
model "hp3390"
driver "generic-postscript"
interface "tcp-port"
driver_opts { }
driver_args {"COPIES" = "1"}
interface_opts { }
interface_args {"REMOTE_PORT" = "9100", "REMOTE_HOST" = "192.168.10.251"}
}


How can I do that ?

slakmagik 02-26-2008 02:46 AM

Code:

:cat kkpal
#!/bin/bash
dp=$(awk '/^default_printer/{ print $2 }' printrc)
rh=$(awk -vFS='=' '/^interface_args/{ gsub(/["} ]/,""); print $3 }' printrc)
sed "
    s,\(^<DefaultPrinter\).*>,\1 $dp>,
    s,\(^DeviceURI ipp:\).*,\1 $rh,
" printers.conf

:./kkpal
# Printer configuration file for CUPS v1.1.23
# Written by cupsd on Wed Jul 11 13:08:38 2007
<DefaultPrinter komalpal>
Info
Location
DeviceURI ipp: 192.168.10.251
State Idle
Accepting Yes
JobSheets none none
QuotaPeriod 0
PageLimit 0
KLimit 0
</Printer>

I changed it in case 'kkp' and '192.168.10.22:9100' change. I see now what you mean about the "}". In the original sample, that wasn't there. Broadening the regex seems to take care of that. If the above works and you have GNU sed, changing 'sed' to 'sed -i~' should do. If not, the usual redirect-and-overwrite.

-- Actually, made a few more changes to make it more consistent and readable, even though there's not much there to read. :)

kkpal 02-26-2008 03:29 AM

hi

this line is not changed
"DeviceURI ipp://192.168.10.22:9100"

I am getting only
"DeviceURI ipp:192.168.10.251"

I need
"DeviceURI ipp://192.168.10.251:9100"

I changed this line
s,\(^DeviceURI// ipp:\).*,\1 $rh:9100,

its working fine.
I want to know I did write thing?

Thanks
KKPal

slakmagik 02-26-2008 03:49 AM

Sorry, I don't even own a printer, but I see what you mean about the port number now. As far as whether you did the right thing, if it works, you did the right thing. :) But it seems that would work as long as it was always 9100. I see now that REMOTE_PORT is also in the interface_args so maybe adding the
Code:

rp=$(awk -vFS='[=,]' '/^interface_args/{ gsub(/["} ]/,""); print $2 }' printrc)
variable and changing the DeviceURI line to
Code:

s,\(^DeviceURI ipp:\).*,\1 $rh:$rp,
would allow that number to vary and still work. Of course, by this time, the entire thing should be rewritten but maybe this will do. ;) If the number won't ever change though, doing what you did is far more efficient.

-- This should be better:
Code:

#!/bin/sh
vars=( $(awk '
BEGIN{ FS="[=, :]" }
/^default_printer/{ dp=$2 }
/^interface_args/{ gsub(/["} ]/,""); rh=$2 }
/^interface_args/{ gsub(/["} ]/,""); rp=$4 }
END{ print dp, rh, rp }' printrc ) )

sed "
    s,\(^<DefaultPrinter\).*>,\1 ${vars[0]}>,
    s,\(^DeviceURI ipp:\).*,\1 ${vars[2]}:${vars[1]},
" printers.conf

This way we just invoke awk once.


All times are GMT -5. The time now is 01:23 AM.