search
HomeSystem TutorialLINUXSummarized the method of removing newline characters in Linux

今天需要删除文件里面的换行符,比如有一个文件a.txt:

1,2,3
4,5,6
1,2,3
4,5,6
1,2,3
4,5,6
1,2,3
4,5,6
1,2,3
4,5,6

1、使用vim删除换行符

vim输入命令:%s/\n//g

2、使用sed命令,例如 sed ':t;N;s/\n//;b t' a.txt

结果:1,2,34,5,61,2,34,5,61,2,34,5,61,2,34,5,61,2,34,5,6

解释一下:

:t 定义label "t"
b t 转到label "t" 继续执行
N 先读入一行到sed的模板空间,加个换行符(\n),再向sed模板空间追加下一行(之后sed 对模板空间中的内容执行s/\n/,/替换,并显示替换后的内容)

3、使用td命令,例如:

cat a.txt | tr '\n' ' ' 
1,2,3 4,5,6 1,2,3 4,5,6 1,2,3 4,5,6 1,2,3 4,5,6 1,2,3 4,5,6

注意tr最后那个参数是空格,否则会报错

Linux shell 中删除文件的所有换行符

需要做一个小功能,就是把一个文本文件中的所有换行符都去掉。一般肯定是想到使用sed来修改啦。但是我搜了一下,并没有找到sed要怎么输入\n——我知道^M对应的是\r,但是\n却失败了,在命令行中直接被切割掉。
  原则上可以用\015这样的方式来指定的,但是我实际操作发现并不行,没有效果。所以最终我只能用直接输入命令来做了(还没验证过写成脚本是否能用)。

假设要替换的文件叫做in.txt,输出文件为out.txt:

sed 's/^M//g' file.txt > tmp.txt
sed -i 's/\"/\\\"/g' tmp.txt
rm -f out.txt; touch out.txt
cat tmp.txt | xargs echo -n >> out.txt
rm -f tmp.txt

五个步骤分别为:

将Windows格式的换行转换成UNIX格式,也就是删除所有的\r字符。其中^M并不是直接输入这两个字符,而是通过在shell里面按住 Ctrl+V,然后再敲 M 得到的
将所有的双引号转义,因为后面的echo需要使用
创建一个全空的输出文件
删除所有的\n字符并输出最终文件
删除中间文件

The above is the detailed content of Summarized the method of removing newline characters in Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:脚本之家. If there is any infringement, please contact admin@php.cn delete
How to Make a USB Drive Mount Automatically in LinuxHow to Make a USB Drive Mount Automatically in LinuxApr 30, 2025 am 10:04 AM

This guide explains how to automatically mount a USB drive on boot in Linux, saving you time and effort. Step 1: Identify Your USB Drive Use the lsblk command to list all block devices. Your USB drive will likely be labeled /dev/sdb1, /dev/sdc1, etc

Best Cross-Platform Apps for Linux, Windows, and Mac in 2025Best Cross-Platform Apps for Linux, Windows, and Mac in 2025Apr 30, 2025 am 09:57 AM

Cross-platform applications have revolutionized software development, enabling seamless functionality across operating systems like Linux, Windows, and macOS. This eliminates the need to switch apps based on your device, offering consistent experien

Best Linux Tools for AI and Machine Learning in 2025Best Linux Tools for AI and Machine Learning in 2025Apr 30, 2025 am 09:44 AM

Artificial Intelligence (AI) is rapidly transforming numerous sectors, from healthcare and finance to creative fields like art and music. Linux, with its open-source nature, adaptability, and performance capabilities, has emerged as a premier platfo

5 Best Lightweight Linux Distros Without a GUI5 Best Lightweight Linux Distros Without a GUIApr 30, 2025 am 09:38 AM

Looking for a fast, minimal, and efficient Linux distribution without a graphical user interface (GUI)? Lightweight, GUI-less Linux distros are perfect for older hardware or specialized tasks like servers and embedded systems. They consume fewer res

How to Install Wine 10.0 in RedHat DistributionsHow to Install Wine 10.0 in RedHat DistributionsApr 30, 2025 am 09:32 AM

Wine 10.0 stable version release: Running Windows applications on Linux to a higher level Wine, this open source and free application, allows Linux users to run Windows software and games on Unix/Linux operating systems, ushering in the release of the 10.0 stable version! This version has been provided with source code and binary package downloads, and supports various distributions such as Linux, Windows and Mac. This edition embodies a year of hard work and over 8,600 improvements, bringing many exciting improvements. Key highlights include: Enhanced support for Bluetooth devices. Improve support for HID input devices. Optimized performance of 32-bit and 64-bit applications.

How to Install and Configure SQL Server on RHELHow to Install and Configure SQL Server on RHELApr 30, 2025 am 09:27 AM

This tutorial guides you through installing SQL Server 2022 on RHEL 8.x or 9.x, connecting via the sqlcmd command-line tool, database creation, and basic querying. Prerequisites Before beginning, ensure: A supported RHEL version (RHEL 8 or 9). Sudo

How to Install Thunderbird 135 on a Linux DesktopHow to Install Thunderbird 135 on a Linux DesktopApr 30, 2025 am 09:26 AM

Mozilla Thunderbird 135: Powerful cross-platform mail client Mozilla Thunderbird is a free, open source, cross-platform email, calendar, news, chat and contact management client designed to efficiently handle multiple email accounts and news sources. On February 5, 2025, Mozilla released the Thunderbird 135 version, introducing a number of new features, performance improvements and security fixes. Thunderbird 135 main features: XZ Packaging for Linux Binaries: Smaller files, faster unpacking, and better integration with modern distributions. Cookie storage support: when creating space

How to Lock Files for Renaming or Deleting in LinuxHow to Lock Files for Renaming or Deleting in LinuxApr 30, 2025 am 09:11 AM

This guide demonstrates how to protect files on Linux from accidental renaming or deletion using simple commands. We'll use the file important.txt in /home/user/ as an example. Method 1: Using chattr for Immutability The chattr command modifies fil

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor