使用AWK将每个单词放在文本文件中的新行上

使用AWK将每个单词放在文本文件中的新行上

本文介绍了使用AWK将每个单词放在文本文件中的新行上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用AWK将文本文档中的每个单词放在新行上.我真的不知道如何使用AWK,但是我在网上找到了一些可以解决我问题的命令.我尝试了以下命令:

I'm trying to use AWK to place every word within a text document on a new line. I don't really know how to use AWK but I've found some commands online which should solve my problem. I've tried the following commands:

$ awk '{ for (i = 1; i <= NF; i++) print $i }' input.txt > output.txt

$ awk '{c=split($0, s); for(n=1; n<=c; ++n) print s[n] }' input.txt > output.txt

但是,这两个命令都具有相同的效果,即删除了所有空格.

However, both of these commands have the same effect, which is that all spaces are removed.

为清楚起见,可以说input.txt包含以下文本:

For clarity, lets say that input.txt contains the text:

The fox jumped over the dog

output.txt应包含:

output.txt should contain:

The
fox
jumped
over
the
dog

但是output.txt包含:

However output.txt contains:

Thefoxjumpedoverthedog

我正在Windows 7上使用Cygwin来使用这些命令.命令中是否缺少某些内容?

I'm using Cygwin on Windows 7 to use these commands. Is there something I'm missing within the commands?

推荐答案

根据联机帮助页,awk中的print打印其参数:

According to the manpage, print in awk prints its arguments:

因此您的第一个命令是可以的,但是您需要确保输出记录分隔符是新行.默认的输出记录分隔符是换行符,但请尝试确保:

So your first command is ok, but you need to make sure your output record separator is a new line. The default output record separator is a newline, but try making sure:

awk -v ORS='\n' '{ for (i = 1; i <= NF; i++) print $i }' input.txt > output.txt

仅在Cygwin上,您可能会遇到Windows/DOS行尾的问题.也尝试ORS='\r\n'.或者,将输出通过unix2dos传递给管道.

On Cygwin only, you might be running into issue with Windows/DOS line endings. Try also ORS='\r\n'. Alternatively, pipe the output through unix2dos.

这篇关于使用AWK将每个单词放在文本文件中的新行上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 17:33