How to split table when value of a column changes?

I have a single table with 3 column (Long Lat Line) like this:

-50 50 A
-50 45 A
-50 40 A
-40 50 B
-40 45 B
-40 40 B

How could I get a multisegment table (or different tables) like this?

-50 50
-50 45
-50 40
>
-40 50
-40 45
-40 40

Interesting problem; I’m also curious about this.

Unfortunately I don’t have an answer, but this reminds me of a related problem I had with splitting a text file into multiple files based on context lines. Maybe it can trigger some thoughts.

I had a file,contours, like this (from gmt):

>
462365 3685357
857483 4869490
576857 4657687
645342 6879576
>
657483 7584930
657483 3746584
....

And wanted each segment (separated by >) to be in individual files.

Here I could use csplit(1):

$ csplit --elide-empty-files --quiet --prefix=split_ contours "/>/0" "{*}"

$ ls
contours  split_00  split_01

$ cat split_00
>
462365 3685357
857483 4869490
576857 4657687
645342 6879576

$ cat split_01
>
657483 7584930
657483 3746584
....

That is a UNIX job; something like

rm -f lines.txt
for line in A B  E; do
    echo "> Line $line >> lines.txt
    grep $line >> lines.txt
done

If there are lots of lines you can use a list with the line ids instead, and if lines sometimes are numerical then maybe do egrep “${line}$” instead.

1 Like

See gmt convert -D.

1 Like

Beautiful.

For the record. Thanks Paul

It creates a unique list of names (from column 1 in file.txt) and them creates different files with the name of the list.

for nombre in $(awk '{print $1}' file.txt | uniq)
do
  grep $nombre $File > $nombre.txt
done