Saturday, July 17, 2010

Split single line into multiple lines in UNIX

There are so many ways to break a line in unix and i just found out two simple ways of doing it

  • Unix provides a command to directly split a line using length of new lines. There is a command 'fold' to do this:
          Syntax of the command is 
         fold - inputLine > outputLine
         e.g. fold -2000 file > outputFile.
         It will break all the lines in file  "file" into lines of maximum length 2000 characters.

  • Another way to do this is using sed or perl command. Using this command line can be broke around a character and also  minimum number of character can be defined in each new line
          Syntax is
          perl -pe 's/(.{1,199}[ ,])/\1\n/g'
            In this command, 
          .{1,199} - it will search the split character after 200 character only.
          [ ,] - split character is either space pr comma.
          \1\n - once split character is found, it will replace the line with first part of line (upto 200  character and then adds newline character.

No comments:

Post a Comment