Visit Our UNIX and Linux User Community

Extract all first numeric character from a string

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract all first numeric character from a string
# 1  
Old 04-17-2015
Extract all first numeric character from a string


I have a file of strings a below:-

9834URD 98HJDU

I need to extract all the first numeric character of every sting as follows:-


thanks to suggest ASAP


Moderator's Comments:
Mod Comment Use code tags, thanks.

Last edited by zaxxon; 04-17-2015 at 08:50 AM..
# 2  
Old 04-17-2015
If your grep version supports -o

grep -oE '^[0-9]+' file

# 3  
Old 04-17-2015
Another one with sed:
$ sed 's/^\([0-9]\+\).*/\1/' infile

# 4  
Old 04-17-2015
I guess you meant group of numeric chars, not a single one. Try
sed 's/[^[:digit:]].*$//' file

# 5  
Old 04-17-2015
In buit-in bash:
while read ; do echo ${REPLY%%[^[:digit:]]*}; done <file

# 6  
Old 04-17-2015
Could try:
tr -d [:alpha:] < sample.txt


Misread, nevermind.
This removes all letters from the file.

Last edited by sea; 04-18-2015 at 09:03 AM..
# 7  
Old 04-17-2015
Standard sed:
sed 's/[^0-9].*//' file

Previous Thread | Next Thread
Test Your Knowledge in Computers #377
Difficulty: Medium
macOS is UNIX V7 compliant.
True or False?

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. Shell Programming and Scripting

How to extract the certain position's character in a string

Suppose there are two files: A, format is like: line1 12 line2 33 line3 6 ... B, format is like: >header taaccctaaccctaaccctaacccaaccccaccccaaccccaaccccaac ccaaccctaaccctaaccctaacccaaccctaaccctaaccctaacccaa ccctcaccctcaccctcaccctcaccctcaccctcaccctcaccctaacc... (1 Reply)
Discussion started by: bioinflix
1 Replies

3. Shell Programming and Scripting

Extract resultset numeric value from isql output ?

isql output comes as below, (0 rows affected) (1 row affected) (7 rows affected) How to extract the resultset number alone from the particular line ?? such as 0 1 7 (3 Replies)
Discussion started by: vikram3.r
3 Replies

4. Shell Programming and Scripting

Extract character from string

ps -eaf | grep “oracleTRLV (LOCAL=NO)” | while read ora_proc do echo $ora_proc done I would like to modify the above shell so that if character 13 and 14 equal "12" to do something. Sorry I'm new to shell:( (14 Replies)
Discussion started by: NicoMan
14 Replies

5. Shell Programming and Scripting

validating a input file for numeric and character

i have a input file like this 001|rahim|bajaj|20090102 while reading the file i need to check whether the first column is a number second column is a name is there any methodology to check for the same thanks in advance (2 Replies)
Discussion started by: trichyselva
2 Replies

6. Shell Programming and Scripting

check whether it is a non-numeric character

Below is the abstract of the script which is working fine. if ] then error_process "Invalid month format." return 1 fi I am doing validation for month and it errors if the value is > 12 or < 0. In addition, I want to add another condition to error if it... (2 Replies)
Discussion started by: sony_dada
2 Replies

7. Shell Programming and Scripting

:-) 1213: Character to numeric conversion error. Plz help

Dear friends, I am new to Unix/Linux. I am trying to run following query but getting an error msg... Please can u people help me in this? Query: echo "select status_ac from db_acct where acct_num=AAA000337" | dbaccess elstest Error: 217: Column (amd000337) not found in any table in the... (2 Replies)
Discussion started by: anushree.a
2 Replies

8. Shell Programming and Scripting

Extract the last character of a string

How can I extract the last character of a string (withou knowing how many characters are in that string ! ) (8 Replies)
Discussion started by: annelisa
8 Replies

9. Shell Programming and Scripting

Bourne: search for a non-numeric character in $VAR

if $1 = "123x456", how can I test for the non-numeric character 'x' in that string. I've tried expr with "" but it did not find the x. Any ideas? Can this perhaps be done with sed? Thanks. (2 Replies)
Discussion started by: lumix
2 Replies

10. Shell Programming and Scripting

Need help to extract a string delimited by any special character

I have a string as follows IS*blahblah TED~blahblah etc. I want to list down only IS and TED Can someone help me? (24 Replies)
Discussion started by: kumariak
24 Replies

Featured Tech Videos