Removing last character of string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Removing last character of string
# 1  
Old 10-27-2009
Removing last character of string

Hi ,
I have a file with the following contents in file test1.txt .
Code:
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:

Code:
 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
Code:
sed 's/.$//'

Thanks in advance folks

Last edited by Franklin52; 10-27-2009 at 07:55 AM.. Reason: Please use code tags!
# 2  
Old 10-27-2009
Code:
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,
Code:
sed 's/.\{1\}$//'



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

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

# 3  
Old 10-27-2009
awk can solve your problem Smilie
Code:
awk -F, '{for(i=0;++i<=NF;)if($i~/_DL/){x=(x)?x FS $i:$i}}END{print x}' file

# 4  
Old 10-27-2009
Quote:
Originally Posted by kinny
Hi ,
I have a file with the following contents in file test1.txt .
Code:
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:

Code:
 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
Code:
sed 's/.$//'

Thanks in advance folks

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

# 5  
Old 10-27-2009
Another way:

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

# 6  
Old 10-27-2009
Perfect Panyam. That worked like a charm.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed searches a character string for a specified delimiter character, and returns a leading or traili

Hi, Anyone can help using SED searches a character string for a specified delimiter character, and returns a leading or trailing space/blank. Text file : "1"|"ExternalClassDEA519CF5"|"Art1" "2"|"ExternalClass563EA516C"|"Art3" "3"|"ExternalClass305ED16B8"|"Art9" ... ... ... (2 Replies)
Discussion started by: fspalero
2 Replies

2. UNIX for Dummies Questions & Answers

[Solved] Removing an 8 character string

Edit: Figured it out. Close the thread please. Solution: \{8}\] edit by bakunin: no need to close the thread, but i changed the title to SOLVED. Thanks for writing a follow-up. (0 Replies)
Discussion started by: unknownn
0 Replies

3. Shell Programming and Scripting

removing last character

Hi, I have a file that has data something like below: A B C D ..... ...... .....and so on I am trying to bring it in one line with comma delimited something like below : A,B,C,D I tried the something below in the code section: cat File.txt | tr '\n' ',' (1 Reply)
Discussion started by: rkumar28
1 Replies

4. UNIX for Dummies Questions & Answers

Need help removing last character of every line if certain character

I need help removing the last character of every line if it is a certain character. For example I need to get rid of a % character if it is in the last position. Input: aaa% %bbb ccc d%dd% Output should be: aaa %bbb ccc d%dd I tried this but it gets rid of all of the % characters.... (5 Replies)
Discussion started by: raptor25
5 Replies

5. UNIX for Dummies Questions & Answers

Removing a character

I need to remove square brackets from output of script. Output is: and I need to remove the square brackets so I am lett with 121 Is sed the only means to do this and if so what are the options? ...ok so far I have managed to get rid of ] by using /usr/bin/sed 's/]//' but that... (5 Replies)
Discussion started by: rob171171
5 Replies

6. Shell Programming and Scripting

Need some help removing a character from name

I have a file like this: DDD_ABCDE2AB2_1104081408.104480 I need to remove the 1 after the . in the file name so that it reads: DDD_ABCDE2AB2_1104081408.04480 Having some difficulty getting the command to work. I tried using cut -d 26 but that just doesn't work. (3 Replies)
Discussion started by: bbbngowc
3 Replies

7. Shell Programming and Scripting

Korn: How to loop through a string character by character

If I have a string defined as: MyString=abcde echo $MyString How can I loop through it character by character? I haven't been able to find a way to index the string so that I loop through it. shew01 (10 Replies)
Discussion started by: shew01
10 Replies

8. Shell Programming and Scripting

Removing character ' from a variable

Hello there, I have a variable in the form of '/example/file.txt' . I want to remove the ' characters from the beginning and the end so that the my new variable becomes /example/file.txt . How can I do it in a script? I know this is a fairly easy question, but i wasn't able to implement it. (3 Replies)
Discussion started by: sertansenturk
3 Replies

9. UNIX for Advanced & Expert Users

Removing first character in a string

While writing a shell script i happen to store some value in a string. Lets say the value is 59788. Now in this script i want to get the value 9788 removing the first charater 5. The original string length usually remains constant. Is there a single line command to do this or any simple way to... (4 Replies)
Discussion started by: npn
4 Replies

10. UNIX for Dummies Questions & Answers

Removing the ^M character in VI

Hello, I am attempting to remove all the ^M characters in a file in VI. The command I am using is :1,$s/^V^M//g but it doesn't work, saying 'substitute pattern match failed'. Any ideas why? Jules (2 Replies)
Discussion started by: julesinbath
2 Replies
Login or Register to Ask a Question