• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / Linux Tips / Fold / How to Fold a Long Single Line Text to Multiple Lines (in Python and Linux)?

How to Fold a Long Single Line Text to Multiple Lines (in Python and Linux)?

January 15, 2016 by cmdlinetips

You might want to convert a really long string in a single line into multiple lines with specified width. For example, if you have string like

12345678912345678912345678912345678912345678910

and want to split the long text to multi-line text, where each line is of length w, like w=9

123456789
123456789
123456789
123456789
123456789
10

In the above example, each line has been formatted to have width of w=9 and the last line has the remaining text. One immediate application of a solution to this problem is in formating a chromosome long genome sequence into multi-line output format like FASTA.

Here are two ways to fold a long line; one using a few lines of Python code, and the other using a simple Linux command on terminal.

Python Code to Convert a Long Single Line to Multi-Line Text

def fold_W(line, w, ofile):
    """
    Fold a long line to multiple lines
    with width w (int)
    Input: a long line of text
    Input: line width
    Output: file with multi-line output
    """
    out = open(ofile, "w")
    line = line.rstrip()
    start=0
    # get the length of long line
    line_length = len(line)
    # loop through the long line using the desired line
    # width
    while line_length - start >= w:
          print >>out, line[start:start+w]
          start += w
    # The rest of the line that does not completely
    # use a line of length w
    print >>out, line[start:]

Linux Command to Convert a Long Single Line to Multi-Line Text

Although the above multi-line code to convert a single line code works fine, there is elegant solution from a Linux command. The Linux command that does the job neatly in a single line is “fold”. If the long line is in a file named “longLine.txt”, one can split into multi-line text by using “fold” command as


fold -w 9 longLine.txt

where the -w option specifies the line width for the multi-line output.

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

Default Thumbnail3 Ways to Write Text to a File in Python Default ThumbnailHow to Run a Shell Command from Python and Get The Output? Default Thumbnail3 Ways to Read A Text File Line by Line in Python How to Get a Specific Line or Lines From a Big File? Hint: Use One Liner AWK

Filed Under: Fold, fold a line to multi--line Tagged With: Fold, Fold a line into multi-line, Linux Commands, Python Tips

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version