Clipboard transformation scripting

Hello all,

I've done a bit of clipboard transformation scripting using xclip before, piping contents with " xclip -o -selection clipboard " to grep, sed, awk, then back into the clipboard with " xclip -i -selection clipboard " ... but I am not a fantastically skilled user of either of the three commands, more of a hack. I'm stumped on this one.

Most of my usage requires scraping clipboard contents and reformatting data for pasting quickly without an intermediary step (such as scraping 3500 lines of html and parsing out 95 links to put into a trouble ticket). This is no different, though it's much less data and just needs to be sorted out to one line for frequent, repetitive pasting.

I have a chunk of text that looks like this:

Customer:         Bobby's Little Widgets, Inc. - 90210 
Ticket Number:    [ChumWorks#: 095959588]
Client:           bobswid-app01

The customer could be any combination of numbers, letters, spaces and symbols of any length, but will always be followed by a hyphen (used in the output) The 5digit number is of no consequence. The Client can be any combination as well, but never contains spaces.

I need to copy one chunk of that, and return a single line that looks like this for pasting:

[ChumWorks#: 095959588] Partial Backup - Bobby's Little Widgets, Inc. - [bobswid-app01]

I would love to do it with xclip / sed (as I'm REALLY green with awk) but whatever gets the job done is what I'll use, and work to understand why it functions properly.

Hello, la2ar0:

sed -n '
/Customer:[[:blank:]]*/ { s//Partial Backup - /; s/-[^-]*$/-/; h; };
/Ticket Number:[[:blank:]]*/ { s///; G; h;};
/Client:[[:blank:]]*/ { s///; s/.*/[&]/; H; g; y/\n/ /; p; q; }'

Welcome to the forum,
Alister

1 Like

That was astoundingly fast and unexpectedly clean, thank you! With all the swapping in and out of hold space though, I'm not sure how to put the brackets around the pattern at the end so it looks as follows:

[bobswid-app01]

Idea? I did study the sed arguments you used, very nice!

Woops. Forgot that bit. I just edited the post. Try it now.

Regards,
Alister

Well, that just saved me a whole lot of clicking every night. :smiley: I think I'll enjoy learning and sharing here.

Here's a cool little grep / sed bit I wrote (with help) to scrape from a massive block of code just exactly the custom linked information I need to paste - it's very efficient and probably easily portable to any source scraping / aggregating usage.

xclip -o -selection clipboard | grep -oE 'ticketid=([0-9]*)' | sed 's/ticketid=/ tix:/g' | while read LINE; do echo '' $LINE ' ';done | xclip -i -selection clipboard 

A bit pedestrian perhaps, but lots of useful things get around on two legs.

Thanks again!