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 ...
- 03-24-2011 #1Just 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:
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:ls -1 *.txt | xargs -i sh -c 'NEW=`echo $0 | awk -F'_' '{print $1}'`; echo $NEW' {}
I haven't been able to figure out how to escape the awk command properly. Any help would be greatly appreciated.Code:}`; echo $NEW: -c: line 0: unexpected EOF while looking for matching ``' }`; echo $NEW: -c: line 1: syntax error: unexpected end of file
Thanks in Advance
- 03-25-2011 #2
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.


Reply With Quote