0

I want to split the large file into pieces. Filename and the number records per file after split will be provided as arguments/variables to awk.

EG: FILENAME=A_20 (say it has 100 records), NoOfRecordsPerFile=50 The splitted file names must be

A_20-1.txt
A_20-2.txt

The command I am using is:

awk -vNoOfRecordsPerFile -vFILENAME 'NR%NoOfRecordsPerFile==1{x=FILENAME"-"++i".txt"}{print > x}' $FILENAME

I am getting error :

awk: (FILENAME=A_20 FNR=1) fatal expression for `>' has null string value

Am I doing anything wrong.

The same command is working for the other file.

  • Which awk are you using? -vFILENAME and -vNoOfRecordsPerFile are not valid for mawk or GNU awk. – muru Jul 20 '18 at 7:21
  • try double quotes for x. – msp9011 Jul 20 '18 at 7:23
  • I am using gawk – Sreekesh Darshi Jul 20 '18 at 7:24
  • I tried doing this with the shell script variables as well. but the same issue. Command I used is awk NR%"'$NoOfRecordsPerFile'"==1{x="'$FILENAME'""-"++i".txt"}{print > x} $FILENAME. But still I get the same error. My filename is around 48 characters, this should not be a problem I think. – Sreekesh Darshi Jul 20 '18 at 7:33
  • Your comment and question show different commands. Please edit your post to include the actual command you're running. – muru Jul 20 '18 at 7:43
2

You're using the FILENAME variable with an empty value (you never give FILENAME a value on the command line), this should produce an error even before your awk script starts running. awk already has a variable called FILENAME that you can use. It's the name of the current input file.

Using -vNoOfRecordsPerFile you say you want to assign a value to an awk variable on the command line, but you never give it a value. This should produce a different error from the one you say you're getting. If you wanted it to take the value of the shell variable with the same name, then use -v NoOfRecordsPerFile="$NoOfRecordsPerFile".

So, the final awk command becomes:

awk -v NoOfRecordsPerFile="$NoOfRecordsPerFile" '
    ((NR-1) % NoOfRecordsPerFile) == 0 { x = FILENAME "-" ++i ".txt" }
                                       { print > x                   }' "$FILENAME"
  • OP says they're using GNU awk, and with gawk, I get an error if I try -v FILENAME without assigning a value: gawk: `FILENAME' argument to `-v' not in `var=value' form – muru Jul 21 '18 at 11:54
  • @muru They are 1) not using gawk, or 2) not getting the error they say they are getting, or 3) not running the code they say they are running. – Kusalananda Jul 21 '18 at 12:21
0

Your error comes from the NR%NoOfRecordsPerFile==1 condition that is not met before using the variable x, so that variable isn't set.

Instead of awk, you could use split:

split -l 50 -d --additional-suffix='.txt' --numeric-suffixes=1 A_20 'A_20-'

This splits the file A_20 into 2 files of 50 lines called A_20-01.txt and A_20-02.txt.


If you want to use awk, you can try this:

awk -v lineno=50 '((NR-1)%lineno)==0{cnt++;f=FILENAME "-" cnt ".txt"}{print >>f}' A_20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.