How to find numbers in text file?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to find numbers in text file?
# 1  
Old 11-06-2012
How to find numbers in text file?

Hi

I have a text file with rows like this:
Code:
7 Herman ASI-40 Jungle (L) Blueprint (L) Weapon Herman ASI-40 Jungle (L) 215.00 57 65.21 114.41

and
Code:
9 Herman CAP-505 (L) Blueprint (L) Weapon Herman CAP-505 (L) 220.00 46.84 49.1 104.82

and
Code:
2 ClericDagger 1C blueprint Melee - Shortblade ClericDagger 1C 24.00 5.06 5.61 110.88

you note format is not fixed, numbers comes at different positions in row.
How could I make a script that extract numbers at different "positions"
the rows above have numbers
Code:
215.00 57 65.21 114.41

and
Code:
220.00 46.84 49.1 104.82

and
Code:
24.00 5.06 5.61 110.88

Example:
myscript1 (first pos) would generate 3 lines
Code:
215.00
220.00
24.00

myscript2 (second pos) would generate this:
Code:
57
46.88
5.06

any help welcome!
Pelle


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

Last edited by zaxxon; 11-06-2012 at 10:45 AM.. Reason: code tags
# 2  
Old 11-06-2012
If I am not wrong, the numbers have fixed positions. In the 3 example lines, the values are always the 4th-last. The next are the 3rd-last.
How should the output look like? Want all numbers together, or just 1 position on all lines per file?

As a start:
Code:
$ awk '{print $(NF-3)}' infile
215.00
220.00
24.00
$ awk '{print $(NF-2)}' infile
57
46.84
5.06

Btw. is the dual wielding barbarian berserker already taken by a player? SmilieSmilie
# 3  
Old 11-07-2012
Thanks alot! works like a charm.
Say I want to sort rows according to value from awk '{print $(NF-3)}' infile
Then I have to build a new file where every line is same as original file except every row has value from awk '{print $(NF-3)}' infile inserted at the beginning.
Could I do that by first vreating the file awk '{print $(NF-3)}' infile > infile2 and then ssomehow merge these 2 files?
I cant find any easy way to do this in a script,is there one?

pelle
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Find duplicates in file with line numbers

Hello All, This is a noob question. I tried searching for the answer but the answer found did not help me . I have a file that can have duplicates. 100 200 300 400 100 150 the number 100 is duplicated twice. I want to find the duplicate along with the line number. expected... (4 Replies)
Discussion started by: vatigers
4 Replies

2. UNIX for Dummies Questions & Answers

Extracting lines from a text file based on another text file with line numbers

Hi, I am trying to extract lines from a text file given a text file containing line numbers to be extracted from the first file. How do I go about doing this? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

3. UNIX for Dummies Questions & Answers

Print numbers and associated text belonging to an interval of numbers

##### (0 Replies)
Discussion started by: lucasvs
0 Replies

4. Shell Programming and Scripting

How to find the matched numbers between 2 text file using perl program??

hi dudes, I nee you kind assistance, I have to find the matched numbers from 2 text files and output of matched numbers should be in another text file.. I do have text files like this , for example File 1 787 665*5-p 5454 545-p 445-p 5454*-p File 2 5455 787 445-p 4356 2445 144 ... (3 Replies)
Discussion started by: sureshraj
3 Replies

5. Shell Programming and Scripting

find all numbers > x and replace with y within a file

How would I do this? How could i use <> symbols for numbers in the find/replace code below? perl -pi -e 's/test/tst/' OR is there a better way? 100 5000 2 432 4 2 33 4 5 6 65 300 301 needs to be: 100 300 2 300 4 2 33 4 5 6 65 300 300 also it might not always need spaces... i... (12 Replies)
Discussion started by: herot
12 Replies

6. Shell Programming and Scripting

Even numbers from text file

Hello, i have started learning scripting with linux commands. So far I am having probelms with one task: To make a script with from file with the text, prints even numbers used in that text. maybe any could script that? Thank You. (23 Replies)
Discussion started by: Hexes
23 Replies

7. Shell Programming and Scripting

how to add line numbers in text file

Hi all How to add line numbers in text file.. ex abcd cdef result 1. abcd 2. cdef thx in advance (4 Replies)
Discussion started by: suryanarayana
4 Replies

8. UNIX for Dummies Questions & Answers

Retrieving random numbers out of a text file

Hi one and all, I'm working on a Bash script that is designed to calculate how much IP traffic has passed through a port to determine traffic volume over a given amount of time. I've currently been able to use the netstat -s command coupled with grep to write to a file the total packets... (13 Replies)
Discussion started by: nistleloy
13 Replies

9. Shell Programming and Scripting

find 2 line numbers, grab text in between

hi, i have a large text file that I just want to extract the important information from. It will be a random number of lines but between two specific line numbers/markers. I was thinking I could get the line number for the first marker: Tablespace Percent Total Free Then get the line... (11 Replies)
Discussion started by: Da_Duck
11 Replies

10. Shell Programming and Scripting

how to print out line numbers of a text file?

i have this text file name test.txt which contain : aaaaa bbb iiiiiiiiiiiii ccf ddaaa ddd and i need a script that can print out the line numbers using a while loop.. so when the script is run..it will have this: 1 2 3 any ideas? :) thanks guys (4 Replies)
Discussion started by: forevercalz
4 Replies
Login or Register to Ask a Question