0

Given some data like so:

"Something something","1234567890123456","12/34/5678","Other thing","123",""

I'm trying to get awk to print the fields and then export them to the X clipboard using xclip with the following:

echo $data | sed 's/"//g' | awk -F',' '{for(i=1;i<NF;i++) {print $i; system("echo \"" $i "\" | xclip -i")}}'

which correctly gives the right output:

Something something
1234567890123456
12/34/5678
Other thing
123

but it seems to only copy the last field to the clipboard. I am using a clipboard manager (klipper) so I would expect there to be all of the fields listed.

What am I missing?

4
  • You are running xclip -i once for each line of input. Is xclip -i incremental or does each run replace the clipboard contents? Did you mean to us | xclip -i on the output from awk? Commented Jan 26, 2016 at 23:08
  • your awk command will not run. the for loop syntax is wrong(for i=1;i<NF;i++) Commented Jan 26, 2016 at 23:34
  • You have several issues in your awk script unrelated to your xclip issue. Read the book Effective Awk Programming, 4th Edition, by Arnold Robbins if you are going to be using awk.
    – Ed Morton
    Commented Jan 26, 2016 at 23:37
  • @wxl it should be for(i=1;i<NF;i++) not (for i=1;i<NF;i++) Commented Jan 27, 2016 at 4:58

2 Answers 2

0

If you are trying to copy the fields to the clipboard with newlines between each value, then consider this:

awk -F',' '
  { s=$1; for (i=2;i<NF;i++) {s=s "\n"$i; }
    print s;
    system("echo \"" s "\" | xcopy -i");
  }'
1
  • with the clipboard manager, i can copy something, copy something else, copy a third thing and they're all on the clipboard manager, with the last thing being the active paste. so what i need is not newlines delimited text in a single copy to the clipboard, but separate ones.
    – wxl
    Commented Jan 27, 2016 at 4:04
0

This is not an awk issue at all. Apparently, xclip can't handle the speed of the input its receiving. Similarly, for i in {1..3}; do echo $i | xclip -i; done fails in just the same way. Adding a sleep after the call to xclip will solve the problem. Based on my experiments, there will need to be 0.1 seconds per item, so for 6 fields shown, you would need:

echo $data | sed 's/"//g' | awk -F',' '{for(i=1;i<NF;i++) {print $i; system("echo \"" $i "\" | xclip -i; sleep 0.6")}}'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.