awk fetch numbers after the word


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk fetch numbers after the word
# 1  
Old 07-14-2010
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.
# 2  
Old 07-14-2010
Quote:
Originally Posted by ahmedwaseem2000
...
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.
If "occurs" is the word in your sample data, and you want to fetch the longest string of digits after that, then -

Code:
$
$
$ cat f2
03 xxxx occurs 1090 times.
04 aslkja occurs 10 times.
$
$ awk '{x=gensub(/.*occurs ([0-9]+) .*/,"\\1",$0); print x}' f2
1090
10
$
$

tyler_durden
# 3  
Old 07-14-2010
Or is this sufficient for your purpose?
Code:
awk '{print $(NF-1)}' file

# 4  
Old 07-14-2010
Quote:
Originally Posted by durden_tyler
If "occurs" is the word in your sample data, and you want to fetch the longest string of digits after that, then -

Code:
$
$
$ cat f2
03 xxxx occurs 1090 times.
04 aslkja occurs 10 times.
$
$ awk '{x=gensub(/.*occurs ([0-9]+) .*/,"\\1",$0); print x}' f2
1090
10
$
$

tyler_durden
what is gensub function?
# 5  
Old 07-14-2010
Quote:
Originally Posted by anbu23
what is gensub function?
It's a gawk specific function:

gensub - The GNU Awk User's Guide
This User Gave Thanks to Franklin52 For This Post:
# 6  
Old 07-15-2010
Thanks guys!! However, gensub doesnt work so, I replaced it with gsub still doesnt work. and its not always the second last field so cant use the field -1 option either. what could be other ways to this?
# 7  
Old 07-15-2010
Quote:
Originally Posted by ahmedwaseem2000
Thanks guys!! However, gensub doesnt work so, I replaced it with gsub still doesnt work.
gensub is gawk specific, did you use gawk?
Quote:
Originally Posted by ahmedwaseem2000
and its not always the second last field so cant use the field -1 option either. what could be other ways to this?
Post a better example of your input file.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. Shell Programming and Scripting

Cut & Fetch word from string

I have a file with some SQL query, I want to fetch only Table Name from that file line by line. INPUT FILE SELECT * FROM $SCHM.TABLENAME1; ALTER TABLE $SCHM.TABLENAME1 ADD DateOfBirth date; INSERT INTO $SCHM.TABLENAME1 (CustomerName, Country) SELECT SupplierName, Country FROM $SCHM.TABLENAME2... (2 Replies)
Discussion started by: Pratik Majithia
2 Replies

3. UNIX for Dummies Questions & Answers

[Solved] awk solution to add sequential numbers based on a word

Hi experts, I've been struggling to format a large genetic dataset. It's complicated to explain so I'll simply post example input/output $cat input.txt ID GENE pos start end blah1 coolgene 1 3 5 blah2 coolgene 1 4 6 blah3 coolgene 1 4 ... (4 Replies)
Discussion started by: torchij
4 Replies

4. Shell Programming and Scripting

Fetch entries in front of specific word till next word

Hi all I have following file which I have to edit for research purpose file:///tmp/moz-screenshot.png body, div, table, thead, tbody, tfoot, tr, th, td, p { font-family: "Liberation Sans"; font-size: x-small; } Drug: KRP-104 QD Drug: Placebo Drug: Metformin|Drug:... (15 Replies)
Discussion started by: Priyanka Chopra
15 Replies

5. Shell Programming and Scripting

Match the word or words and fetch the entries

Hi all, I have 7 words Now I have 1 file which contain data in large number of rows and columns and 6th column contain any of these words or may be more than one words among above 7 words: I want script should search for the above mentioned 7 words in the 6th column ... (9 Replies)
Discussion started by: manigrover
9 Replies

6. Shell Programming and Scripting

match sentence and word adn fetch similar words in alist

Hi all, I have ot match sentence list and word list anf fetch similar words in a separate file second file with 2 columns So I want the output shuld be 2 columns like this (3 Replies)
Discussion started by: manigrover
3 Replies

7. Shell Programming and Scripting

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. desired result: (13 Replies)
Discussion started by: hitmansilentass
13 Replies

8. Shell Programming and Scripting

Help to fetch first two characters from a word in perl

Hi All, I have a word "DE_PR_Package__Basic" , i need to check if the first two characters of this words is DE or something else using perl script. Can anyone pls let me know how to proceed? Thanks in advance. Giri! (3 Replies)
Discussion started by: girish.raos
3 Replies

9. Shell Programming and Scripting

How to fetch rows based on line numbers or based on the beginning of a word?

I have a file which will have rows like shown below, ST*820*316054716 RMR*IV*11333331009*PO*40.31 REF*IV*22234441009*xsss471-2762 DTM*003*091016 ENT*000006 RMR*IV*2222234444*PO*239.91 REF*IV*1234445451009*LJhjlkhkj471-2762 </SPAN> DTM*003* 091016 RMR*IV*2223344441009*PO*40.31... (18 Replies)
Discussion started by: Muthuraj K
18 Replies

10. 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
Login or Register to Ask a Question