# Adding columns using gmt math

**URL:** <https://forum.generic-mapping-tools.org/t/adding-columns-using-gmt-math/5937>\
**Category:** Q&A\
**Created:** [May 22, 2025, 1:12pm UTC](https://forum.generic-mapping-tools.org/t/adding-columns-using-gmt-math/5937 "2025-05-22T13:12:04Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![RunningWild](https://avatars.discourse-cdn.com/v4/letter/r/59ef9b/32.png) [@RunningWild](https://forum.generic-mapping-tools.org/u/RunningWild)\
**Post date:** [May 22, 2025, 1:12pm UTC](https://forum.generic-mapping-tools.org/t/adding-columns-using-gmt-math/5937/1 "2025-05-22T13:12:04Z")

</div>

Hi all,

I wanted to try my hand at doing some calculations on csv files using gmt math and I discovered I couldn’t even add two columns together. Naturally I came here to ask the most basic of questions: How do I add column 1 to column 2?

I’ve tried the following:

```auto
gmt begin

	sws=distances_Y.csv
	
	gmt math $sws -C0 $sws -C1 ADD = solution2.txt
	
gmt end

```

This didn’t add column 1 to column 2, instead it pasted the columns one next to another. I can add a column to itself by selecting -C1 twice for example, but I’m trying to do column 1 plus column 2, write result in new column (3)

---

<div class="post-metadata">

**Author:** ![Joaquim](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/joaquim/32/16_2.png) [@Joaquim](https://forum.generic-mapping-tools.org/u/Joaquim)\
**Post date:** [May 22, 2025, 6:19pm UTC](https://forum.generic-mapping-tools.org/t/adding-columns-using-gmt-math/5937/2 "2025-05-22T18:19:07Z")

</div>

I remember a couple of questions similar to this and I think Paul Wessel always said that, no, gmtmath doesn’t do column operations. I personally find gmtmath so complicated when now we have other ways of doing most of what it does but much easier.

---

<div class="post-metadata">

**Author:** ![EvanPi](https://avatars.discourse-cdn.com/v4/letter/e/e47774/32.png) [@EvanPi](https://forum.generic-mapping-tools.org/u/EvanPi)\
**Post date:** [May 23, 2025, 6:11pm UTC](https://forum.generic-mapping-tools.org/t/adding-columns-using-gmt-math/5937/3 "2025-05-23T18:11:19Z")

</div>

I would just use awk.  
`awk -F, '{print $1, $2, $1+$2}' distances_Y.csv > solution2.txt`  
That should put columns 1 and 2 and then add them in the 3rd column

---

<div class="post-metadata">

**Author:** ![timhume](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/timhume/32/296_2.png) [@timhume](https://forum.generic-mapping-tools.org/u/timhume)\
**Post date:** [May 24, 2025, 6:46am UTC](https://forum.generic-mapping-tools.org/t/adding-columns-using-gmt-math/5937/4 "2025-05-24T06:46:59Z")

</div>

Here is how you can almost do it (but not exactly as you want), though personally I’d use the `awk` solution posted by @EvanPi

Suppose your data is in a file with two columns, called `data.txt`:

```auto
1	3
3	9
4	12
2	6

```

You want to add column 0 (first column) and 1 (second column):

```auto
gmt math data.txt 0 COL ADD = twocolsum.txt

```

The file `twocolsum.txt` looks like this:

```auto
1	4
3	12
4	16
2	8

```

You can see the sum of the first two columns of `data.txt` are now in the second column of `twocolsum.txt`. Here is what happened with the `gmt math` command:

1. Use `data.txt` as the tabular data: `gmt math data.txt`
2. Take the first (column 0) column and add it to the stack: `0 COL`
3. Add the contents of the stack to the tabular data: `ADD` Note that this addition is to the second and subsequent columns, `gmt math` treats the first column as a special time column.
4. Write the results to `twocolsum.txt`: `= twocolsum.txt`

Reverse Polish notation forces one to rewire one’s mind after having been taught BEDMAS/BODMAS or whatever for years. However, when one gets the hang of it, it can be quite powerful. Though for your application of adding two columns, honestly use `awk` or similar. @Joaquim is an advocate for Julia language, which is very nice, but would be overkill for this task.

Edit: A friend at university used to always espouse the virtues of RPN. At the time I thought it stood for “randomly processes numbers”. But now, many years later, I’ve got an old HP 11C calculator in a drawer at home. Confession - it stays in the drawer, and I use a bog standard Casio when I still want to use a calculator.

---

<div class="post-metadata">

**Author:** ![timhume](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/timhume/32/296_2.png) [@timhume](https://forum.generic-mapping-tools.org/u/timhume)\
**Post date:** [May 24, 2025, 7:09am UTC](https://forum.generic-mapping-tools.org/t/adding-columns-using-gmt-math/5937/5 "2025-05-24T07:09:26Z")

</div>

Here’s how to do what you want exactly as you want.

```auto
gmt math -N3 data.txt -C2 0 COL ADD 1 COL ADD = foo.txt

```

This works because -N3 specifies three columns. Since the input data only had two columns, the third will start life as a column of zeros. Then the following RPN commands add the values of columns 0 and 1 to the last column.

---

<div class="post-metadata">

**Author:** ![Joaquim](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/joaquim/32/16_2.png) [@Joaquim](https://forum.generic-mapping-tools.org/u/Joaquim)\
**Post date:** [May 24, 2025, 9:33am UTC](https://forum.generic-mapping-tools.org/t/adding-columns-using-gmt-math/5937/6 "2025-05-24T09:33:41Z")

</div>

> [@timhume](#):
>
> @Joaquim is an advocate for Julia language, which is very nice, but would be overkill for this task.

Agree. And that is why I was vague when said “_we have other ways_”. Awk was one the options I had in mind. And the other was ofc GMT.jl. Not for this in particular, but for **everything** GMT related.

---

<div class="post-metadata">

**Author:** ![timhume](https://yyz1.discourse-cdn.com/flex047/user_avatar/forum.generic-mapping-tools.org/timhume/32/296_2.png) [@timhume](https://forum.generic-mapping-tools.org/u/timhume)\
**Post date:** [May 24, 2025, 11:54pm UTC](https://forum.generic-mapping-tools.org/t/adding-columns-using-gmt-math/5937/7 "2025-05-24T23:54:23Z")

</div>

awk supposedly dates back to 1977. I’ve always thought it was under-appreciated - perl became popular, and then languages like Python. But for many things awk is all that is needed.

A long time ago (in the1990s some time) I wrote a program in awk which converted tables of pressure, temperature and dew-point temperature (the temperature at which dew forms if air is cooled - a sort of humidity measure) into tephigrams: [Tephigram - Wikipedia](https://en.wikipedia.org/wiki/Tephigram) (which were of course plotted using our favourite plotting software!) The charts are still on my old university’s website:

 ![202505200000.93112](https://canada1.discourse-cdn.com/flex047/uploads/gmt/original/2X/8/84260e38e94aa1ce0fba7a15a9ec9f9659717937.png)

---

<div class="post-metadata">

**Author:** ![RunningWild](https://avatars.discourse-cdn.com/v4/letter/r/59ef9b/32.png) [@RunningWild](https://forum.generic-mapping-tools.org/u/RunningWild)\
**Post date:** [May 28, 2025, 6:42pm UTC](https://forum.generic-mapping-tools.org/t/adding-columns-using-gmt-math/5937/8 "2025-05-28T18:42:07Z")

</div>

Tim, thank you for this reply (which works) but also your previous reply which walks me through the logic of COL. I’m ok with RPN (not super comfortable, but I understand it enough to appreciate its advantages over infix notation). I think my stumbling block was not knowing the COL function (I was too focused on -C). The other thing was your clever use of -N3 to create a fresh column.

---

<div class="post-metadata">

**Author:** ![RunningWild](https://avatars.discourse-cdn.com/v4/letter/r/59ef9b/32.png) [@RunningWild](https://forum.generic-mapping-tools.org/u/RunningWild)\
**Post date:** [May 28, 2025, 6:47pm UTC](https://forum.generic-mapping-tools.org/t/adding-columns-using-gmt-math/5937/9 "2025-05-28T18:47:19Z")

</div>

Thanks Evan, I’ve checked this and it works. The reason I was asking about gmt math in particular is because I was trying to practice using that module a bit. But I appreciate you showing me an alternative approach, I don’t want you to think I’m being ungrateful.
