tr command giving wrong output

Hi All,
i have a file which have many fields delimited by ,(comma)
now i have to show only few fields and not all.

the sample text file looks like this:

 
TYPE=SERVICEEVENT, TIMESTAMP=05/06/2009 11:01:40 PM, HOST=sppwa634, APPLICATION=ASComp, FUNCTION=LimitsService, SOU
RCE=com.example.as.limits.ejb.LimitsServiceBean, SOURCE_METHOD=getLimitsList, MESSAGE=Failed to execute the
 CAS Limits Transaction via tfwk2.0 - due to SORResponseException: com.example.util.transfwk.exception.SORR
esponseException : Timed out in receive for CorrelationID:ID:414d512053505057413633342020202049fb54cf2a1a5dd0 from:
CASA:JMSWrapper : receive() :Timed out waiting for response for a correlation id : [ID:414d512053505057413633342020
202049fb54cf2a1a5dd0]QCFPoolName : [CASQCF] and ResponseQueueName : [MYCA_CAS_REPLY_QUEUE] Timeout[10000]for Card N
umber : , SESSION_ID=b302cbd0-8d67a6e6-e3f371bd-d2561b0d, GUID=null, CARD_TYPE=null, ENTRY_URL=account, STATUS=fals
e, TIME_TO_EXECUTE=36079, MARKET_IDENTIFIER=null, FAILED_SYSTEM=null, FAILURE_CATEGORY=null, FAIL_REASON_TEXT=null

Form this file if i want to remove fields APPLICATION and SOURCE_METHOD both ending with a ,

i am using this line

cat myfile | tr -d 'APPLICATION' 'SOURCE_METHOD'

But this command is not working properly

It is giving output like this:

 
YE=SERVEEVE, MESM=05/06/2009 11:01:40 M, HS=sppwa634, =Somp, FU=imitsService, SURE=com.example.as.limits.ej
b.imitsServiceBean, SURE_MEHD=getimitsist, MESSGE=Failed to execute the S imits ransaction via tfwk2.0 - due to SRR
esponseException: com.example.util.transfwk.exception.SRResponseException : imed out in receive for orrelat
ionD:D:414d512053505057413633342020202049fb54cf2a1a5dd0 from:S:JMSWrapper : receive() :imed out waiting for respons
e for a correlation id : [D:414d512053505057413633342020202049fb54cf2a1a5dd0]QFoolame : [SQF] and ResponseQueueame
: [MY_S_REY_QUEUE] imeout[10000]for ard umber : , SESS_D=b302cbd0-8d67a6e6-e3f371bd-d2561b0d, GUD=null, RD_YE=null,
 ERY_UR=account, SUS=false, ME__EXEUE=36079, MRKE_DEFER=null, FED_SYSEM=null, FURE_EGRY=null, F_RES_EX=null

It is not removing the complete fields but removing some letters from every field.

Please advice.
Thanks

tr doesn't replace words or fields, but characters. Example:

$ echo "abcdefghijklmnopqrstuvwxyz" | tr 'afik' 'AFIK'
AbcdeFghIjKlmnopqrstuvwxyz

The first letter in the first parameter is replaced with the first letter in the second parameter, the second letter in the first parameter is replaced with the second letter in the second parameter, ...

Hi Pludi,Thanks for your quick reply.
I got it.
But i am not very comfortable with sed.

Is there any way so that from that complete line i can remove some words

for Eg:
APPLICATION=ASComp, this complete field i want to remove similarly
SOURCE_METHOD=getLimitsList, this field i want to remove??

Thanks

echo 'foo=bar, APPLICATION=ASComp, fred=baz' | sed 's/[ ]*APPLICATION=ASComp,//g'

try this:

sed -e 's/APPLICATION[^,]*,//g' -e 's/SOURCE_MEATHOD[^,]*,//g' filename

cheers,
Devaraj Takhellambam

if you have Python, here's an alternative

#!/usr/bin/env python
data=open("file").read().split(",")
for num,item in enumerate(data):
    if "APPLICATION=ASComp" in item or "SOURCE_METHOD" in item:
        data.pop(num)
print ','.join(data)

output:

 ./test.py
TYPE=SERVICEEVENT, TIMESTAMP=05/06/2009 11:01:40 PM, HOST=sppwa634, FUNCTION=LimitsService, SOU
RCE=com.example.as.limits.ejb.LimitsServiceBean, MESSAGE=Failed to execute the
 CAS Limits Transaction via tfwk2.0 - due to SORResponseException: com.example.util.transfwk.exception.SORR
esponseException : Timed out in receive for CorrelationID:ID:414d512053505057413633342020202049fb54cf2a1a5dd0 from:
CASA:JMSWrapper : receive() :Timed out waiting for response for a correlation id : [ID:414d512053505057413633342020
202049fb54cf2a1a5dd0]QCFPoolName : [CASQCF] and ResponseQueueName : [MYCA_CAS_REPLY_QUEUE] Timeout[10000]for Card N
umber : , SESSION_ID=b302cbd0-8d67a6e6-e3f371bd-d2561b0d, GUID=null, CARD_TYPE=null, ENTRY_URL=account, STATUS=fals
e, TIME_TO_EXECUTE=36079, MARKET_IDENTIFIER=null, FAILED_SYSTEM=null, FAILURE_CATEGORY=null, FAIL_REASON_TEXT=null

> tr "," "\n" <file19 | grep -v "APPLICATION" | tr "\n" ","

Take my input file called file19, translate "," to new-line characters to put each parameter on its own line, grep to exclude any line with the word "APPLICATION", then change the new-line characters back to "," characters.

Would this approach work for you?

Thanks a lot to all for your valuable replies.
Hi Ghostdog74,
I dont have python installed in my system so couldnt get this option worked.Anyways thanks a lot for your suggestion and i would keep it so that i will be useful in future if i have python installed.

Hi devtakh,
your code works but only for first option i.e the fields with APPLICATION are getting deleted but SOURCE_METHOD are still remaining.
Is it like that we can remove only one field at a time using sed??

Hi joeyg,
Thanks for showing another approach but it is not working in my system
It is showing like this.
$ > tr "," "\n" <file19 | grep -v "APPLICATION" | tr "\n" ","
ksh: ,: not found

Could you please help me find out why is it so?

Thanks in advance.
Usha

That is because METHOD was incorrectly spelt as MEATHOD :slight_smile:

sed -e 's/APPLICATION[^,]*,//g' -e 's/SOURCE_METHOD[^,]*,//g' filename

cheers,
Devaraj Takhellambam