Awesome Conferences

Run "sed" on your cut&paste buffer

Two little known Mac OS X commands available from the Unix shell ("Terminal") are pbpaste and pbcopy. They are used for retrieving and sending text to the paste buffer. pbpaste outputs the cut&paste buffer to stdout. phbcopy reads stdin and puts it into the cut&paste buffer.

The more I use these commands the more new ways I find to use them. Read on.

Basic cutting and pasting:

Let's start with the basics.

Reading and writing the cut&paste buffer:

To store something, just send it to pbcopy's stdin.
pbcopy <file.txt

Easy. Right? Now go into TextEdit or MS-Word and paste (CMD-V). The file gets pasted. It's much more accurate than selecting the file from the screen. No worries about missing the first or last line, etc.

Now do the opposite. Select a bunch of text in your favorite application and copy it into your cut&paste buffer. (CMD-C) Want to save it to a file?

pbpaste >newfile.txt
The entire buffer is there. Check it and see.

Paste pipeline into buffer:

Ever been working in the Unix shell and wanted to take the output of one command and paste it into an application? (For example, paste into MS-Word for a book you are writing). That becomes a lot easier:

ls ????-*-IRR.txt | pbcopy
Now the output goes into the paste buffer.

If we want to see the output too, we can either run the command twice (once with the "|pbcopy" and once without) or we can simply add a "tee" command:

ls ????-*-IRR.txt | tee /dev/tty | pbcopy

Here's a longer command line:

ls | tr '[A-Z]' '[a-z']' | sort | pbcopy
Whatever you do on the command line, you can put the output into the cut&paste buffer by ending the line with | pbcopy.

Paste into a pipeline:

What about the other way? Ever have some really great stuff in your cut&paste buffer but wish you could see it sorted?

pbpaste | sort
Here's a better one. How many times have you been looking at a huge Cisco router configuration and wished you could find all references to a certain IP address? I used to copy the configuration into my cut&paste buffer then paste it into NotePad or TextEdit, and use "find". However, sometimes I needed to do a complicate search that could only be done with egrep. Then I had to save the file and grep from the command line. Now its a lot more simple:


pbpaste | grep
or find as a whole word, don't treat "." as a wildcard:
pbpaste | grep -F -w
or maybe we just want to count the number of lines:
pbpaste | grep -F -w | wc -l

"sed" your cut&paste buffer:

Here's my favorite. Today I had the following string in my cut&paste buffer:

access-list 101 permit tcp object-group OSXservers any eq ftp
This is a rule for access list 101 on my PIX firewall. Now what I wanted to do is paste that into access list 202. Sure, I could retype it. I could even paste it without the ENTER at the end of the line, and use my cursor keys to get to to "101" and change it to "202". However, that's a pain and it is also error prone, epecially considering the way a PIX can mishandle command line editing on long lines leaving you without a clue about the real location of your cursor.

Therefore I did this instead:

pbpaste | sed -e 's/ 101 / 202 /g' | pbcopy
To test my work, I looked at the buffer:
$ pbpaste
access-list 202 permit tcp object-group OSXservers any eq ftp
It worked! W00t! Thus I pasted it into my PIX and was on my way.

More info:

The system is smart enough to tell ASCII text from RTF from EPS. The man page has more information.

Posted by Tom Limoncelli in Mac OS X

No TrackBacks

TrackBack URL:

1 Comment | Leave a comment

Great post! Windows also has a similar command to pbcopy, clip.exe.

Do you know of a similar function for *nix? I don't mean xclip or any of the GUI-oriented commands, since sometimes I'm ssh'd into a remote system and don't have X!


Leave a comment