Techmago
Techmago

Reputation: 380

Insert string after match in variable

I am trying to make some workaround to solve a problem.

We have a gtk+ program that call a bash script who calls rdesktop. In a machine, we discover that the rdesktop call need on extra parameter... Since i didnt write anything of this code, and i can modify the GTK part of the problem, i can only edit the bash script that make the middle call between the calls.

i have a variable called CMD with something that look like:

rdesktop -x m -r disk:USBDISK=/media -r disk:user=/home/user/ -r printer:HP_Officejet_Pro_8600 -a 16 -u -p -d -g 80% 192.168.0.5

i need to "live edit" this line for when the printer parameter exists, it append ="MS Publisher Imagesetter" after the printer name.

The best i accompplish so far is

ladb@luisdesk ~ $ input="rdesktop -x m -r disk:USBDISK=/media -r disk:user=/home/user/ -r printer:HP_Officejet_Pro_8600 -a 16 -u -p -d -g 80% 192.168.0.5"
ladb@luisdesk ~ $ echo $input | sed s/'printer:.*a /=\"MS Publisher Imagesetter\" '/ 

Which return me:

rdesktop -x m -r disk:USBDISK=/media -r disk:user=/home/user/ -r ="MS Publisher Imagesetter" 16 -u -p -d -g 80% 192.168.0.5

Almost this, but i need to append the string, not replace it.

help?

Edit: i pasted incomplete exemples. fixed

Edit2: With the help of who respond, i end up with

echo "$input" | sed 's/\(printer:\)\([^ ]*\)/\1\2="MS Publisher Imagesetter"/'

Upvotes: 1

Views: 745

Answers (2)

hek2mgl
hek2mgl

Reputation: 157967

You can use this:

sed 's/printer:[^=]\+=/\0 "MS Publisher Imagesetter"/' <<< "$input"

The \0 in the replacement pattern outputs the match itself.

Upvotes: 1

user4401178
user4401178

Reputation:

If you want the output to look like: rdesktop -x m -r disk:USBDISK=/media -r disk:user=/home/user/ -r printer:"HP_Officejet_Pro_8600 MS Publisher Imagesetter" -a 16 -u -p -d -g 80% 192.168.0.5

This sed will do, it matches the printer: part first then the existing printer name and quotes both, if not you can adjust the replacement variables to put the quotes/spacing where you want:

input="rdesktop -x m -r disk:USBDISK=/media -r disk:user=/home/user/ -r printer:HP_Officejet_Pro_8600 -a 16 -u -p -d -g 80% 192.168.0.5"

echo "$input" | sed 's/\(printer:\)\([^ ]*\)/\1"\2 MS Publisher Imagesetter"/' 

output:

rdesktop -x m -r disk:USBDISK=/media -r disk:user=/home/user/ -r printer:"HP_Officejet_Pro_8600 MS Publisher Imagesetter" -a 16 -u -p -d -g 80% 192.168.0.5

Upvotes: 1

Related Questions