Useful Linux command—paste

I have for long ignored blogging, but today I came across the useful linux command “paste” and I thought I should write it down. In the past I have used quite a few smart tricks to handle data or output files, but I failed to document these tricks. I really should do it this time.

So here is the story. I have an output file test.o1551739 which contains lines such as “iter = 125” scattering everywhere. I need to get the sum of all the numbers in these lines. How can I achieve this without writing a program? The following is a little thought process.

First, I am so used to grep that I can grab all such lines by doing:

grep iter test.o1551739

Then, use sed to get rid of the suffix “iter = “:

sed s/"iter = "//

Now that I have a bunch of numbers, one in a line. How can I sum them up? Yah, here comes the usage of paste. The command paste allows me to merge the lines and put a delimiter in between. I chose to use the plus sign, because then it gives me an arithmetic expression. Try this:

paste -sd+

Once I have the arithmetic expression, I’ll just use bc to calculate the result. So piping all these steps, here is the one line command:

grep iter test.o1551739 | sed s/"iter = "// | paste -sd+ | bc

Nice. I realize that there must be tons of ways to do the same job. But I really love my solution.


1 Response to “Useful Linux command—paste”

  1. 1 anthonyvenable110 February 6, 2013 at 5:30 pm

    Reblogged this on anthonyvenable110.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s


Blog Stats

  • 257,555 hits

%d bloggers like this: