Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Unix Problems

  • 01-03-2011 1:30pm
    #1
    Registered Users, Registered Users 2 Posts: 86 ✭✭


    I'm using SUSE csh

    I have a unix problem.

    I want a command line to search a unix hierarchy for files with a pipe “|”in their name and rename them to dash “-”.

    any ideas?


Comments

  • Registered Users, Registered Users 2 Posts: 86 ✭✭RobbieM


    This is what I'm testing so far...
    find ../ | egrep '\|' | xargs -I {} -e echo cp '"{}"' \`echo '"{}"' \| sed "'s/|/-/g'" \`
    It's close. eventually change the "echo cp" for "mv"


  • Registered Users, Registered Users 2 Posts: 40,038 ✭✭✭✭Sparks


    Moved to the Unix forum.


  • Registered Users, Registered Users 2 Posts: 218 ✭✭Tillotson


    There's probably a much nicer way to do this using the `-exec` option or xargs.
    for old in $( find test/* -name "*|*"  -type f )
    do
        new="$( echo $old | sed 's/|/-/' )"
        mv $old $new
    done
    

    Hopefully someone who knows what they're doing will come along soon.


  • Registered Users, Registered Users 2 Posts: 1,110 ✭✭✭Skrynesaver


    tested, it works only assumption is that you shoot users who put spaces in file names, also assuming you will henceforth shoot users who put pipes in file names, but that's not necessary for the script to work ;)
    for i in $(find ./files -name \*\|\* -exec echo {} \;) ;do 
    mv  $i $(echo $i| sed 's/|/-/g')
    done
    


  • Registered Users, Registered Users 2 Posts: 2,771 ✭✭✭niallb


    ... you shoot users who put spaces in file names, also assuming you will henceforth shoot users who put pipes in file names, but that's not necessary for the script to work ...

    It's just necessary.


  • Advertisement
  • Moderators, Recreation & Hobbies Moderators, Science, Health & Environment Moderators, Technology & Internet Moderators Posts: 94,296 Mod ✭✭✭✭Capt'n Midnight


    also assuming you will henceforth shoot users who put pipes in file names,
    and in the microsoft world they've stuck in &'s and +'s and reserved devices like .con

    fairly unparsable on the command line be glad you're not working with windows


    in the windows world however, you can fall back to 8.3 names to avoid spaces and some dodgy characters - is there something similar in linux/unix or should one just use something like sed and pick a delimiter you know isn't in use anywhere ?


  • Closed Accounts Posts: 4,564 ✭✭✭Naikon


    Best to use xargs where the param list is large. This is because -exec runs a seperate instance of the command to be run for each match.


  • Registered Users, Registered Users 2 Posts: 86 ✭✭RobbieM


    %> set tag = '"*log.gz"' ; set fin = "find . -name $tag" ; echo "$fin" ; pwd ; $fin
    find . -name "*log.gz"
    /tmp/test1
    find: No match.
    %> find . -name "*log.gz"
    ./testfile.log.gz
    ./testfil2.log.gz
    ./testfil3.log.gz

    Why is there no match within the command line, but the find works following the command line?


  • Registered Users, Registered Users 2 Posts: 86 ✭✭RobbieM


    Stuck on this... This is the biggest challenge I have come up against. It will take a great programmer to figure this one out !

    here it is:

    take a file list (with some multi element lines) and reformat into stacked columns for a csv.

    example:



    ""Similar problem. I revisited this again recently but to no avail.

    all_errors
    bin_error exe_error noa_error_error noa
    bin_error exe_error test_error noa tap tag_error
    bin_error exe_error noa tap tag_error lvis
    bin_error
    exe_error
    test_error
    noa
    to


    [Embedded Image Removed]

    some errors in my paste above... basically list to stacked cols.

    It’s impossible!!!!""


    I will post my solution if I ever find one. Trying to do this in csh, and therein lies the problem. Going to learn Perl or Ruby or tcl to try solve.




    | noa_error | test_error |
    | exec_errors | exec_errors | exec_errors
    all_errors | bin_error | bin_error | bin_error | bin_error exe_error | test_error | noa |


  • Registered Users, Registered Users 2 Posts: 35 mobitron


    RobbieM wrote: »
    %> set tag = '"*log.gz"' ; set fin = "find . -name $tag" ; echo "$fin" ; pwd ; $fin
    find . -name "*log.gz"
    /tmp/test1
    find: No match.
    %> find . -name "*log.gz"
    ./testfile.log.gz
    ./testfil2.log.gz
    ./testfil3.log.gz

    Why is there no match within the command line, but the find works following the command line?

    Because csh is useless for this kind of thing. It works OK for interactive commands that you only want to run once. If you try doing anything (even basic stuff) with variables, redirection or filename matching it gets hopelessly confused.

    If you're trying to create any kind of useful shell script, use ksh or bash.

    To do what you want in ksh:
    tag="*log.gz"; echo $tag; fin="find . -name $tag";echo $fin; pwd; $fin
    *log.gz
    find . -name *log.gz
    /appl
    ./common/testfile2.log.gz
    ./common/testfile3.log.gz
    ./common/testfile.log.gz
    


  • Advertisement
Advertisement