Close Menu
    Facebook X (Twitter) Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook X (Twitter) Instagram
    TecAdmin
    You are at:Home»Linux Commands»How to replace newline (\n) with a space using sed?

    How to replace newline (\n) with a space using sed?

    By RahulApril 7, 20232 Mins Read

    If you have a text file with newline characters (\n) and you need to replace them with spaces, you can use the sed command in Linux. In this article, we’ll explore how to use sed to replace newline characters with spaces.

    Step 1: Check the contents of the file

    Before you replace newline characters with spaces, it’s a good idea to check the contents of the file to ensure that you’re replacing the correct characters. You can use the cat command to display the contents of the file on the screen. For example, if you have a file called “file.txt”, you can use the following command to display the contents of the file:

    cat file.txt 
    

    This will display the contents of the file on the screen.

    Step 2: Replace newline characters with sed

    Once you’ve verified that you want to replace newline characters with spaces, you can use the sed command to do so.
    To replace newline characters with spaces in a file called “file.txt”, you can use the following command:

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

    This command will print output on screen with replacing new line character (\n) with an space ” ” character. To replace the changes in same file us -i (inline) option with same command.

    sed -i ':a;N;$!ba;s/\n/ /g' file.txt 
    

    The above command will replace all occurrences of newline characters with spaces in the same file.

    Step 3: Verify that the characters were replaced

    After you’ve used sed to replace newline characters with spaces in the file, you can verify that the characters were replaced by using the cat command again:

    cat file.txt 
    

    This command will display the contents of the modified file on the screen. If the newline characters were successfully replaced with spaces, the file should now be displayed without any visible line breaks.

    Conclusion

    In this article, we’ve explored how to replace newline characters with spaces in a text file using the sed command in Linux. By using the sed command with the appropriate syntax, you can quickly and easily modify text files from the command line. Remember to verify the contents of the file before you replace any characters to avoid accidentally modifying the wrong characters.

    sed
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    How to Ignore SSL Certificate Check with Wget

    How to Ignore SSL Certificate Check with Curl

    echo Command in Linux with Practical Examples

    Add A Comment

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • How to Change Port in Next.Js
    • Ubuntu 24.04 LTS: The Future of Open-Source Excellence
    • How to Execute Linux Commands in Python
    • Creating MySQL User with GRANT OPTION
    • Where to find crontab (cron) logs in Ubuntu & Debian
    Facebook X (Twitter) Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.