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?
# 8  
Old 07-19-2010
Just joking Smilie

Code:
awk -F'[^0-9]*' '$0=$NF _?$NF _:$(NF-1) _' infile

# 9  
Old 07-19-2010
radoulov, crazy Smilie, this works too:
Code:
awk -F'[^0-9]*' '$0=$NF?$NF:$(NF-1)' file

# 10  
Old 07-19-2010
Quote:
Originally Posted by Franklin52
radoulov, crazy Smilie, this works too:
Code:
awk -F'[^0-9]*' '$0=$NF?$NF:$(NF-1)' file

Smilie

It won't work with an input like this:

Code:
a 0
0 c

Actually the last space is unnecessary:

Code:
awk -F'[^0-9]*' '$0=$NF _?$NF _:$(NF-1)_' infile


Last edited by radoulov; 07-19-2010 at 07:01 PM..
# 11  
Old 07-19-2010
Quote:
Originally Posted by radoulov
Smilie

It won't work with an input like this:

Code:
a 0
0 c

You're right, it only works with numbers != 0 and hopefully he doesn't have negative numbers.
# 12  
Old 07-20-2010
Thank you all for all the responses. I wasn't able to think one and you have so many back of your mind(reminds me how much more I need to learn).

well, is there a way I can parse the fields for the records stored in arrays???
# 13  
Old 07-20-2010
Yes. awk, for instance, does that automatically for you.
If you need more help, post an example of the desired output (or explain what exactly you're trying to achieve).
# 14  
Old 07-20-2010
Quote:
Originally Posted by anbu23
Code:
sed 's/.* \([0-9]\{1,\}\)[^0-9]*/\1/' file

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

Quote:
Originally Posted by radoulov
Just joking

Code:
awk -F'[^0-9]*' '$0=$NF _?$NF _:$(NF-1) _' infile


These all fail to properly handle alphanumeric fields. The correct output (as I understood the problem) for the following input is '123', but these solutions return '456':
Code:
$ echo abc 123 def 456ghi | sed 's/.* \([0-9]\{1,\}\)[^0-9]*/\1/'
456
$ echo abc 123 def 456ghi | tr -d [:alpha:] | awk '{print $NF}'
456
$ echo abc 123 def 456ghi | awk -F'[^0-9]*' '$0=$NF _?$NF _:$(NF-1)_'
456

Regards,
Alister

---------- Post updated at 12:47 PM ---------- Previous update was at 12:19 PM ----------

A sh solution which reads from standard input:
Code:
#!/bin/sh

while IFS= read -r line; do
    set $line
    number=
    for word; do
        case $word in
            *[!0-9]*) continue;;
                   *) number=$word;;
        esac
    done
    echo $number
done

Regards,
Alister
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