Find the answer to your Linux question:
Results 1 to 2 of 2
Hi, I have run into this issue repeatedly and haven't been able to find a relevant thread. I often want to extract some info using awk from a variable/filename while ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Mar 2011
    Posts
    1

    using awk within an (ba)sh command via xargs


    Hi,
    I have run into this issue repeatedly and haven't been able to find a relevant thread.
    I often want to extract some info using awk from a variable/filename while running other things using xargs and sh. Below is an example:
    Code:
    ls -1 *.txt | xargs -i sh -c 'NEW=`echo $0 | awk -F'_' '{print $1}'`; echo $NEW' {}
    In the above case I would like to grab just the first field from a filename (delimited by '_') from within an sh command. This is a simplified example, where normally I would be doing some further data processing with the sh command(s). The error message that I get is:
    Code:
    }`; echo $NEW: -c: line 0: unexpected EOF while looking for matching ``'
    }`; echo $NEW: -c: line 1: syntax error: unexpected end of file
    I haven't been able to figure out how to escape the awk command properly. Any help would be greatly appreciated.

    Thanks in Advance

  2. #2
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,345
    This approach is a little different, also contains bashisms, but should work

    Code:
    while read -r -d '' FILE; do echo NEW=$(echo ${FILE} | awk -F'_' '{print $1}'; echo ${NEW}) ;done < <(find ~/comparedir/ -type f -iname "*\.txt" -print0)
    You must always face the curtain with a bow.

Posting Permissions

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