Prev: use variable to do some substitution with awk.
Next: A Free Business Directory with Trading Platform. Add your Company
From: Ed Morton on 29 Mar 2010 23:47 On 3/29/2010 9:05 PM, houghi wrote: > I have a bash script that does the following: > DIFF=`xwininfo|grep 'Corners'|awk '{print $2}'` You never need grep if you're already using awk, the above is equivalent to: DIFF=`xwininfo| awk '/Corners/{print $2}'` > > This will give an otput like: > DIFF=+82+24 or DIFF=+1023+987 > > However the output must be +82,24 (or +1023,987) so the second + needs > to become a comma instead of a plus. I could remove the first character, > change the + to a , with sed and then add the + again, but I would think > that there is a more elegant solution. > > houghi DIFF=`xwininfo| awk '/Corners/{split($2,a,"+"); printf "+%s,%s\n",a[2],a[3]}'` Ed.
From: Chris F.A. Johnson on 29 Mar 2010 23:53
On 2010-03-30, houghi wrote: > I have a bash script that does the following: > DIFF=`xwininfo|grep 'Corners'|awk '{print $2}'` > > This will give an otput like: > DIFF=+82+24 or DIFF=+1023+987 > > However the output must be +82,24 (or +1023,987) so the second + needs > to become a comma instead of a plus. I could remove the first character, > change the + to a , with sed and then add the + again, but I would think > that there is a more elegant solution. printf "%s,%s\n" "${DIFF%+*}" "${DIFF#*"${DIFF%+*}"}" Or: temp=${DIFF%+*} printf "%s,%s\n" "${DIFF%+*}" "${DIFF#*"$temp"}" -- Chris F.A. Johnson, author <http://shell.cfajohnson.com/> =================================================================== Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress) Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress) ===== My code in this post, if any, assumes the POSIX locale ===== ===== and is released under the GNU General Public Licence ===== |