Reputation: 1
I want to split my text file by line count
ex
GenTextFile.txt
have 3000 line I want split to
GenText_Output_1.txt
>> 1000 line (line 1 - 1000)
GenText_Output_2.txt
>> 1000 line (line 1001 - 2000)
GenText_Output_3.txt
>> 1000 line (line 2001 - 3000)
get 3 parameter input from console are input name, output name, line count to split
but when I execute, It have a problem
/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
Am I doing something wrong?
#!/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;
Thanks.
Upvotes: 0
Views: 4305
Reputation: 1
I try to follow, but the problem persists.
awk: syntax error near line 1.
awk: bailing out near line 1.
And I was found a solution here
Solaris is well known for the fact that the some commands under /bin /usr/bin are not POSIX compliant. Instead they have additional compliant versions under /usr/xpg4 and similar hierarchies.
Thus, under Solaris you can use just:
/usr/xpg4/bin/awk -v NAME=MACHINE '$1 == NAME' /etc/hosts
Under Solaris 10 this works.
When I use the command man awk
, I found that I was running SunOS 5.9.
Then I replace awk
with usr/xpg4/bin/awk
It worked!
@Barmar Thank you very much for the advice about awk command line.
-- This is my code --
input_name=$1
output_name=$2
line_split=$3
/usr/xpg4/bin/awk -v "out_name=$output_name" -v "line=$line_split" 'NR%line==1{x=++i;}{print > out_name""x".txt"}' ${input_name}
Upvotes: 0
Reputation: 780974
You need to do it in just one awk
command, not two separate commands. And the input file doesn't need to be an awk variable, it's just a command line argument.
awk -v "out_name=$output_name" -v "line=$line_split" 'NR%line==1{x=++i;}{print > (out_name"_"x".txt")}' "$input_name"
You can also use the split
command (this requires the version from GNU coreutils):
split --numeric-suffixes --lines=$line_split "$input_name" "$output_name"_
Upvotes: 3