Find the answer to your Linux question:
Results 1 to 5 of 5
I am currently writing a script to compare a file list created over an FTP connection to a local directory. I have cleaned the FTP file list up so that ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jul 2010
    Posts
    3

    prepending text conditionally


    I am currently writing a script to compare a file list created over an FTP connection to a local directory.
    I have cleaned the FTP file list up so that I just have a raw list of filenames however due to the directory structure employed (both locally and on the ftp site) I need to prepend each line with a directory name based on the first letter of the filename. For filenames that are numeric, the directory name is '123'. From this, I can perform a diff in order to give me a manifest of files missing locally to be downloaded using curl or wget.

    Suggestions please?

  2. #2
    Linux Enthusiast meton_magis's Avatar
    Join Date
    Oct 2006
    Location
    arizona
    Posts
    699
    awk could be used for this. something along the lines of

    awk '$1 ~ /^[1-9].*/ {print "/directory/" $1}'

    replacing $1 with whatever column the text is in. Awk breaks stuff down into 2 parts, a condition, and an action. The condition here is 'if column 1 starts with [1-9],' the action is {print /directory/column1}

    it's kinda confusing at first, but mess around with it. You could create some complex stuff with it.
    New to the internet, technical forums, or the hacker / open source community??
    Read this to learn good posting habits http://www.catb.org/~esr/faqs/smart-questions.html

    RHCE for RHEL version 5
    RHCT for RHEL version 4

  3. #3
    Just Joined!
    Join Date
    Jul 2010
    Posts
    3
    OK I think I get the idea - will have a play

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Enthusiast meton_magis's Avatar
    Join Date
    Oct 2006
    Location
    arizona
    Posts
    699
    you can run it from the command line as shown, but you can also run it as a script file. make a file like

    Code:
    #!/bin/awk
    $1 ~ /^[1-9].*/ {print "/directory/" $1}
    then run (for example)
    echo "123asdf" | awk --exec=filename

    it will output
    /directory/123asdf

    you can add more lines, and it will check each line of input text against all paterns. You can get a lot more advanced, but that is the limit of my awk knowledge.
    New to the internet, technical forums, or the hacker / open source community??
    Read this to learn good posting habits http://www.catb.org/~esr/faqs/smart-questions.html

    RHCE for RHEL version 5
    RHCT for RHEL version 4

  6. #5
    Just Joined!
    Join Date
    Jul 2010
    Posts
    3
    Thanks - started playing with awk, but then somebody gave me this suggestion using sed

    sed 's|.|/&/&|;s|^/[0-9]|/123|' infile

    where infile is the file containing the list of files.

Posting Permissions

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