Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Shell Scripting
  • Kali Linux
  • Ubuntu
  • Red Hat
  • CentOS
  • Docker in Linux
  • Kubernetes in Linux
  • Linux interview question
  • Python
  • R
  • Java
  • C
  • C++
  • JavaScript
  • DSA
Open In App
Next Article:
Convert Multiple Rows into Single Row in Linux
Next article icon

Convert Multiple Rows into Single Row in Linux

Last Updated : 19 Sep, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we are going to see how to convert multiple rows into a single row using Linux.

Here is the file for demonstration:

Method 1: Using Pure Bash

Let us convert multiple rows into a single row using pure bash since it is the default shell, please make a note that bash shell does not depend on other utilities as it makes use of only built-in commands :

We use the above input file as a template to perform our desired operation i.e converting multiple rows into a single row in Linux.

Syntax: $ (readarray -t ARRAY < inputfile.txt; IFS=''; echo "${ARRAY[*]}")

Where:

  • The readarray is a Bash built-in command. It was introduced in Bash ver.4. The readarray is used for reading inputs standard input into an array variable: ARRAY.
  • The -t option is handy for removing
  • The IFS is a special shell variable which ia an abbreviation for Internal Field Separator. Here, we assigned the IFS with a single character, empty or ‘,’ depends on our requirements.
  • ${ARRAY[*]} means all elements surrounded in ARRAY variable, by using the  echo command, all elements of ARRAY will be printed out which are separated  by the IFS variables.

Example:

$ (readarray -t ARRAY < newfile.txt; IFS=''; echo "${ARRAY[*]}")

Output :

Method 2: Using tr command

tr is a command which is handy at deletion and translation of specific characters fetched from the standard input file.

Syntax: $ tr [OPTION] SET1 [SET2]

The tr command can simply do this job by simply by removing line breaks from the file content.

Example:

$ tr -d '\n' ',' <newfile.txt

Output :

Method 3: Using Paste command

Paste command is handy for joining files horizontally by outputting lines consisting of lines from each file specified.

Syntax : paste [OPTION]... [FILES]...

The paste command is used for merging lines belonging to multiple input files. This Merging operation by default is executed in such a way  that entries in the first column belong to the first file, and the ones in the second column are for the second file.The -s option can let it merge lines row-wise.

Example:

$ paste -sd ',' newfile.txt 

Output : 

Method 4: Using sed command

Sed in Unix is an abbreviation for stream editor, which is handy in executing different kinds of operations like searching, find and replace, insertion or deletion. 

Syntax : sed OPTIONS... [SCRIPT] [INPUTFILE...] 

Here, the motto of using sed one-liner is: append each line into the pattern space, at last replace all line breaks with the given string. 

Example:

$ sed ':a; N; $!ba; s/\n/,/g'  newfile.txt

Output : 


Next Article
Convert Multiple Rows into Single Row in Linux

R

ravi.geek24
Improve
Article Tags :
  • Linux-Unix
  • linux-command
  • Linux-text-processing-commands

Similar Reads

    Convert Multiple Lines to Single Line Using RStudio Text Editor
    In data science and programming, it's common to deal with multiline text or code that needs to be condensed into a single line. This might be required for various reasons, such as simplifying data entry, formatting SQL queries, or preparing strings for functions that expect single-line input. The RS
    3 min read
    Grouping Multiple Columns and Transposing in Hive
    Apache Hive is a powerful tool for data warehousing and analysis, enabling users to manage and query large datasets stored in Hadoop. One common requirement is to group data by multiple columns and then transpose the results, converting rows into columns. This article will guide you through the step
    3 min read
    How to Efficiently Convert Rows to Columns in PL/SQL?
    In Oracle PL/SQL, converting rows into columns is a common operation, especially useful for reporting, data analysis, and reformatting data for easy visualization. PL/SQL, or Procedural Language/Structured Query Language, is a powerful procedural extension to SQL, created by Oracle, that integrates
    5 min read
    How to Convert Rows into Columns in MySQL?
    Converting rows into columns, also known as pivoting or transposing, is a common operation in DBMS, and MySQL provides robust functionality for achieving this transformation. This process is useful to reshape data for better analysis or reporting. This guide will explore the syntax, usage, and examp
    3 min read
    Split a Matrix into a List of its Rows using R
    In this article, we will discuss how to split a given matrix into a List of its rows using R Programming Language. A matrix in R is a two-dimensional data set with columns and rows that can hold homogeneous data. Splitting a matrix can be useful for various purposes in data manipulation and analysis
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences