简体   繁体   English

如何将一个文本文件逐行拆分为两个按定界符分隔的文本文件?

[英]How can I split a text file line by line into 2 text files by delimited column?

I've tried various combinations of the following: 我尝试了以下各种组合:

awk -F" ||| " '{$0=$1}1' source_file.txt > column1.txt
awk -F" ||| " '{$0=$1}2' source_file.txt > column2.txt

or 要么

awk 'BEGIN {FS=" ||| ";}{print $1}' source_file.txt > column1.txt
awk 'BEGIN {FS=" ||| ";}{print $2}' source_file.txt > column2.txt

Instead of the desired output, I either get the entire line (ex. foo bar ||| baz ) or I get only the first word (ex. foo ). 除了获得期望的输出,我要么得到整行(例如foo bar ||| baz ),要么仅得到第一个单词(例如foo )。

If you'd like to help, here is a sample text file: 如果您想提供帮助,请参见以下示例文本文件:

source_file.txt source_file.txt

foo bar ||| baz
qux ||| quux
corge grault ||| garply waldo
fred |||
xyzzy ||| thud

And here's the desired output: 这是所需的输出:

column1.txt column1.txt

foo bar
qux
corge grault
fred
xyzzy

column2.txt column2.txt

bar
quux
garply waldo

thud
awk -F' \\|\\|\\| ?' '{print $1 > "column1"; print $2 > "column2"}' file

或更一般地

awk -F' \\|\\|\\| ?' '{for(i=1;i<=NF;i++) print $i > "column"i}' file

You could try 你可以试试

cat /tmp/a | tr -s '|' | cut -d'|' -f1 #for part 1

cat /tmp/a | tr -s '|' | cut -d'|' -f2 | sed -E "s/^[[:space:]]+//g" #for part 2

The tr flag squeezes delimiters together. tr标志会将定界符一起压缩。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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