doubts about shell scripting!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lilly07
    New Member
    • Jul 2008
    • 89

    doubts about shell scripting!

    I am trying to concatenate some 100 files from one directory and writing them into another directory and I want to automate this as follows:

    But cat statement inside for loop does not work. But echo statement inside the for loop shows the correct path to the files? Why is it not concatenating files? I am new to shell scripting. Thanks in advance.

    Code:
    if [ $# -lt 4 ]
            then
               echo Usage: $0 Directory st end lib_name
               exit 1
    fi
    
    echo $1 $2 $3 $4
    mkdir test
    
    dir=$1
    
    IN=$1
    IFS="/"
    bar=( $IN )
    #echo ${bar[3]}
    Name=${bar[3]}
    
    
    #contatenating test files
    for(( i = $2; i <= $3; i++ ))
            do
                   echo "$dir/Data/Int/Calls/s_${i}_1_0*qtest.txt"
                    cat $dir/Data/Int/Calls/s_${i}_1_0*test.txt > $4/$Name.txt
    
            done
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    If the data does not go to the output file where does it go?

    If it doesn't go to the screen it must be going somewhere so if it does not go to your desired output and it doesn't go to the screen that suggests that $4/$Name.txt is not producing the expected result. You should check (echo) what it contains to make sure it is correct.

    Comment

    • lilly07
      New Member
      • Jul 2008
      • 89

      #3
      Echo statement inside the for loop is correctly printing the file path. But I get to to see theerror like "cat... No file found " three times because the path is broken. How can it happen? How can the path gets broken in the cat statement but not in echo statement. Please clarify. Thanks in advance.

      Is there any problem because of line 11 and 12 of my code as I am trying to get the name of the file from directory path. No clue for me :)

      Comment

      • Nepomuk
        Recognized Expert Specialist
        • Aug 2007
        • 3111

        #4
        What are the file names, that the cat command has problems with? If there are any special characters (e.g. spaces) in the file name, that may be the problem here. As echo doesn't interpret the file names, just prints them, while cat actually tries to access the file, there might be a small error in or problem with the file name which you just missed.

        I see, that in your code you put quotation marks around the name with the echo command, not however with the cat command. Quotation marks would solve the space-problem, so a simple solution may be, to put them around the cat file name too.

        Greetings,
        Nepomuk

        Comment

        • Oralloy
          Recognized Expert Contributor
          • Jun 2010
          • 988

          #5
          Lilly07,

          You do realize that the filenames you give in lines 22 and 23 are one character different?

          In line 22, you call for
          qtest.txt
          and in line 23 you call for
          test.txt
          This shouldn't matter because of the preceeding asterisk, however you should be aware.

          Also, in line 23, you are using simple output redirection '>', and not output concatenation '>>'.

          Good Luck!
          Oralloy

          Comment

          • Aniket sonmale
            New Member
            • Jan 2011
            • 7

            #6
            what are the extensions of the file??

            for eg: if all your files are .txt files try this

            cat *.txt>>new.txt

            new.txt is your concatenated file.
            but make sure that the folder in which you execute your scripts contains the only .txt files which you want to concatenate.


            store it any where you want

            Comment

            Working...