0

I have a text file (name, surname, phone number) than I would import in excel and the play with column (name, surname...)

The file was previously a docx file. I've converted it on as a text file. But the converted txt file, has a strange format: Repeated 6 lines block, with each field (name, surname, phone, town)

xxx
yyy
999
zzz
xxx1
yyy1

repeated more times. I would compact each block in a single line and separate field by space. NOTE that xxx1 and yyy1 are name and surname of next person So I can import it in Excel, taking space as a separator column.

How can I do that? I know I can do that in Lisp, but I do not know how.

1
  • 1
    You say "Repeated 6 lines block" but you mention 4 parts to each block. Could you clarify how to reconcile this 4 vs 6 discrepency?
    – Stefan
    Commented Jan 8, 2020 at 13:34

3 Answers 3

1

IIUC you could do that with a keyboard macro.

Place the cursor at the beginning i.e. the very first x.

Define the macro (and also transform the first block):

C-x (
C-SPC
C-n
C-n
C-n
M-%
C-q C-j RET
SPC RET
!
C-n
C-a
C-x )

Transform the rest by calling the macro until it fails.

C-u 0 C-x e
0

I would try with sed (unix utility) and regular expressions.

2
  • I don't think you can do this[1] with sed, at least not easily. awk might be a more reasonable suggestion, but remember, this is an emacs group and the OP asked for a lisp solution (although I have been guilty of suggesting non-lisp, non-emacs solutions a couple of times in the past too :-) ). [1]BTW, it's not clear what "this" is: the question is unclear.
    – NickD
    Commented Jan 10, 2020 at 4:24
  • 1
    With some bad faith: awk is in Emacs through eshell. Commented Jan 10, 2020 at 23:38
0

You could use something like this

(defun my-join-n-lines (n)
  (dotimes (_ (1- n))
    (delete-indentation t))
  (forward-line))

(defun my-join-lines-loop ()
  (interactive "*")
  (while (= 0 (my-join-n-lines 4))))

The first function joins n lines, where n is the argument with which the function is called. The second one, which you can call with M-x my-join-lines-loop, calls my-join-n-lines with argument 4 until the buffer ends. Make sure to call it from the first line of a block otherwise it will merge all the lines with an offset with respect to your desired block-splitting.

You can make my-join-lines-loop accept a prefix argument by using this definition instead of the simpler one above,

(defun my-join-lines-loop (&optional n)
  (interactive "P*")
  (while (= 0 (my-join-n-lines (or n 4)))))

Here the argument is optional and defaults to 4.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.