The Simple Man's shell scripts
Monday, December 10, 2007
Datestamp and serial number incremented log file
LogFile="/tmp/logfile.$(date +%y%m%d)$(ls -l /tmp/logfile.$(date +%y%m%d)* 2>/dev/null | wc -l | sed 's/ //g').log"
Friday, December 7, 2007
Using a variable as awk's print field specifier
If you have the following input string:
1 to 6 : shirts and tops| 7 and 8 : shirts and tops| 9 to 11 : shirts and tops|12 : shirts and tops
Try This:
FieldCounter='1'
until [ $FieldCounter -gt $GirlsFieldCount ]
do
echo "$GirlsRanges" | awk -F"," "{print $$FieldCounter}"
((FieldCounter = FieldCounter + 2))
done
To pull out every other field.
I had a hard time figuring this one out! The key is double quotes around the print statement instead of single (usually it's '{print $1}'), then escaping the dollar sign so it wouldn't print the value of $$... then I had it.
Thursday, December 6, 2007
Search for skus
#!/bin/ksh
BigFile='/home/usrbinsed/Desktop/tmp/2008margins.csv'
SmallFile='/home/usrbinsed/Desktop/tmp/salesbypart07.csv'
BigFileCount=$(cat $BigFile | wc -l)
Counter='1'
MatchCounter='0'
until [ $Counter -gt $BigFileCount ]
do
CurrentRecord=$(cat "$BigFile" | awk 'FNR == '$Counter' {print}')
CurrentSKU=$(echo $CurrentRecord | awk -F"," '{print $1}')
SmallFileMatch=$(cat $SmallFile | awk -F"," '{print $1}' | grep -xn $CurrentSKU)
if [ -n "$SmallFileMatch" ]
then
MatchRecord=$(cat $SmallFile | awk 'FNR == '$(echo $SmallFileMatch | awk -F":" '{print $1}')' {print}')
echo "$CurrentRecord, $MatchRecord"
((MatchCounter = MatchCounter + 1))
else
echo "$CurrentRecord"
fi
((Counter = Counter + 1))
done
echo "Total Matches = $MatchCounter"
Subscribe to:
Posts (Atom)