I have this script to divide a file over a 100000 or even more in 50000 lines.

   echo $1 $2|awk '{d=$1;}                             
   BEGIN {a=1;b=0}                                     
      { f=d"0"a;                                       
        while ((getline x<d)>0)                        
          { print x>>f".txt"                           
            if (b++>=50000)                            
  END {print "Terminado ... ! "}'                      

if [ -f $1 ]; then                                     
        desc $1 $2                                     

When I execute it I got this error message:

sh-3.2$ parte.sh pa.txt                                 
: command not found                                     
'/parte.sh: line 2: syntax error near unexpected token `
'/parte.sh: line 2: `desc()                             

Can anyone help to solve it?

  • How do you execute this? source script_name ? You probably need to add #!/bin/bash if your script is executable – Anthon Oct 22 '13 at 21:24
  • 1
    The script is fine. However split -l 50000 pa.txt pa is much easier. If you want to keep the pa01.txt format, run this next: let c=1; for i in pa??; do mv $i $(printf "pa%02d.txt" $c); let c++; done – forcefsck Oct 22 '13 at 21:38
  • 4
    Sounds like classic carriage return issue (lines ended with CRLF instead of LF as if written for Microsoft OSes). – Stéphane Chazelas Oct 22 '13 at 22:12

The first error message is : command not found rather than the usual format bash: SOMECOMMAND: command not found. This indicates that something fishy is going on.

What is going on is that your script contains carriage return characters at the end of some lines. Under Windows, a newline is represented by the two-character sequence CR,LF (carriage return followed by line feed). Under all Unix flavors, a newline is represented by the character LF only. Bash sees a line like SOMECOMMAND␍ were is a CR character, which it parses as a call to the command called SOMECOMMAND␍. Since there is no such command, bash displays its usual error message.

In the terminal, the control character CR moves the cursor back to the beginning of the line. So the rest of the message overwrites the command name.

The fix is not to use a Windows editor to edit shell scripts, or to configure your editor not to put CR characters in shell scripts.

To convert an existing file in place under Linux or Cygwin from Windows line endings to Unix line endings, you can use the following command. It works even if the original file has mixed Windows and Unix line endings.

sed -i -e 's/\r$//' parte.sh
| improve this answer | |

Use split(1), e.g. split --lines=50000 pa.txt (or set your own prefix). Probably much, much faster than poor awk.

   Output  fixed-size  pieces of INPUT to PREFIXaa, PREFIXab, ...; default
   size is 1000 lines, and default PREFIX is `x'.  With no INPUT, or  when
   INPUT is -, read standard input.
| improve this answer | |

If this is an sh script, it looks like the error is in your function definition.


Should be:

function desc {

Script example:

function desc {
echo Hi
echo Starting
echo Ending

| improve this answer | |
  • Have you tried? The way it is works fine. – Kevin Oct 22 '13 at 21:53
  • I tried it under an old version of Red Hat Cygwin and the very first thing I changed included the old function definition method and the shebang. Shell script functions should be declared "function_name()". However, I assumed the OPs error might have something to do with an old or non-compliant shell along with missing the shebang. The next error the exact same script had was "awk: cmd. line:10: (FILENAME=- FNR=1) fatal: expression for ``<' redirection has null string value``" In some of the answers above why not use unix2dos? I thought that was standard with Cygwin. – tamarintech Oct 24 '13 at 14:30

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.