Help with find, xargs and awk

Hi,

I want to find some files and then search for some lines in it with a particular pattern and then write those lines into a file. To do this I am using something like this from command prompt directly.

cd /mdat/BVG
find -name "stmt.*cl" -newer temp.txt | xargs -i awk '/BVG-/{print}' {} > /home/sandhya/list.txt
#awk -f BVG-stmt.awk /home/sandhya/list.txt > /home/sandhya/BVG-Statement-detail.txt

This works absolutely fine. But I have lots of directories in mdat and I have to do the same action in all of it. So I decided to write a shell script so that it works for all. Below is the shell script code for it. But the problem is that when I execute the above commands from shell script, I do not get the desired output. Can anybody help me out?

*****************************************************
Shell Script
*****************************************************

DESTDIR=/home/sandhya
CLIENT_NAME=

while [ -z "${CLIENT_NAME}" ]
do
    echo "Enter Client Short Name :(<Ctrl>-C, <Enter> to cancel): \c"
    read CLIENT_NAME
done

CLIENT_NAME=`echo $CLIENT_NAME | tr '[:lower:]' '[:upper:]'`

if [ -d /mdat/${CLIENT_NAME} ]
    then
    echo "Client is valid!!"
else
    echo "\n Client ${CLIENT_NAME} does not exist. Try again!!\n"
    exit 1
fi

cd /mdat/${CLIENT_NAME}

LNAME=${DESTDIR}/list.txt
#SDNAME=${DESTDIR}/${CLIENT_NAME}-Statement-detail.txt
#AWKF=${CLIENT_NAME}-stmt.awk

find -name "stmt.*cl" -newer temp.txt | xargs -i awk '/${CLIENT_NAME}-/{print}' {} > ${LNAME}
#awk -f ${AWKF} ${LNAME} > ${SDNAME}

*********************************************************

Note: The contents of awk file is same only difference is the pattern BVG replaced with other appropriate client names/dirs.
OS: QNX 4.25

Thanks in advance for your help.

Sandhya.

Change

find -name "stmt.*cl" -newer temp.txt | xargs -i awk '/${CLIENT_NAME}-/{print}' {} > ${LNAME}

to

find . -name "stmt.*cl" -newer temp.txt | xargs -i awk -v cn="$CLIENT_NAME-" 'index($0,cn)' {} > $LNAME

And if you are running this in a loop, change > to >> .

Hi Gotham,

When I used the command your way , i get file of 0 size. But the earlier code was giving me file of 307 KB.

Sandhya.

Sandhya,
try to change ur code with double quotes and let me know ..

awk "/${CLIENT_NAME}-/{print}"

Sandhya && Lohith:
Where is the >> in your code? That is the problem. Elixir gave instructions about adding >>.

I should also mention: if you rerun code with different input files truncate (set to zero length"

> $LNAME

at the VERY first line of your code. Otherwise >> will add the new data to the END of the old data.

If your find supports the POSIX-standard -exec ... {} + primary, then you don't need xargs. You can just use find to invoke cat with many file arguments and feed that directly to awk.

Regards,
Alister

Thanks lohitdutta. Double quotes worked!! Can you explain the difference in behaviour of Single and double quote when used in script?

Also I tried to use cat and grep instead of xargs and awk and I got the correct result.

Now which is better? xrags-awk combi or cat-grep combi with find.

Sandhya.

ywc ::slight_smile:
while you use variables in awk within script .. double quotes are essential to interpolate those variables..

I think xrags-awk combination will b the smart choice ..what do u say guys ?!!