How to copy columns data from your database IDE (DataGrip, Navicat) into Comma-separated values

Mohammad Nazari
Sep 2, 2018 · 2 min read

Ok, What the problem was? many of times, I wanted to copy data from a column and use them as the IN condition part from another query. Here is an example:

I wanted to copy this IDs
and use them as the IN part for querying another table

So, the first solution was to develop such feature in DataGrip IDE, but since I was using another database tools such Navicat. I thought it was good (and fun!) to somehow define another shortcut for copying (or pasting) shortcut to have this feature anywhere I want. At this very first time, I have no clue how to do it, So let’s get our hands dirty.


Perfect, took me 20 minutes to find the solution. Here’s the steps I went thorough:

  • I found a tool that gives me the content of the clipboard. It turns out the best is xclip
  • I converted every new line character with comma using tr famous tool!
  • And finally, I trimmed the last comma using lovely sed

So here’s the command that I got after this step:

xclip -o -selection clipboard | tr '\n' ',' | sed 's/,$//g'

Now just two steps remaining:

  • adding a command for typing the result using xdotool
  • assigning a shortcut key to the script, something like Ctrl+Alt+v

Awesome, so the final version of the script is:

#!/bin/sh
sleep 0.5
xdotool type $(xclip -o -selection clipboard | tr '\n' ',' | sed 's/,$//g')

And finally, assigning it to Ctrl+Alt+V did the magic:

If I had the time, I will definitely make a debian package out of it!

Mohammad Nazari

Written by

Exceptionally ordinary

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade