• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
sharadchhetri

sharadchhetri

Tutorials On Linux, Unix & Open Source

  • Home
  • Linux Commands
  • Resources
    • Learn Linux
  • My WordPress plugins

Convert new line to space by using sed command

August 28, 2013 by Sharad Chhetri 1 Comment

This is a one line sed command trick in which we will convert the new line into space.
I have a file in which there is a words per line.Now here I want to remove all these lines and instead of this I would like to use spaces.

For eg. I have a file called test and it has data as given below

linux@tuxworld:/tmp$ cat test 
red
blue
green
yellow
white
black
brown
orange
linux@tuxworld:/tmp$ 

Now using sed comand to convert new line into space

sed ':a;N;$!ba;s/n/ /g' filename 

Below is reference –

linux@tuxworld:/tmp$ sed ':a;N;$!ba;s/n/ /g' test 
red blue green yellow white black brown orange
linux@tuxworld:/tmp$

If you use -i flag with sed command it will do changes in file also

linux@tuxworld:/tmp$ sed -i ':a;N;$!ba;s/n/ /g' test 
linux@tuxworld:/tmp$ cat test 
red blue green yellow white black brown orange
linux@tuxworld:/tmp$

Share this:

  • Twitter
  • Facebook
  • More
  • Print
  • Email
  • LinkedIn
  • Reddit
  • Tumblr
  • Pinterest
  • Pocket
  • Telegram
  • WhatsApp
  • Mastodon

Related posts:

  1. convert space into new line using sed command
  2. tr command to convert lines to space , tab and vertical tab
  3. Convert video file into gif file through command line in linux
  4. How to print particular line number by using sed command
  5. vi : remove space at beginning and end of each line
  6. sed : find the pattern (keyword) and delete the line from file
  7. df command not showing correct free space in linux
  8. fatal error: error writing to /tmp/ccwAjc9Z.s: No space left on device
  9. remove html tags by using sed command
  10. Taking file backup while using sed command with -i flag

Filed Under: Linux, Tips And Tricks Tagged With: sed

Reader Interactions

Comments

  1. Anton says

    October 22, 2014 at 7:33 pm

    Wrong tool for the job. Much simpler:

    tr ‘n’ ‘ ‘ < test

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Our Social Media Presence

  • Facebook
  • GitHub
  • Twitter

Linux Command

What is Linux Internal And External Command

Linux Basic Commands With Examples For Every Beginner

tr command to convert lines to space , tab and vertical tab

smbpasswd command not found on CentOS 7 and RHEL 7

Solution : semanage command not found

Unix / Linux : How to print duplicate lines from file

More Posts from this Category

You Might Like These Articles!

simplecodesyntax wordpress plugin

SimpleCodeSyntax : My Another WordPress Plugin

Install Nginx

How To Install Nginx On Ubuntu 22.04 LTS

Install Latest Git package in Ubuntu Operating System

How To Always Install Latest Git Package In Ubuntu Operating System

Bash script for installing VirtualBox on Ubuntu 22.04 LTS Desktop

Install VirtualBox On Ubuntu 22.04 LTS Desktop (Bash Script)

libfuse

dlopen(): error loading libfuse.so.2 – Got Error On Ubuntu

Failed to open/create the internal network

VirtualBox Error: Failed to open/create the internal network

Always Useful Tips And Tricks

Do not show line haveing particular keyword by grep command

How to find when Operating system was installed in linux CentOS and Red Hat

Allow only members of Wheel group to use su command on RHEL/CentOS

Yum Error database disk image is malformed

GNU Screen

How to empty Trash through command line in Ubuntu

New XAMPP security concept

Explore 90+ Article On "Linux Tips And Tricks"

Copyright © 2023 ยท
The material in this site cannot be republished either online or offline, without our permission.
Proudly Blogging From Bharat.

  • Contact
  • About Me
  • My WordPress plugins
  • Privacy Policy