LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 02-25-2008, 09:26 AM   #1
kkpal
Member
 
Registered: Oct 2007
Posts: 101

Rep: Reputation: 15
Thumbs up 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?
 
Old 02-25-2008, 11:06 AM   #2
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
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.
 
Old 02-25-2008, 11:29 AM   #3
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
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
........
.......
 
Old 02-26-2008, 02:03 AM   #4
kkpal
Member
 
Registered: Oct 2007
Posts: 101

Original Poster
Rep: Reputation: 15
Thumbs up

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

Last edited by kkpal; 02-26-2008 at 02:06 AM.
 
Old 02-26-2008, 02:33 AM   #5
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
Quote:
Originally Posted by kkpal View Post
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
 
Old 02-26-2008, 03:11 AM   #6
kkpal
Member
 
Registered: Oct 2007
Posts: 101

Original Poster
Rep: Reputation: 15
Thumbs up

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 ?
 
Old 02-26-2008, 03:46 AM   #7
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
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.

Last edited by slakmagik; 02-26-2008 at 03:57 AM. Reason: cleanup
 
Old 02-26-2008, 04:29 AM   #8
kkpal
Member
 
Registered: Oct 2007
Posts: 101

Original Poster
Rep: Reputation: 15
Thumbs up

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

Last edited by kkpal; 02-26-2008 at 04:36 AM.
 
Old 02-26-2008, 04:49 AM   #9
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
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.

Last edited by slakmagik; 02-26-2008 at 05:22 AM. Reason: improved variable-grabbing
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash remove part of a file based on contents of another file bhepdogg Programming 4 01-31-2007 04:13 PM
synchronize contents two files with a bash script paul_mat Linux - Software 5 01-17-2007 07:31 AM
Bash script to compare dir contents Boffy Programming 2 08-02-2005 07:08 AM
get file contents with newlines into bash variable otoomet Linux - Software 2 01-06-2005 02:23 PM
file editing in a bash script Harpune Programming 4 11-23-2002 12:35 AM


All times are GMT -5. The time now is 12:54 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration