Results 1 to 5 of 5
  1. #1
    Join Date
    Sep 2014
    Posts
    1

    Unanswered: Concatenating Multiple files using Wildcard character

    Hi All,

    I relatively new to unix scripting. I am facing problem while trying to concatenate multiple files using wildcard character. The problem is that I have HDR, DAT, TLR in each file. After concatenating when I look at the output file I see that HDR record of the next file gets padded on the Trailer record of the previous file.

    Code:

    cat /hom/user1/dir1/source/sampfile* > /home/user2/dir1/target/concatfile.txt

    source files:

    sampfile1:
    HDR,aa33343,2014-09-18
    DAT,32473,USD,2014-10-19,CASH1
    TLR,1

    sampfile2:
    HDR,bb33343,2014-09-18
    DAT,32473,USD,2014-10-19,CASH2
    TLR,1

    sampfile3:
    HDR,cc33343,2014-09-18
    DAT,32473,USD,2014-10-19,CASH3
    TLR,1

    Output:

    HDR,aa33343,2014-09-18
    DAT,32473,USD,2014-10-19,CASH1
    TLR,1HDR,bb33343,2014-09-18
    DAT,32473,USD,2014-10-19,CASH2
    TLR,1HDR,cc33343,2014-09-18
    DAT,32473,USD,2014-10-19,CASH3
    TLR,1


    I want the o/p to be as follows,

    HDR,aa33343,2014-09-18
    DAT,32473,USD,2014-10-19,CASH1
    TLR,1
    HDR,bb33343,2014-09-18
    DAT,32473,USD,2014-10-19,CASH2
    TLR,1
    HDR,cc33343,2014-09-18
    DAT,32473,USD,2014-10-19,CASH3
    TLR,1


    Please help me with this issue. Thanks in advance.

  2. #2
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    There is not a line feed at the end of the last line of the input file.
    You have several choices:
    Code:
    rm concatfile
    for file in samplefile*
    do
      echo "\n" >>$file
      cat $file >>concatfile
    done
    If you have access to the code that creates the file, add the final line feed there.

  3. #3
    Join Date
    May 2014
    Posts
    6
    hi,

    Code:
    echo "\n" > "$file"
    this will add litteral "\n" to the file, not a line feed
    and better is to not add line feed to files, but to concatenated file:
    Code:
    for f in samplefile*; do cat "$f"; echo; done > concatenatedFile

  4. #4
    Join Date
    Sep 2009
    Location
    Ontario
    Posts
    1,057
    Provided Answers: 1
    this will add litteral "\n" to the file, not a line feed
    Not on Unix systems, but does on Linux without the -e option.

    I prefer your solution of adding the line feed to the output file rather than modifying the input file.

  5. #5
    Join Date
    Jun 2003
    Location
    West Palm Beach, FL
    Posts
    2,713
    Nevermind...
    The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •