简体   繁体   中英

Join two lines in a text file in Unix based on regex pattern matching by removing the newline character in between

I want to do the following Find & Replace operation to a file in Unix

Find:   (\n)(^[^T])
Replace:  \t$2

For example,

Time table
DataColumn

becomes

Time table     DataColumn

This works in a text editor like TextPad, but, is there any way to do this in a single line command? For example, something like:

sed 's/\(\n\)\(^[^T]\)/\t\2/g' tmpfile2.txt    

Using :

perl -0pe 's/\n/\t/' file
Time table      DataColumn

This might work for you (GNU sed):

sed 'N;s/\n\([^T]\)/\t\1/;P;D' file

This replaces a newline followed by a non T character by a tab and the non T character throughout the file.

由于已建议使用sed和perl,因此出现了awk:

awk '/^T/{value=$0; getline; $0= value "\t" $0;} {print}'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM