Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

desc()                                                 
   {                                                   
   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)                            
               {                                       
                 close(f);b=0;a++;f=(a<10)?d"0"a:f=d""a
               }                                       
          }                                            
      }                                                
  END {print "Terminado ... ! "}'                      
  }                                                    

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

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()                             
sh-3.2$                                                 

Can anyone help to solve it?

share|improve this question
    
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
add comment

3 Answers 3

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

DESCRIPTION
   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.
share|improve this answer
add comment

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
share|improve this answer
add comment

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

desc()
{

Should be:

function desc {

Script example:

#!/bin/bash
function desc {
echo Hi
}
echo Starting
desc
echo Ending

share|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. –  esnyder Oct 24 '13 at 14:30
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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