Removing numbering from last character


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Removing numbering from last character
# 1  
Old 12-04-2018
Removing numbering from last character

Hi,
I have a file test.txt, below the contents:

Code:
SCDE1
SF9
STR1D2
SREDF21
FRED
STER2R4


I want to remove only the last character if it is the number, so the output should as below:

Code:
SCDE
SF
STR1D
SREDF2
FRED
STER2R


Any help or assistant will greatly appreciated.

Thanks

Last edited by khchong; 12-04-2018 at 10:52 PM..
# 2  
Old 12-04-2018
When starting a new thread in the Shell Programming and Scripting forum, please always tell us what operating system and shell you're using.

Also tell us what you have tried to solve this on your own.

Did you search for similar threads that may have already explained how to do what you're trying to do? There seem to be several threads in the list of topics you might find helpful at the end of output for this thread that exactly address what you want to do.
# 3  
Old 12-05-2018
Hi,

This is for bash shell.
Yes, I tried below:
cat test.txt |awk '{print substr($1, 1, length-1)}'

test.txt:
Code:
SCDE1
SF9
STR1D2
SREDF21
FRED
STER2R4

Output:
Code:
SCDE
SF
STR1D
SREDF2
FRE
STER2R


This will allow me to remove whatever last character in the line, but the requirement is only remove if numeric (1-9).
Appreciate if any suggestion given.

Thanks

Last edited by Scrutinizer; 12-05-2018 at 01:37 AM..
# 4  
Old 12-05-2018
Quote:
Originally Posted by khchong
Hi,

This is for bash shell.
Yes, I tried below:
cat test.txt |awk '{print substr($1, 1, length-1)}'

Code:
test.txt:
SCDE1
SF9
STR1D2
SREDF21
FRED
STER2R4

Output:
SCDE
SF
STR1D
SREDF2
FRE
STER2R


This will allow me to remove whatever last character in the line, but the requirement is only remove if numeric (1-9).
Appreciate if any suggestion given.

Thanks
You still haven't told us what operating system you're using.

Note that numeric is 0 through 9; not 1 through 9.

Note that awk and sed are perfectly capable of reading a file by itself without creating a pipe and an unneeded process (cat) to slow down processing and waste system resources.

One might try any of the following (all of which are very similar to suggestions in the threads listed at the bottom of this page) each of which should do what you requested:
Code:
sed 's/[0-9]$//' test.txt

Code:
awk '{sub(/[0-9]$/, ""}1' test.txt

Code:
awk '/[0-9]$/{$0=substr($0, 1, length-1)}1' test.txt

If you're trying this on a Solaris/SunOS system, change awk in either of the above to /usr/xpg4/bin/awk or nawk.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 12-05-2018
It worked for me, thank you so much! Will perform more searching in the forum moving forward.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

removing of junk character

Dear ALL, How to remove junk charecter ^M from unix file i am using sun solaris unix. I already tried few commands :%s/^M//g :%s/r//g but it didnt helped me. Any help appriciated. Thanks Ripudaman Please view this code tag video for how to use code tags when posting code... (5 Replies)
Discussion started by: ripudaman.singh
5 Replies

2. 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

3. 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

4. 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

5. 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

6. Shell Programming and Scripting

Removing ^Z Character in a File

Hi, I am having a pipe (|) delimited file which is having ^Z character in the middle of the text.Could anyone please suggest me how to remove this ^Z Character from the file.I almost used all the ideas posted in this site but none of them worked in my case since tis ^Z character is not coming at... (4 Replies)
Discussion started by: phoenix86
4 Replies

7. Shell Programming and Scripting

Removing Control M character

Hi, I'm using MKS tool kit to execute KSH on windows and samba to move files to unix from windows. My script is appending header record for the data file. I'm using echo "$header" > $SambaFilename cat $windowsfile >> $SambaFilename But after execution of script ,The file in Unix... (2 Replies)
Discussion started by: ammu
2 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