Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2006
    Posts
    15

    Unanswered: Putting filenames on the same line

    Hi,

    I am trying to use a script to place some text in a file, then read the filenames from a folder, then follow it all with some more text. So that it end up something like this:

    starting_text_here file1 file2 file3 finishing_text

    But so far I have only managed to get it so that the files are on the same line, ie:

    starting_text_here
    file1 file2 file3
    finishing_text

    I have tried using the multi line stuff in sed but I don't really understand how to use it
    Thanks

  2. #2
    Join Date
    Jun 2003
    Location
    West Palm Beach, FL
    Posts
    2,713

    Cool


    Please clarify your requirements providing a sample input file(s) and sample result file(s) and the code to your script.

    Thanks.

    The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb

  3. #3
    Join Date
    Feb 2006
    Posts
    15
    OK, sorry.

    I have a list of files like this in a folder.

    90007-01-11-04Z
    90007-01-12-00
    90007-01-12-00A
    90007-01-13-00
    90007-01-13-00A
    90007-01-13-00Z
    90007-01-14-00
    90007-01-14-00A
    90007-01-14-00Z
    90007-01-15-00A

    What I want to do is insert a bit of code at the start of the line, then the file names, followed by a different bit of code.
    This is the code that I have so far.

    #insert combine script
    sed '$a\
    \
    \
    \${combi} all' $EXPATH/combine.tmp > $EXPATH/combine


    #search folder for name remove unwanted names, place on same line with awk
    ls $EXPATH | grep '^'"$FILE"'.*' | grep -v '_' | grep -v '\.' |
    awk '{
    printf ("../%s ",$1)
    }' >> $EXPATH/combine


    sed '$a\
    -addclusters -nosyst -top\
    ' $EXPATH/combine > $EXPATH/combine.tmp

    I have so far managed to get all the filenames on the same line, but I can't get the code onto the same line

    thanks

  4. #4
    Join Date
    Jun 2003
    Location
    West Palm Beach, FL
    Posts
    2,713

    Cool



    Your first grep (grep '^'"$FILE"'.*') combined with the the third grep ( grep -v '\.' ) may not work.

    I assume you are wanting to list (ls) all file that begin with "$FILE"? excluding those that have an underscore (_) and dot (.).

    Do you want the result in the "$EXPATH/combine" file to be something like this???:

    -addclusters -nosyst -top ../file1 ../file2 ../file3

    Ok try this:
    Code:
    EXPATH=/path/to/files
    FILE='90007'
    echo "`ls -1 $EXPATH | grep "${FILE} | grep -v '_' | grep -v '\.' `"|\
    awk 'BEGIN{printf "-addclusters -nosyst -top ";}
    {printf " ../%s ",$1;}
    END {print ";"}
    ' >> $EXPATH/combine











    The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb

  5. #5
    Join Date
    Feb 2006
    Posts
    15
    Thankyou so much!! You have just removed some of the most cumbersome parts of my code!!

    The grep parts appear to work, although I recognise that there may be an issue if I am not careful and there are unwanted filenames without _ or .

    Thanks

  6. #6
    Join Date
    Jun 2003
    Location
    West Palm Beach, FL
    Posts
    2,713

    Cool


    Glad to help.
    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
  •