Removing last character of string

Hi ,
I have a file with the following contents in file test1.txt .

AMY_MTT_240Y001,N60_PG2_10G001,A2H_P3H_10G002,7C7_7D7_NP1,A2E_PV0_10G002,L78_PG1_64S001,A2H_P2M_NP2,LDN_YSN_64S001,WV6_WYV_64
S801,LY9_PV0_64S-R2052P,L78_PV0_64S-R2042,L78_PYE_64S-R2080,GVV_NW8_64S002,LY9_PV0_64S-UPNE1,DIS_MTP_DL72INTL,PVU_WV6_10G002,
DQE_NYY_NP6,2CS_LDN_NP1,C8L_LDN_NP1,2CS_LDN_NP2,2CS_LDN_NP3,D4O_LDN_NP1,AEQ_FR1_10G001,LDP_LE0_DL75INTL,A2H_P3H_NP2,A2H_P2M_N
P1,KS9_LDN_NP1,8BV_LDP_DL71INTL,AGA_FVP_DL71INTL,KSE_LDN_NP3,8MV_FVP_DL71INTL,8BV_FVP_DL71INTL,FV0_MTT_30N301

I want to display only those names that have "_DL" in them.

I came up with the following command:

 cat test1.txt | tr "," "\n" | grep -i _DL | tr '\n' "," 

Problem with this is there is a trailing comma. I am unable to replace the trailing comma with sed or cut or head .Can anyone suggest an alternative or how to replace the trailing comma. The following is the sed i used that failed in this case

sed 's/.$//' 

Thanks in advance folks

echo $string|sed 's/.\{n\}$//' 

Where n is the number of characters of the string that you want to remove from the end. In your case it is 1. So it should be,

sed 's/.\{1\}$//' 

---------- Post updated at 06:21 AM ---------- Previous update was at 06:20 AM ----------

[root@wiki ~]# string=qwerty
[root@wiki ~]# echo $string|sed 's/.\{1\}$//'
qwert
[root@wiki ~]# echo $string|sed 's/.\{2\}$//'
qwer

awk can solve your problem :wink:

awk -F, '{for(i=0;++i<=NF;)if($i~/_DL/){x=(x)?x FS $i:$i}}END{print x}' file

Something like this ?..

cat test1.txt | tr "," "\n" | grep -i _DL | tr '\n' ","  | awk '{ print substr($0,1,length($0)-1) }' 

Another way:

awk '/_DL/{s=s?s RS $0:$0}END{print s}' RS=","  file

Perfect Panyam. That worked like a charm.