unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Is it possible to move a column where column on right is variable width
@ 2017-05-16 16:34 Angus Comber
  2017-05-16 17:38 ` Kaushal Modi
                   ` (5 more replies)
  0 siblings, 6 replies; 12+ messages in thread
From: Angus Comber @ 2017-05-16 16:34 UTC (permalink / raw)
  To: help-gnu-emacs

I downloaded a file, LSE.txt from this link:

http://www.eoddata.com/Data/symbollist.aspx?e=LSE

which is of the form:

symbol<tab>Description

the description is pretty free form.  Here are some examples:

06GG.L	BARCLAYS BANK PLC UNDATED FLTG RATE PRIM CAP NOTES'2'USD
06GL.L	NATIONAL GRID GAS 8.75% BDS 27/6/25 GBP(VAR)
07OZ.L	HAMMERSON PLC 6.875% BDS 31/3/20 GBP(VAR)

What I would like to do is swap so it displays:
Description<tab>Symbol.

I tried using rectangles, eg:

set mark at top left buffer, C-space
select area, ie selected area is all of Symbols column
Then c-x r k to remove the symbols from 1st column and save to the rectangle kill ring.
then I replace $ with tab.

Then I navigate to the end of the first line and try c-x r y - but that inserts the symbols into the middle of the Descriptions.

Is there any way to do what I want?


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Is it possible to move a column where column on right is variable width
  2017-05-16 16:34 Is it possible to move a column where column on right is variable width Angus Comber
@ 2017-05-16 17:38 ` Kaushal Modi
  2017-05-16 19:11   ` Joost Kremers
  2017-05-16 18:51 ` Tomas Nordin
                   ` (4 subsequent siblings)
  5 siblings, 1 reply; 12+ messages in thread
From: Kaushal Modi @ 2017-05-16 17:38 UTC (permalink / raw)
  To: Angus Comber, help-gnu-emacs

On Tue, May 16, 2017 at 12:35 PM Angus Comber <anguscomber@gmail.com> wrote:

> I downloaded a file, LSE.txt from this link:
>
> http://www.eoddata.com/Data/symbollist.aspx?e=LSE


I cannot access that site without a login, but I used the below data
snippet of yours.


> which is of the form:
>
> symbol<tab>Description
>
> the description is pretty free form.  Here are some examples:
>
> 06GG.L  BARCLAYS BANK PLC UNDATED FLTG RATE PRIM CAP NOTES'2'USD
> 06GL.L  NATIONAL GRID GAS 8.75% BDS 27/6/25 GBP(VAR)
> 07OZ.L  HAMMERSON PLC 6.875% BDS 31/3/20 GBP(VAR)
>
> Is there any way to do what I want?
>

As the Description portion of the data is not of equal width, using
rectanges gets tricky.

A simple query-replace-regexp works in this case.

1. M-x query-replace-regexp (or C-M-%)
2. Enter this in the first field:
      ^\([^[:space:]]+\)[[:space:]]+\(.+\)
    and hit return.
3. Enter this in the next field:
      \2 \1 (or, \2 C-q TAB \1)
    and hit return
4. Hit ! to execute that search/replace for all hits.


-- 

Kaushal Modi


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Is it possible to move a column where column on right is variable width
  2017-05-16 16:34 Is it possible to move a column where column on right is variable width Angus Comber
  2017-05-16 17:38 ` Kaushal Modi
@ 2017-05-16 18:51 ` Tomas Nordin
  2017-05-16 19:14   ` Emanuel Berg
  2017-05-16 21:56   ` Tomas Nordin
  2017-05-17  1:17 ` Skip Montanaro
                   ` (3 subsequent siblings)
  5 siblings, 2 replies; 12+ messages in thread
From: Tomas Nordin @ 2017-05-16 18:51 UTC (permalink / raw)
  To: Angus Comber, help-gnu-emacs

> which is of the form:
>
> symbol<tab>Description
>
> the description is pretty free form.  Here are some examples:
>
> 06GG.L	BARCLAYS BANK PLC UNDATED FLTG RATE PRIM CAP NOTES'2'USD
> 06GL.L	NATIONAL GRID GAS 8.75% BDS 27/6/25 GBP(VAR)
> 07OZ.L	HAMMERSON PLC 6.875% BDS 31/3/20 GBP(VAR)
>
> What I would like to do is swap so it displays:
> Description<tab>Symbol.
>

Here is a suggestion using GNU sed

C-x h          mark-whole-buffer
C-u M-|        shell-command-on-region (with prefix to output in buffer)

then yank this as the shell command
sed -E 's/(.+)\t(.+)/\2\t\1/'

and hope there was no tabs in the description.

--
Tomas



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Is it possible to move a column where column on right is variable width
  2017-05-16 17:38 ` Kaushal Modi
@ 2017-05-16 19:11   ` Joost Kremers
  2017-05-16 19:23     ` Emanuel Berg
  2017-05-16 19:32     ` Kaushal Modi
  0 siblings, 2 replies; 12+ messages in thread
From: Joost Kremers @ 2017-05-16 19:11 UTC (permalink / raw)
  To: Kaushal Modi; +Cc: help-gnu-emacs


On Tue, May 16 2017, Kaushal Modi wrote:
> On Tue, May 16, 2017 at 12:35 PM Angus Comber 
> <anguscomber@gmail.com> wrote:
>
>> I downloaded a file, LSE.txt from this link:
>>
>> http://www.eoddata.com/Data/symbollist.aspx?e=LSE
>
>
> I cannot access that site without a login, but I used the below 
> data
> snippet of yours.
>
>
>> which is of the form:
>>
>> symbol<tab>Description
>>
>> the description is pretty free form.  Here are some examples:
>>
>> 06GG.L  BARCLAYS BANK PLC UNDATED FLTG RATE PRIM CAP 
>> NOTES'2'USD
>> 06GL.L  NATIONAL GRID GAS 8.75% BDS 27/6/25 GBP(VAR)
>> 07OZ.L  HAMMERSON PLC 6.875% BDS 31/3/20 GBP(VAR)
>>
>> Is there any way to do what I want?
>>
>
> As the Description portion of the data is not of equal width, 
> using
> rectanges gets tricky.

Unless you do the totally uncool thing of adding enough spaces to 
the final line so that you can create a rectangle of the 
appropriate size. It'll cost you some geek points, but it gets the 
job done. ;-)



-- 
Joost Kremers
Life has its moments



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Is it possible to move a column where column on right is variable width
  2017-05-16 18:51 ` Tomas Nordin
@ 2017-05-16 19:14   ` Emanuel Berg
  2017-05-16 21:56   ` Tomas Nordin
  1 sibling, 0 replies; 12+ messages in thread
From: Emanuel Berg @ 2017-05-16 19:14 UTC (permalink / raw)
  To: help-gnu-emacs

Tomas Nordin wrote:

> Here is a suggestion using GNU sed
>
> C-x h mark-whole-buffer C-u M-|
> shell-command-on-region (with prefix to output
> in buffer)
>
> then yank this as the shell command sed -E
> 's/(.+)\t(.+)/\2\t\1/'
>
> and hope there was no tabs in
> the description.

How big is the buffer? Often one can do such
edits manually pretty quickly. Get some work on
your finger habits/muscle memory in the
bargain. May come in "hand"y :)

-- 
underground experts united
http://user.it.uu.se/~embe8573




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Is it possible to move a column where column on right is variable width
  2017-05-16 19:11   ` Joost Kremers
@ 2017-05-16 19:23     ` Emanuel Berg
  2017-05-16 19:32     ` Kaushal Modi
  1 sibling, 0 replies; 12+ messages in thread
From: Emanuel Berg @ 2017-05-16 19:23 UTC (permalink / raw)
  To: help-gnu-emacs

Joost Kremers wrote:

> Unless you do the totally uncool thing of
> adding enough spaces to the final line so
> that you can create a rectangle of the
> appropriate size. It'll cost you some geek
> points, but it gets the job done. ;-)

Ha! You should read *my* suggestion!

Actually I think typing and editing skills are
the sign of the true hacker.

Lots of people think of it the other direction.
You know the quote, the best hacker is the
hacker who is laziest, because he will find
ways to automate everything with
minimal effort?

I don't know who said it but I don't believe in
it. It think it is a quote for very young
programmers, like 12-?, because then you take
credit in being lazy doing the least required.
Maybe because your body is busy growing?

No, good old typing, moving the cursor around
at lightning speed, killing and yanking, it can
get you anywhere. I like regexps as well tho
but often they are not easily transferable.
You start anew every time.



PS. Probably the best hacker is the guy you
    never heard about, using technology you
    never thought existed...

-- 
underground experts united
http://user.it.uu.se/~embe8573




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Is it possible to move a column where column on right is variable width
  2017-05-16 19:11   ` Joost Kremers
  2017-05-16 19:23     ` Emanuel Berg
@ 2017-05-16 19:32     ` Kaushal Modi
  1 sibling, 0 replies; 12+ messages in thread
From: Kaushal Modi @ 2017-05-16 19:32 UTC (permalink / raw)
  To: Joost Kremers; +Cc: help-gnu-emacs

On Tue, May 16, 2017 at 3:11 PM Joost Kremers <joostkremers@fastmail.fm>
wrote:

> > As the Description portion of the data is not of equal width,
> > using
> > rectanges gets tricky.
>
> Unless you do the totally uncool thing of adding enough spaces to
> the final line so that you can create a rectangle of the
> appropriate size. It'll cost you some geek points, but it gets the
> job done. ;-)
>

I had ended up doing a "thesis" on that topic a while back. The resultant
solution adds the right number of spaces temporarily to get the widest
required rectangle without modifying the buffer :)

https://github.com/kaushalmodi/.emacs.d/blob/df25c8cd0d84ec2af647e92db2c0c19e3097d035/setup-files/setup-editing.el#L507-L630

The main logic is inside modi/extend-rectangle--core, but then I apply it
to different function to "do the right thing".

From the doc-string of modi/advice-select-rectangle-to-end in that code:

  "Extract the rectangle so that the longest line of region is completely
included when the prefix \\[universal-argument] is used.

In the below example, ▯ is the mark and ▮ is the point.

  a =▯12345;
  b = 6;▮

If that region is selected and if we do \\[copy-rectangle-as-kill], the
following
rectangle gets copied:

  12
  6;

.. which was not the intention.

But with this advice, \\[universal-argument] \\[copy-rectangle-as-kill] on
that
same region will copy the below rectangle:

  12345;
  6;

.. which obviously was the actual intention.

Similar rectangle extension behavior is applied when using the
prefix \\[universal-argument] with similarly advised functions too."

-- 

Kaushal Modi


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Is it possible to move a column where column on right is variable width
  2017-05-16 18:51 ` Tomas Nordin
  2017-05-16 19:14   ` Emanuel Berg
@ 2017-05-16 21:56   ` Tomas Nordin
  1 sibling, 0 replies; 12+ messages in thread
From: Tomas Nordin @ 2017-05-16 21:56 UTC (permalink / raw)
  To: Angus Comber, help-gnu-emacs

> Here is a suggestion using GNU sed
>
> C-x h          mark-whole-buffer
> C-u M-|        shell-command-on-region (with prefix to output in buffer)
>
> then yank this as the shell command
> sed -E 's/(.+)\t(.+)/\2\t\1/'

and to spare your finger muscles, just yank this into the shell

sed -E 's/(.+)\t(.+)/\2\t\1/' LSE.txt > LSE-swap.txt



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Is it possible to move a column where column on right is variable width
  2017-05-16 16:34 Is it possible to move a column where column on right is variable width Angus Comber
  2017-05-16 17:38 ` Kaushal Modi
  2017-05-16 18:51 ` Tomas Nordin
@ 2017-05-17  1:17 ` Skip Montanaro
  2017-05-17  2:05 ` Ben Bacarisse
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 12+ messages in thread
From: Skip Montanaro @ 2017-05-17  1:17 UTC (permalink / raw)
  To: Angus Comber; +Cc: help-gnu-emacs

> I tried using rectangles, eg:
>
> set mark at top left buffer, C-space
> select area, ie selected area is all of Symbols column
> Then c-x r k to remove the symbols from 1st column and save to the rectangle kill ring.
> then I replace $ with tab.

I think you were on the right track with rectangles, but I I would cut
out the ragged description and move it in front of the symbols. So,
using your example:

* place point over the "H" in "HAMMERSON"
* set mark
* move point after the "USD" at the end of the first line.
* insert a single tab (perhaps need C-q C-i).
* cut the rectangle
* move point to the start of the buffer
* yank the rectangle.

If the first line isn't the longest, continue entering TABs until you
are past the length of the longest line. You will probably have to
collapse multiple whitespace characters into one to get your TAB
separator back.

All the trailing whitespace will disappear if you have this set:

(add-hook 'before-save-hook 'delete-trailing-whitespace)

You might want to make that mode-specific.

Other people have offered non-Emacs solutions as well. I offer two.

My favorite for such simple things is awk:

awk -F ' \t' '{printf("%s\t%s\n", $2, $1)}'< description.txt

If I wanted to place the algorithm into a file (you won't just be
downloading a single file of stock data :-), either toss the awk
command into a shell script, or write a short little Python script:

#!/usr/bin/env python

import sys, csv

rdr = csv.reader(sys.stdin, delimiter="\t")
wtr = csv.writer(sys.stdout, delimiter="\t")

for row in rdr:
    wtr.writerow(list(reversed(row)))


Skip Montanaro



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Is it possible to move a column where column on right is variable width
  2017-05-16 16:34 Is it possible to move a column where column on right is variable width Angus Comber
                   ` (2 preceding siblings ...)
  2017-05-17  1:17 ` Skip Montanaro
@ 2017-05-17  2:05 ` Ben Bacarisse
  2017-05-17  6:22 ` Loris Bennett
  2017-05-18  6:17 ` hector
  5 siblings, 0 replies; 12+ messages in thread
From: Ben Bacarisse @ 2017-05-17  2:05 UTC (permalink / raw)
  To: help-gnu-emacs

Angus Comber <anguscomber@gmail.com> writes:

> I downloaded a file, LSE.txt from this link:
>
> http://www.eoddata.com/Data/symbollist.aspx?e=LSE
>
> which is of the form:
>
> symbol<tab>Description
>
> the description is pretty free form.  Here are some examples:
>
> 06GG.L	BARCLAYS BANK PLC UNDATED FLTG RATE PRIM CAP NOTES'2'USD
> 06GL.L	NATIONAL GRID GAS 8.75% BDS 27/6/25 GBP(VAR)
> 07OZ.L	HAMMERSON PLC 6.875% BDS 31/3/20 GBP(VAR)
>
> What I would like to do is swap so it displays:
> Description<tab>Symbol.

It would help to show the output you want.

> I tried using rectangles, eg:

This would be the method of choice if you want the symbols to be lined
up.  If "Description<tab>Symbol" means you want one tab character
between the two, use a regex replace:

M-x replace-regexp RET
\(.*\)	\(.*\)RET
\2	\1RET

(I typed a tab between ) and \ and 2 and \ but it might get mauled by
some part of the mail sending)

<snip>
-- 
Ben.


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Is it possible to move a column where column on right is variable width
  2017-05-16 16:34 Is it possible to move a column where column on right is variable width Angus Comber
                   ` (3 preceding siblings ...)
  2017-05-17  2:05 ` Ben Bacarisse
@ 2017-05-17  6:22 ` Loris Bennett
  2017-05-18  6:17 ` hector
  5 siblings, 0 replies; 12+ messages in thread
From: Loris Bennett @ 2017-05-17  6:22 UTC (permalink / raw)
  To: help-gnu-emacs

Angus Comber <anguscomber@gmail.com> writes:

> I downloaded a file, LSE.txt from this link:
>
> http://www.eoddata.com/Data/symbollist.aspx?e=LSE
>
> which is of the form:
>
> symbol<tab>Description
>
> the description is pretty free form.  Here are some examples:
>
> 06GG.L	BARCLAYS BANK PLC UNDATED FLTG RATE PRIM CAP NOTES'2'USD
> 06GL.L	NATIONAL GRID GAS 8.75% BDS 27/6/25 GBP(VAR)
> 07OZ.L	HAMMERSON PLC 6.875% BDS 31/3/20 GBP(VAR)
>
> What I would like to do is swap so it displays:
> Description<tab>Symbol.
>
> I tried using rectangles, eg:
>
> set mark at top left buffer, C-space select area, ie selected area is
> all of Symbols column Then c-x r k to remove the symbols from 1st
> column and save to the rectangle kill ring.  then I replace $ with
> tab.
>
> Then I navigate to the end of the first line and try c-x r y - but
> that inserts the symbols into the middle of the Descriptions.
>
> Is there any way to do what I want?

You could convert to a an Org mode table, reorder the columns and, if
needed, convert back to plain(er) text:

1. M-x orgtbl-mode
2. Highlight the lines
3. Do C-c | (or M-x orgtbl-create-or-convert-from-region)
4. With the cursor in the left-hand column, do M-<right arrow>
5. Replace the |-symbols 

Cheers,

Loris

-- 
This signature is currently under construction.


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Is it possible to move a column where column on right is variable width
  2017-05-16 16:34 Is it possible to move a column where column on right is variable width Angus Comber
                   ` (4 preceding siblings ...)
  2017-05-17  6:22 ` Loris Bennett
@ 2017-05-18  6:17 ` hector
  5 siblings, 0 replies; 12+ messages in thread
From: hector @ 2017-05-18  6:17 UTC (permalink / raw)
  To: help-gnu-emacs

On Tue, May 16, 2017 at 09:34:02AM -0700, Angus Comber wrote:
> I downloaded a file, LSE.txt from this link:
> 
> http://www.eoddata.com/Data/symbollist.aspx?e=LSE
> 
> which is of the form:
> 
> symbol<tab>Description
> 
> the description is pretty free form.  Here are some examples:
> 
> 06GG.L	BARCLAYS BANK PLC UNDATED FLTG RATE PRIM CAP NOTES'2'USD
> 06GL.L	NATIONAL GRID GAS 8.75% BDS 27/6/25 GBP(VAR)
> 07OZ.L	HAMMERSON PLC 6.875% BDS 31/3/20 GBP(VAR)
> 
> What I would like to do is swap so it displays:
> Description<tab>Symbol.

I think it is already solved but just another approach.
Lately I tend to use macros for a lot of things.
It is a way of programming without having to know
the syntax. Very easy and very powerful if you don't care
much about efficiency.

Record this macro:
C-SPC                               ; set mark at the beginning
M-x search-forward RET C-q TAB RET  ; go past the first TAB
C-b                                 ; go backwards
C-d                                 ; delete TAB
C-w                                 ; kill region (the first field)
C-e                                 ; go to end of line
C-q TAB                             ; insert TAB
C-y                                 ; yank the first field

Then just repeat it for all the lines (apply-macro-to-region-lines)
and that's it.



^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2017-05-18  6:17 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-05-16 16:34 Is it possible to move a column where column on right is variable width Angus Comber
2017-05-16 17:38 ` Kaushal Modi
2017-05-16 19:11   ` Joost Kremers
2017-05-16 19:23     ` Emanuel Berg
2017-05-16 19:32     ` Kaushal Modi
2017-05-16 18:51 ` Tomas Nordin
2017-05-16 19:14   ` Emanuel Berg
2017-05-16 21:56   ` Tomas Nordin
2017-05-17  1:17 ` Skip Montanaro
2017-05-17  2:05 ` Ben Bacarisse
2017-05-17  6:22 ` Loris Bennett
2017-05-18  6:17 ` hector

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).