find the last word with all numbers?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting find the last word with all numbers?
# 1  
Old 07-19-2010
find the last word with all numbers?

Hi,

I was trying to extract the last word with all numbers using awk. like the below example. I am looking for a pattern using awk.

Quote:
lasjdfjsafd asdf 1232 asdf 324 safdsf 34334 afsasfd
la;kjsfd 234 asdf 23432 adsfasdf 32433 asfda afaf afaf
alsdkjf; 23432 asfdadsf 32432 afdsasdf 24234 afdsasf
ka;ljsfd alksfdj;ajf ;lasjfd;saf 09897
desired result:

Quote:
34334
32433
24234
09897
# 2  
Old 07-19-2010
Hi,

try:

Code:
awk '{for (i=NF;i>0;i--){if ($i ~ /[0-9]+/){print $i;next}}}' file

gives me:
Code:
34334
32433
24234
09897

HTH Chris
# 3  
Old 07-19-2010
thanks for the reply.

It is working perfectly fine. However, it is failing when there are alphanumeric fields
# 4  
Old 07-19-2010
Code:
sed 's/[a-zA-Z|;]//g' urfile |awk '{print $NF}'

awk '{gsub(/[a-zA-Z]|;/,"");print $NF}' urfile


Last edited by rdcwayx; 07-19-2010 at 11:23 AM..
# 5  
Old 07-19-2010
Quote:
Originally Posted by hitmansilentass
it is failing when there are alphanumeric fields
Code:
awk '{for (i=NF;i>0;i--){if ($i ~ /^[0-9]+$/){print $i;next}}}' file

# 6  
Old 07-19-2010
Code:
sed 's/.* \([0-9]\{1,\}\)[^0-9]*/\1/' file

# 7  
Old 07-19-2010
Question

what about
Code:
echo abc 123 def 456 ghi | tr -d [:alpha:] | awk 'print $NF}'
456

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find a word and increment the number in the word & save into new files

Hi All, I am looking for a perl/awk/sed command to auto-increment the numbers line in file, P1.tcl: run_build_model sparc_ifu_dec run_drc set_faults -model path_delay -atpg_effectiveness -fault_coverage add_delay_paths P1 set_atpg -abort_limit 1000 run_atpg -ndetects 1000 I would like... (6 Replies)
Discussion started by: jypark22
6 Replies

2. Shell Programming and Scripting

Put numbers after word

Hello I have an file with this content -------------------------------------------- timer one timer two timer three timer four timer five timer six timer seven ------------------------------------------- And I want the following output.... (4 Replies)
Discussion started by: thailand
4 Replies

3. Shell Programming and Scripting

Shell Script @ Find a key word and If the key word matches then replace next 7 lines only

Hi All, I have a XML file which is looks like as below. <<please see the attachment >> <?xml version="1.0" encoding="UTF-8"?> <esites> <esite> <name>XXX.com</name> <storeId>10001</storeId> <module> ... (4 Replies)
Discussion started by: Rajeev_hbk
4 Replies

4. Shell Programming and Scripting

perl lwp find word and print next word :)

hi all, I'm new there, I'm just playing with perl and lwp and I just successfully created a script for log in to a web site with post. I have a response but I would like to have something like this: I have in my response lines like: <div class="sender">mimi020</div> <some html code.....>... (3 Replies)
Discussion started by: vogueestylee
3 Replies

5. UNIX for Dummies Questions & Answers

Find EXACT word in files, just the word: no prefix, no suffix, no 'similar', just the word

I have a file that has the words I want to find in other files (but lets say I just want to find my words in a single file). Those words are IDs, so if my word is ZZZ4, outputs like aaZZZ4, ZZZ4bb, aaZZZ4bb, ZZ4, ZZZ, ZyZ4, ZZZ4.8 (or anything like that) WON'T BE USEFUL. I need the whole word... (6 Replies)
Discussion started by: chicchan
6 Replies

6. Shell Programming and Scripting

Find and replace a word in all the files (that contain the word) under a directory

Hi Everyone, I am looking for a simple way for replacing all the files under a directory that use the server "xsgd1234dap" with "xsdr3423pap". For Example: In the Directory, $pwd /home/nick $ grep -l "xsgd1234dap" *.sh | wc -l 119 I have "119" files that are still using... (5 Replies)
Discussion started by: filter
5 Replies

7. Shell Programming and Scripting

Summing numbers after specific word

Hi all, Looking for suggestions on a better way to sum numbers in a key value pair formated file. What I have works but seems really clunky to me. Any suggestions would be greatly appreciated. cat test.txt | perl -ne 'm/(M=)(\d+\.?\d?\d?)/ && print "$2\n"' | awk '{ sum+=$1} END {printf... (7 Replies)
Discussion started by: cgol
7 Replies

8. Shell Programming and Scripting

awk fetch numbers after the word

Hi, I would want to fetch all the numbers after a word the number of characters could very. how can I do that? below is the example of the data and the expected output sample data 03 xxxx occurs 1090 times. 04 aslkja occurs 10 times. I would want to fetch 10 & 1090 separately. (13 Replies)
Discussion started by: ahmedwaseem2000
13 Replies

9. Shell Programming and Scripting

extract numbers from a word

Hi ppl, I am a bit lost on this...can some one assist. I know this can be down with awk or sed, but i cant get the exact syntax right. I need to only extract the numbers from a signle word ( eg abcd.123.xyz ) How can i extract 123 only ? Thanks (14 Replies)
Discussion started by: systemali
14 Replies

10. Shell Programming and Scripting

find a word in a file, and change a word beneath it ??

Hi all, I have a file with lines written somewhat like this. aaaa ccc aa linux browse = no xssxw cdcedc dcsdcd csdw police dwed dwd browse = no cdecec (2 Replies)
Discussion started by: vikas027
2 Replies
Login or Register to Ask a Question