Bash script for editing contents of one file according to other
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
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://.
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.
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.
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
# 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
: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.
Last edited by slakmagik; 02-26-2008 at 03:57 AM.
Reason: cleanup
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.