Find the answer to your Linux question:
Results 1 to 6 of 6
So.. I thought I understood how xargs worked for the most part, but it seems I don't as well as I thought. Why does: Code: find (foo) | cut (fields ...
  1. #1
    Just Joined!
    Join Date
    Oct 2009
    Posts
    22

    Confusion about xargs...

    So.. I thought I understood how xargs worked for the most part, but it seems I don't as well as I thought. Why does:
    Code:
    find (foo) | cut (fields and delimter)
    give me exactly what I expect, the files the find execution turned up, trimmed by whatever the cut command specifies, but
    Code:
    find (foo) | xargs -0 -I NAME cut NAME (fields and delimeter)
    goes all sorts of wonky? I was under the impression xargs took the lhs of the pipe and rather than passing it to the rhs as one single large argument handed it over in sequence, akin to a loop, to escape the argument list size limitation?

    A more specific example I guess would be something like..
    Code:
    find . -maxdepth 1 -name "*.cpp"
    will turn up a series of .cpps in the current dir.. then say you want to trim out the middle (for whatever reason, bear with me, I'm making this up as I go)
    Code:
    find . -maxdepth 1 -name "*.cpp" | cut -f2 -d'.'
    THAT works. But I can't make an xargs command out of it. I thought it would be something akin to:
    Code:
    find . -maxdepth 1 -name "*.cpp" | xargs -0 -I FILE cut FILE -f2 -d'.'
    Where FILE would be replaced with the values retrieved by find. But creating a few random cpps right now and trying it I get:
    Code:
    cut: ./foo.cpp
    ./bar.cpp
    ./cutcutcut.cpp
    ./main.cpp
    : No such file or directory
    Am I missing something? This approach works for just about every other instance I've used xargs for before, but I'm starting to wonder if that was just by coincidence and I've totally understood it wrong this entire time...?

  2. #2
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    You know something about xargs, so I don't think I'll need to explain this:
    Code:
    #!/usr/bin/env bash
    
    # @(#) s1	Demonstrate xargs.
    
    # Infrastructure details, environment, commands for forum posts. 
    echo
    set +o nounset
    LC_ALL=C ; LANG=C ; export LC_ALL LANG
    echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
    echo "(Versions displayed with local utility \"version\")"
    c=$(  ps | grep $$ | awk '{print $NF}' )
    version >/dev/null 2>&1 && s=$(_eat $0 $1) 
    [ "$c" = "$s" ] && p="$s" || p="$c"
    version >/dev/null 2>&1 && version "=o" $p xargs
    set -o nounset
    echo
    
    # Create a few files.
    
    FILES="a.cpp b.cpp c.cpp"
    rm -f $FILES
    touch $FILES
    ls -lgG $FILES
    
    echo
    echo " Results:"
    find . -maxdepth 1 -name "*.cpp" | cut -f2 -d'.'
    
    echo
    echo " Results:"
    find . -maxdepth 1 -name "*.cpp" | xargs
    
    echo
    echo " Results:"
    find . -maxdepth 1 -name "*.cpp" | xargs cut -f2 -d. 
    
    echo
    echo " Results:"
    find . -maxdepth 1 -name "*.cpp" | xargs -I FILE echo FILE | cut -f2 -d. 
    
    exit 0
    producing:
    Code:
    % ./s1
    
    Environment: LC_ALL = C, LANG = C
    (Versions displayed with local utility "version")
    OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
    Distribution        : Debian GNU/Linux 5.0 
    GNU bash 3.2.39
    xargs (GNU findutils) 4.4.0
    
    -rw-r--r-- 1 0 Jan 23 12:04 a.cpp
    -rw-r--r-- 1 0 Jan 23 12:04 b.cpp
    -rw-r--r-- 1 0 Jan 23 12:04 c.cpp
    
     Results:
    /c
    /b
    /a
    
     Results:
    ./c.cpp ./b.cpp ./a.cpp
    
     Results:
    
     Results:
    /c
    /b
    /a
    Best wishes ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  3. #3
    Just Joined!
    Join Date
    Oct 2009
    Posts
    22
    You.. might need to explain a LITTLE. Like I said, I'm starting to think my understanding of xargs wasn't as good as I thought before, so let me ask this: why does echoing the names over to cut work, while strictly passing the names to xargs fails?

  4. #4
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    Suppose there is only one file, "t1.cpp".

    What is the difference between:
    Code:
    cut -f2 -d"." t1.cpp
    and
    Code:
    echo t1.cpp | cut -f2 -d"."
    Does
    Code:
    cut t1.cpp -f2 -d"."
    make any sense?
    cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  5. #5
    Just Joined!
    Join Date
    Oct 2009
    Posts
    22
    Ah.. yes.. that does make a bit more sense now.. the issue was more with cut than xargs XD Thanks, appreciate it.

  6. #6
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    You're welcome. I think you would have gotten the solution by yourself, but a hint sometimes helps ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

Posting Permissions

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