Grep result loses formatting

I am searching for a string in a file and then redirecting the contents in another file... however the formatting is not preserved.. Can you please help me on this ...

Hi.

There is no reason the formatting should be changed (when using grep).

Can you please show the command you are using, and a sample of the data?

This is result of grep

PER QUEUED | NORMAL FINAL CREDIT CR/FIN INACTV LOCKED SKIP ERROR | MISSING
--- ------ | ------ ------ ------ ------ ------ ------ ------ ------ | -------
J28 2 | 2 0 0 0 0 0 0 0 | 0
T01 88 | 34 0 0 0 54 0 0 0 | 0
T10 4 | 1 0 0 0 3 0 0 0 | 0
T15 6 | 6 0 0 0 0 0 0 0 | 0
--- ------ | ------ ------ ------ ------ ------ ------ ------ ------ | -------
TOT 100 | 43 0 0 0 57 0 0 0 | 0
=== ====== | ====== ====== ====== ====== ====== ====== ====== ====== | =======

Original text

PER   QUEUED | NORMAL  FINAL CREDIT CR/FIN INACTV LOCKED   SKIP  ERROR | MISSING
---   ------ | ------ ------ ------ ------ ------ ------ ------ ------ | -------
J28        2 |      2      0      0      0      0      0      0      0 |      0
T01       88 |     34      0      0      0     54      0      0      0 |      0
T10        4 |      1      0      0      0      3      0      0      0 |      0
T15        6 |      6      0      0      0      0      0      0      0 |      0
---   ------ | ------ ------ ------ ------ ------ ------ ------ ------ | -------
TOT      100 |     43      0      0      0     57      0      0      0 |      0
===   ====== | ====== ====== ====== ====== ====== ====== ====== ====== | =======

This is command that i m using
grep -A 31 "PROCESS ID" $obj

---------- Post updated at 03:19 PM ---------- Previous update was at 03:03 PM ----------

I think i was doing it wrong its not grep its echo

grep -A 31 "PROCESS ID" $obj | while read obj2
do
echo $obj2 > filename
done

can you suggest how to work around this.

:slight_smile:

Try quoting the echo...

grep -A 31 "PROCESS ID" $obj | while read obj2
do
echo "$obj2" > filename
done

Thanks mate it worked...
However i am facing one more problem

touch -t `date +%m%d0000` /tmp/$$
queued_total=0
processed_total=0
locked_total=0
error_total=0

find /apps/path/reports/ctrl/ -type f -newer /tmp/$$ | grep BIP_ | while read obj
do
echo "$obj">> /apps/path/report_$log_date.log
echo "_______________________________________________________________________________________________________________">> /apps/home/testfit01/report_$log_date.log
grep -E -A 31 "PROCESS ID" "$obj"  | while read obj2
do
if echo $obj2| grep -q "TASK ID:"; then
echo "$obj2">> /apps/path/report_$log_date.log
fi
if echo $obj2| grep -q "TOTAL ACCOUNTS QUEUED"; then
queued=`echo "$obj2" | cut -f2 -d ':'`
queued_total=`expr $queued_total + $queued`
echo "$obj2">> /apps/path/report_$log_date.log
fi
if echo $obj2| grep -q "TOTAL SUCCESSFULLY PROCESSED"; then
echo "$obj2">> /apps/path/report_$log_date.log
processed=`echo "$obj2" | cut -f2 -d ':'`
processed_total=`expr $processed_total + $processed`
fi
if echo $obj2| grep -q "TOTAL LOCKED/SKIPPED"; then
echo "$obj2">> /apps/home/testfit01/report_$log_date.log
locked=`echo "$obj2" | cut -f2 -d ':'`
locked_total=`expr $locked_total + $locked`
fi
if echo $obj2| grep -q "TOTAL IN ERROR"; then
echo "$obj2">> /apps/path/report_$log_date.log
error=`echo "$obj2" | cut -f2 -d ':'`
error_total=`expr $error_total + $error`
echo "_______________________________________________________________________________________________________________">> /apps/home/testfit01/report_$log_date.log
fi
done
echo " ">> /apps/path/report_$log_date.log
echo " ">> /apps/path/report_$log_date.log
done

echo "Total Queued      : $queued_total"
echo "Total Processed   : $processed_total"
echo "Total Locked      : $locked_total"
echo "Total Error       : $error_total"

I am getting 0 for all totals. Can you help.

The data in post #3 does not appear to relate to the script.

Can we see the data file (or files??) containing:

"PROCESS ID"
"TASK ID:"
"TOTAL ACCOUNTS QUEUED"
"TOTAL SUCCESSFULLY PROCESSED"
"TOTAL LOCKED/SKIPPED"
"TOTAL IN ERROR"