all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* printing columns into a file
@ 2009-12-28  2:45 Jin
  2009-12-28 11:49 ` Pascal J. Bourguignon
  2009-12-30  9:39 ` Arnaldo Mandel
  0 siblings, 2 replies; 3+ messages in thread
From: Jin @ 2009-12-28  2:45 UTC (permalink / raw
  To: help-gnu-emacs

Hi,

I will really appreciate any help on this!

I have a file looking like the following

date        month           year

12              5                1982
30              6               1982
                  3               1982
28                               1877
30              2                1888

As you can see, my file has blanks in a column, but I like to keep it
that way.
When I tried to use awk to print each column into a separate file
(date.txt, month.txt, and year.txt),
it doesn't retain the blank information on emacs but moves the value
below upward to cover the blank. So, it'd be like

date.txt
12
30
28
30

month.txt
5
6
3
2

How can I keep the blank as it is in the original fiile?

Please help.


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

* Re: printing columns into a file
  2009-12-28  2:45 printing columns into a file Jin
@ 2009-12-28 11:49 ` Pascal J. Bourguignon
  2009-12-30  9:39 ` Arnaldo Mandel
  1 sibling, 0 replies; 3+ messages in thread
From: Pascal J. Bourguignon @ 2009-12-28 11:49 UTC (permalink / raw
  To: help-gnu-emacs

Jin <kyeunjin@gmail.com> writes:

> Hi,
>
> I will really appreciate any help on this!
>
> I have a file looking like the following
>
> date        month           year
>
> 12              5                1982
> 30              6               1982
>                   3               1982
> 28                               1877
> 30              2                1888
>
> As you can see, my file has blanks in a column, but I like to keep it
> that way.
> When I tried to use awk to print each column into a separate file
> (date.txt, month.txt, and year.txt),
> it doesn't retain the blank information on emacs but moves the value
> below upward to cover the blank. So, it'd be like
>
> date.txt
> 12
> 30
> 28
> 30
>
> month.txt
> 5
> 6
> 3
> 2
>
> How can I keep the blank as it is in the original fiile?

So this is an awk question?  man awk

Otherwise you could do it in emacs:


(require 'cl)


(defmacro* dolines (start-end &body body)
  "Executes the body with start-var and end-var bound to the start and the end of each lines of the current buffer in turn."
  (let ((vline (gensym)))
    (destructuring-bind (start-var end-var) start-end
      `(let ((sm (make-marker))
             (em (make-marker)))
         (unwind-protect
              (progn
                (goto-char (point-min))
                (while (< (point) (point-max))
                  (let ((,vline (point)))
                    (set-marker sm (point))
                    (set-marker em (progn (end-of-line) (point)))
                    (let ((,start-var  (marker-position sm))
                          (,end-var    (marker-position em)))
                      ,@body)
                    (goto-char ,vline)
                    (forward-line 1))))
           (set-marker sm nil)
           (set-marker em nil))
         nil))))


(dolines (start end)
  (goto-char start)
  (when (re-search-forward "^\\( *?[0-9]*\\)\\( *?[0-9]*\\)\\( *[0-9]*\\)$" end t)
     (let ((day   (match-string 1))
           (month (match-string 2))
           (year  (match-string 3)))
        (with-current-buffer (get-buffer-create "day.txt")
           (insert day "\n"))
        (with-current-buffer (get-buffer-create "month.txt")
           (insert month "\n"))       
        (with-current-buffer (get-buffer-create "year.txt")
           (insert year "\n")))))







-- 
__Pascal Bourguignon__                     http://www.informatimago.com/


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

* RE: printing columns into a file
  2009-12-28  2:45 printing columns into a file Jin
  2009-12-28 11:49 ` Pascal J. Bourguignon
@ 2009-12-30  9:39 ` Arnaldo Mandel
  1 sibling, 0 replies; 3+ messages in thread
From: Arnaldo Mandel @ 2009-12-30  9:39 UTC (permalink / raw
  To: help-gnu-emacs

Jin wrote (on Dec 27, 2009):
 > Hi,
 > 
 > I will really appreciate any help on this!
 > 
 > I have a file looking like the following
 > 
 > date        month           year
 > 
 > 12              5                1982
 > 30              6               1982
 >                   3               1982
 > 28                               1877
 > 30              2                1888
 > 
 > As you can see, my file has blanks in a column, but I like to keep it
 > that way.
 > When I tried to use awk to print each column into a separate file
 > (date.txt, month.txt, and year.txt),
 > it doesn't retain the blank information on emacs but moves the value
 > below upward to cover the blank. So, it'd be like
 
There are two solutions:

a) You slighly change the file format, introducing a column
   separator.  Tab would be convenient, since you can make it look
   like a space in emacs.  Then you can use that as a field separator
   for awk or cut.

b) If you don't have a field separator, your columns are determined by
   a range of positions in the line.  You can figure them out and give
   them as parameters to  cut -c.

-- 
Arnaldo Mandel                        
Departamento de Ciência da Computação - Computer Science Department
Universidade de São Paulo, Bra[sz]il	  
am@ime.usp.br
Talvez você seja um Bright http://the-brights.net Maybe you are a Bright.




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

end of thread, other threads:[~2009-12-30  9:39 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-12-28  2:45 printing columns into a file Jin
2009-12-28 11:49 ` Pascal J. Bourguignon
2009-12-30  9:39 ` Arnaldo Mandel

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.