【发布时间】:2013-09-23 18:34:18
【问题描述】:
我想按行数分割我的文本文件
前
GenTextFile.txt 有 3000 行我想拆分到
GenText_Output_1.txt >> 1000 行(第 1 - 1000 行)
GenText_Output_2.txt>>1000行(1001-2000行)
GenText_Output_3.txt >> 1000行(2001-3000行)
从控制台获取 3 个参数输入,分别是输入名称、输出名称、要拆分的行数
但是当我执行时,它有问题
/devhome/See/Split_file > ./shell_call_awk.sh GenTextFile.txt GenText_Output 1000
awk: syntax error near line 1
awk: bailing out near line 1
awk: can't open in_name
我做错了吗?
-- 这是我的代码--
#!/bin/ksh
#echo "input name : $1"
#echo "output name : $2"
#echo "line split : $3"
input_name=$1
output_name=$2
line_split=$3
awk -v "in_name=$input_name" -v "out_name=$output_name" -v "line=$line_split"
awk 'NR%line==1{x=++i;}{print > out_name"_"x".txt"}' in_name
exit 1;
谢谢。
【问题讨论】:
-
有什么理由不只是使用
split命令? -
@Barmar 我使用了 split 命令,文件名的结果是 -> filenameaa,filenameab,filenameac.. 但我希望结果名是 -> filename1, filename2, filename3.. .. 我是 shellscript 的新手,我不知道如何使用 split 命令然后结果如预期。
-
您看过手册页吗?
--numeric-suffixes选项不符合您的要求吗? -
你能展示一个命令的例子吗?我阅读了手册页但没有找到“--numeric-suffixes”选项我找到了这个模式。
split [-linecount | -l linecount] [-a suffixlength] [ file [name]]split [ -b n | nk | nm] [-a suffixlength] [ file [name]] -
抱歉,我以为您使用的是 GNU 版本的
split。在大多数 Linux 系统上都可以找到。