我有一个txt文件,其中包含一列单个单词:
windfall
winnable
winner
winners
winning
我想将文件中的单词用作映射作业的正则表达式字符串。完成后,单词应如下所示:
windfall|winnable|winner|winners|winning
我需要使用python或awk打开文件,放置一个|在每个文件的末尾,将新内容写入新文件,添加新字符并将列转换为单个水平线。
有什么建议吗?
答案 0 :(得分:3)
最简单的是tr
:
tr '\n' '|' < file.txt
答案 1 :(得分:1)
使用Python你可以做到:
with open('oldfile.txt') as fin:
with open('newfile.txt', 'w') as fout:
fout.write('|'.join(map(str.strip, fin)))
str.split
删除换行符和空格,而join
将行与|
连接起来。
答案 2 :(得分:0)
使用sed
:
$ cat file
windfall
winnable
winner
winners
winning
$ sed ':a;N;s/\n/|/;ba' file
windfall|winnable|winner|winners|winning
:a
N
加载到执行空间答案 3 :(得分:0)
在awk中,如果你不想要跟踪|
:
$ awk '{ s=s (NR>1"?"|":"") $0 } END { print s }' file
windfall|winnable|winner|winners|winning
getline
的原始版本基本上是awk干扰会话的(甚至不是)结果:
$ awk 'BEGIN {
while(r=getline) { # read until EOF
s=s (p==r?"|":"") $0; # pile it to s, preceed with | after the first
p=r # p revious r eturn value of getline
} print s # out with the pile
}' file
windfall|winnable|winner|winners|winning
答案 4 :(得分:0)
awk -v RS= -v OFS="|" '/ /{next}$1=$1' file
windfall|winnable|winner|winners|winning
答案 5 :(得分:0)
使用paste
:
$ cat /tmp/so.txt
windfall
winnable
winner
winners
winning
$ paste -sd'|' /tmp/so.txt
windfall|winnable|winner|winners|winning