2

I have a text file with following content (Actual filecontains thousands of lines)

abcd
xyas
sasss
jddks
asa
sasa
lkkkss
asasd

I want the output in the folloing format.

abcd xyas sasss jddks
asa sasa lkkkss asasd

How can i acheve this?

  • Always four words per line? Or is it more wrapped a set column of your file? – Drav Sloan Sep 11 '13 at 17:51
10

If you just want to join every 4 lines into one, you can do it with a recursive macro:

ggqaqqa4Jj@aq@a

Explanation:

  • gg go to start of file
  • qaq clear any previously stored macro in register a
  • qa start recording a macro in register a
  • 4J join 4 lines into one
  • j go down one line
  • @a have the macro in register a call itself
  • q stop recording
  • @a call the macro in register a which will keep working on the file until it's done.

An alternative in Perl

perl -ni -e 'chomp;print $_, $.%4? " ":"\n"' your_file

Reference

  • The -l option for perl will eliminate the need for the chomp and the "\n". – jordanm Sep 11 '13 at 18:31
  • @jordanm How can it eliminate the "\n"? – Joseph R. Sep 11 '13 at 18:34
  • Why is the qaq step necessary? If you start recording to a small letter register, the previous content is automatically overwritten. – Marco Sep 11 '13 at 18:35
  • @Marco Any reference for that? – Joseph R. Sep 11 '13 at 18:36
  • @JosephR. Just try it: :let @a="foobar", then record a macro: qa6ddq and display the result: :echo @a6dd. If you record the macro as: qA6ddq then the commands are appended: :echo @afoobar6dd – Marco Sep 11 '13 at 18:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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