I'm trying to replicate the setup of 50 printers from one server to another server. Command:

lpadmin -p printer_name -v printer_ip -E and some more parameters.

I have the printer names in one text file and the printer IPs in another text file.

Printername.txt contains name of printers in separate lines

Printerip.txt contains ip of same printers in separate lines

I want to pass the printer name and printer ip from these two files as an argument to the command mentioned above.

I know how to do it for 1 parameter i.e by using For i in cat file but I'm not able to do it with two files.

2 Answers 2


You can take one line each from each file using paste:

paste Printername.txt Printerip.txt

Then use awk to insert the option arguments in that:

paste Printername.txt Printerip.txt | awk '{print "-p", $1, "-v", $2, "-E ..."}'

And finally use xargs to use this as arguments for the command:

paste Printername.txt Printerip.txt | awk '{print "-p", $1, "-v", $2, "-E ..."}' | 
  xargs -L1 lpadmin

-L1 lets xargs use one line of input for each run of the command. It will do some splitting, so -p, the printer name etc. are passed as separate arguments. This works best of the printer names don't have whitespace or other special characters in them.

Alternatively, you can use sh with xargs to position the input as arguments:

paste -d '\n' Printername.txt Printerip.txt | xargs -d '\n' -n2 sh -c 'lpadmin -p "$1" -v "$2" -E ...' _

You can use join to join the two files. Then maybe xargs, or for i in ….

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .