Code: Select all
#using shell builtins to replace long pipes of external commands
#
#while read A || [ "$A" ]; do A=`echo $A`;echo ${A};done <<<"${VAR}" |
#or
#while read A || [ "$A" ]; do A=`echo $A`;echo ${A};done <$FILE |
#can replace:
# echo "${VAR}" |tr "\t" " "|tr -s " "|cut/sed ... |
#or
# cat $FILE |tr "\t" " "|tr -s " "|cut/sed ... |
#
#both tr commands are replaced with VAR=`echo $VAR`
#cut is replace with the second <and third> echo using substring manipulation
#use:
# echo ${A#* * *} to remove the first 3 fields or ${A%* * *} to remove the last 3
#there is another alternative if you no longer need the input parameters
#use:
# set `echo $A`; echo $1,$5,$7 ... this uses the 1st,5th&7th " " separated word
#you can also use the IFS value if you want "|" to separate words
#ex. OLDIFS="${IFS}";IFS="|";...commands...;IFS="${OLDIFS}"
#sed can often be replaced with thie ${VAR///} substring manipulations
#use:
# echo ${A//"${OLDSTRING}"/"${NEWSTRING}"} as a sed replacement
#NOTE this method will work for multiple lines while sed will not
#NOTE on IFS and read, set etc...
#you can get creative with read and IFS too, but it can get difficult to follow
#normally read uses a new line (represented by \n) as the delimiter
#_some_ shells have -d parameter for read, but for portability set/reset IFS
#be careful though, setting IFS will affect other commands too, for example
#set `echo "a b c d e f"`; echo $1 .... "a b c d e f" when IFS="|" ...oops
#this is a feature, not a bug