Sponsored Content
Full Discussion: Remove char if not a number
Top Forums UNIX for Dummies Questions & Answers Remove char if not a number Post 302845239 by millsy5 on Wednesday 21st of August 2013 06:09:50 AM
Old 08-21-2013
Remove char if not a number

I need to write a BASH script that takes a 2 character string and removes the second character if it is not a digit e.g.

If the string is numberical value >9 e.g. string1 = '34' then leave string1 = '34'.

However if the string is <10 e.g. string1 = '3X' then remove the second char (which can be any character, it's whatever was in memory) so that '3X' will now be '3'.

I was thinking about using ASCII values to determine whether the second character is a number.

Here's my attempt (I'm a newbie):

Code:
#!/bin/bash
 
if [ [string1[1]<'48'] || [string1[1]>'57'] ] # if 2nd char <0 or >9
then
        string1[1]="NOT SURE WHAT TO PUT HERE!!"
fi

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Number of specific char in a string.

I wish to compute the number of dot chars in a string. Example: VAR="aaaa.bbbbb.cccc" I try the shortest command to solve this test. Thanks in advance for your help. Regards, Giovanni (7 Replies)
Discussion started by: gio123bg
7 Replies

2. Shell Programming and Scripting

how to get number char from a string

for example: i hav a string like : /rmsprd/arch01/rmsprd/rmsprdarch72736.log how I can extract my_num=72736? I know I can echo "/rmsprd/arch01/rmsprd/rmsprdarch72736.log" | tr "/" " " | awk '{ print $4 }' to get rmsprdarch72736.log (4 Replies)
Discussion started by: netbanker
4 Replies

3. Shell Programming and Scripting

To find number of char occur

To find out number of "|" symbol is available in file: Input: a|b|c|d|z Ouput: 4 I am using below set of commands,It is working... Anybody have anyother solution using sed / awk. cnt=`wc -c <1.txt` cnt1=`tr -d "|" <1.txt >c.dat` cnt2=`wc -c <c.dat` outp=`expr $cnt... (19 Replies)
Discussion started by: Jairaj
19 Replies

4. Shell Programming and Scripting

Help needed to remove a char from a specified sub-string

Hi! I'm having trouble usind sed to remove the char ' from within a database's varchar register's sql. For example, on the following sql: INSERT INTO patrimonio_municipal.patrimonio_municipal_airc_tmp Values('|Estação Elevatória|',|16723|,'|Grandes Reparações|', '|2010-03-26... (3 Replies)
Discussion started by: cremat0rio
3 Replies

5. Shell Programming and Scripting

can I remove the first char using AWK?

Hi everyone, suppose that I have the following line: #test your knowledge can I use AWK to print the word "test" only? without the #? what should I change to this: awk '{print $1}' thanks in advance guys (2 Replies)
Discussion started by: Abdulelah
2 Replies

6. Shell Programming and Scripting

Remove char after Colon

Hi guys, This is my input 2735:<7001> 34 789 701 2 2774:<7001> 34 789 701 2 How to delete characters after colon : Including colon : too ? My output should... (3 Replies)
Discussion started by: gowrishankar05
3 Replies

7. UNIX Desktop Questions & Answers

Help me to remove junk char

I wanted to remove junk char in my csv. :mad: Input file format: "17","9986782190","0","D","2" "17","9900918331","0","D","2" "13","9986782194","0","A","2" Output file format 9986782190 9900918331 9986782194 And one more thing all the time "13"," this will be different Ex: . (2 Replies)
Discussion started by: Siddartha
2 Replies

8. Shell Programming and Scripting

How count number of char?

hello how can i cont number of char with loop coomand? i dont want to use wc or other special command the script should check all word's char. one by one also a counter can handle the number As noted in other threads started today. This is not the correct forum for homework assignments. ... (2 Replies)
Discussion started by: nimafire
2 Replies

9. Shell Programming and Scripting

Remove double char occurences

I have a file with random characters where every time a char occurs twice, one occurrence must be removed. Eg. asjkdhaSSd Must become: asjkdhaSd Anybody has a SED script in mind to do it? (1 Reply)
Discussion started by: rlopes
1 Replies

10. UNIX for Advanced & Expert Users

How to remove a char before a pattern?

Hi I have a file where i want to remove a char before a specific pattern. exp: CREATE TABLE ( A, B, C, ----comma needs to be removed )AS SELECT A, B, C, ----comma needs to be removed FROM TABLE. So i want to delete the comma(,) after the C both ways.Pattern can be... (11 Replies)
Discussion started by: raju2016
11 Replies
tr(1B)						     SunOS/BSD Compatibility Package Commands						    tr(1B)

NAME
tr - translate characters SYNOPSIS
/usr/ucb/tr [-cds] [ string1 [string2]] DESCRIPTION
The tr utility copies the standard input to the standard output with substitution or deletion of selected characters. The arguments string1 and string2 are considered sets of characters. Any input character found in string1 is mapped into the character in the corresponding posi- tion within string2. When string2 is short, it is padded to the length of string1 by duplicating its last character. In either string the notation: a-b denotes a range of characters from a to b in increasing ASCII order. The character , followed by 1, 2 or 3 octal digits stands for the character whose ASCII code is given by those digits. As with the shell, the escape character , followed by any other character, escapes any special meaning for that character. OPTIONS
Any combination of the options -c, -d, or -s may be used: -c Complement the set of characters in string1 with respect to the universe of characters whose ASCII codes are 01 through 0377 octal. -d Delete all input characters in string1. -s Squeeze all strings of repeated output characters that are in string2 to single characters. EXAMPLES
Example 1: Creating a list of all the words in a filename The following example creates a list of all the words in filename1, one per line, in filename2, where a word is taken to be a maximal string of alphabetics. The second string is quoted to protect `' from the shell. 012 is the ASCII code for NEWLINE. example% tr -cs A-Za-z '12' <filename1>filename2 ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWscpu | +-----------------------------+-----------------------------+ SEE ALSO
ed(1), ascii(5), attributes(5) NOTES
Will not handle ASCII NUL in string1 or string2. tr always deletes NUL from input. SunOS 5.10 26 Sep 1992 tr(1B)
All times are GMT -4. The time now is 10:43 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy