Bash 拆分文本文件';将以特定字符开头的行放入单独的文件中
在bash中是否有一行解决方案可以将所有以相同整数开头的句子移动到同一个文件中(保留它们的顺序) 要将以相同数字开头的行移动到单独的文件0、1、2等中,请执行以下操作:Bash 拆分文本文件';将以特定字符开头的行放入单独的文件中,bash,Bash,在bash中是否有一行解决方案可以将所有以相同整数开头的句子移动到同一个文件中(保留它们的顺序) 要将以相同数字开头的行移动到单独的文件0、1、2等中,请执行以下操作: awk '{print > $1".txt"}' file 这将使用awk获取每行的第一个字段,并将其用作文件名(连同“.txt”)。然后,打印输出到给定文件 对于给定的输入,请查看输出: $ tail *.txt ==> 0.txt <== 0 ||| the shortage of snow in mo
awk '{print > $1".txt"}' file
这将使用awk
获取每行的第一个字段,并将其用作文件名(连同“.txt”)。然后,打印
输出到给定文件
对于给定的输入,请查看输出:
$ tail *.txt
==> 0.txt <==
0 ||| the shortage of snow in mountain stirred hoteliers
0 ||| the shortage of snow in mountain stirred hotel
==> 1.txt <==
1 ||| the runways deserted pose any problem that operators ski .
1 ||| the runways deserted do not pose a problem that operators of ski .
==> 2.txt <==
2 |||
请务必注意,我们需要使用>
来追加。否则,每次我们都将删除文件中以前的所有内容。肯定:
awk '{print > $1".txt"}' file
这将使用awk
获取每行的第一个字段,并将其用作文件名(连同“.txt”)。然后,打印
输出到给定文件
对于给定的输入,请查看输出:
$ tail *.txt
==> 0.txt <==
0 ||| the shortage of snow in mountain stirred hoteliers
0 ||| the shortage of snow in mountain stirred hotel
==> 1.txt <==
1 ||| the runways deserted pose any problem that operators ski .
1 ||| the runways deserted do not pose a problem that operators of ski .
==> 2.txt <==
2 |||
请务必注意,我们需要使用
>
来追加。否则,每次我们都会删除文件中以前的所有内容。awesome。它可以工作,但会抱怨打开的文件太多。正如预期的那样,创建的文件数将介于2000-4000个文件之间。Mmmm然后您可能可以在打印后关闭它们:awk'{file=$1.txt”;print>file;close(file)}file
。你能试试吗?它不再抱怨:),但似乎覆盖了同一个文件。所以现在每个文件中只有一行。@user3639557 ooops那么我们应该使用>
来追加,而不是
!我的错我以前没提过。完美:)太棒了:)太棒了:)太棒了。它可以工作,但会抱怨打开的文件太多。正如预期的那样,创建的文件数将介于2000-4000个文件之间。Mmmm然后您可能可以在打印后关闭它们:awk'{file=$1.txt”;print>file;close(file)}file
。你能试试吗?它不再抱怨:),但似乎覆盖了同一个文件。所以现在每个文件中只有一行。@user3639557 ooops那么我们应该使用>
来追加,而不是
!我的错我以前没提过。完美:)太棒了:)太棒了:)