I have a flat text file which contains a column of numbers.
The format of the number is nnnn.nn- (with minus sign at end).
I need to put the minus sign before the number ie -nnnn.nn
They have no sign
AND
they are not the only column in each record.
What do you think of this ;
while read CD CRN FUND AMT
do
if ( echo $AMT | grep -c "-" > /dev/null )
then
AMT=`echo $AMT | sed s/-// `
AMT=`echo "-"$AMT`
fi
echo "$CD \t $CRN \t $FUND \t $AMT" >> outfile
Very quickly - I created a data file called x which contains
FIELD1 123.45- 123
abdc 12.45 123
abcd 1.20- 123
I created the following file called x.sh
while read -r f1 f2 f3
do
echo $f1"\t\c"
LENGTH=`expr $f2 : ".*"`
LENGTH1=`expr $LENGTH - 1 `
SEP=`echo $f2 | cut -c$LENGTH`
if [ "$SEP" = "-" ]
then
REST=`echo $f2 | cut -c1-$LENGTH1`
echo $SEP$REST"\t\c"
else
echo $f2"\t\c"
fi
echo $f3 "\n"
done < x
execute x.sh , which reads a line at a time form the input file and checks the length of field 2 - see's if the last char is a -, if so print the - then the rest of the string - otherwise just print the string.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.