Finding numbers in lines with strings and number and doing some manipulation


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Finding numbers in lines with strings and number and doing some manipulation
# 1  
Old 12-07-2011
Finding numbers in lines with strings and number and doing some manipulation

Hi,

I want to write a script that does something like this:

I have a file, in which in every line, there is a string of words, and followed by some space, a number.

Quote:
..
nFaces 212;
startFace 8118;
..
Now, I want to identify the line, which has the largest startFace number (say m=8118), take that number and add it to the number (say n=212) in the line which is just before the line with the largest number, and write the output in the same file, which has the following format

Quote:
..
nFaces 0;
startFace 8330 (m+n);
..
Can anyone help me with this? What is the best options to use here... -sed or AWK?
# 2  
Old 12-07-2011
Best is subjective Smilie, but I would use awk:
Code:
                  { $2 += 0; } # force numeric comparisions
$1 == "nFaces"    { n = $2; }
$1 == "startFace" { if (startFace < $2) { nFaces = n; startFace = $2; } }
END               { print "nFaces", 0; print "startFace", startFace + nFaces; }

# 3  
Old 12-07-2011
A wee Perl struct would be my choice, though I'm sure an AWK guru [strike]will be along to provide a more concise solution shortly...[/strike] has already got there

I generated sample data with
Code:
perl -e '
for (1..100){
>    if ( ($_ % 2) == 0 ){printf "startFace %d\n", (int(rand(100)));}
>    else{ printf "nFaces %d\n", (int(rand(100)));}}
> ' >file

and the following script seems to do the job
Code:
 perl -e 'while(<>){
   if (/startFace (\d+)/){
      if ($1 > $faces{max}){
         $faces{max} = $1;
         $faces{prev}=$prev;
      }
   }
   else{
      ($prev)=/\s+(\d+)/;
   }
}
print "nFaces 0;\nstartFace ",$faces{max}+$faces{prev}," (m+n)\n";' file

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sort strings containing numbers

How can I sort this, first by 2nd field then by 1st field. tried sort -b -k 2,2 Input: AS11 AB1 BD34 AB10 AF12 AC2 A345 AB10 R134 AB2 456 AC10 TTT2 BD12 desired output: AS11 AB1 R134 AB2 A345 AB10 BD34 AB10 AF12 AC2 456 AC10 TTT2 BD12 (2 Replies)
Discussion started by: aydj
2 Replies

2. Shell Programming and Scripting

Challenge in finding listing class method and its number of code lines

there are about 300 objectivec .m files and I need to print each file name and its method and number of lines inside the method there is a sample perl files that do perl brace matching... (0 Replies)
Discussion started by: steve32001
0 Replies

3. Shell Programming and Scripting

Trying to take file numbers from a file, pass them to sed to change strings in corresponding lines

I have a bunch of file numbers in the file 'test': I'm trying the above command to change all the instances of "H" to "Na+" in the file testsds.pdb at the line numbers indicated in the file 'test'. I've tried the following and various similar alternatives but nothing is working: cat test |... (3 Replies)
Discussion started by: crunchgargoyle
3 Replies

4. Shell Programming and Scripting

Finding contiguous numbers in a list but with a gap number tolerance

Dear all, I have a imput file like this imput scaffold_0 10558458 10558459 1.8 scaffold_0 10558464 10558465 1.75 scaffold_0 10558467 10558468 1.8 scaffold_0 10558468 10558469 1.71428571428571 scaffold_0 10558469... (5 Replies)
Discussion started by: valente
5 Replies

5. Shell Programming and Scripting

Finding strings through multiple lines

Hi, I need to search for a multiple line pattern and remove it the pattern is search for (ln number) <TABLE name=*> and if 3 lines below that the line is (ln number) </TABLE> Then remove those 4 lines. Thank you (14 Replies)
Discussion started by: legolad
14 Replies

6. Shell Programming and Scripting

Delete lines in file containing duplicate strings, keeping longer strings

The question is not as simple as the title... I have a file, it looks like this <string name="string1">RZ-LED</string> <string name="string2">2.0</string> <string name="string2">Version 2.0</string> <string name="string3">BP</string> I would like to check for duplicate entries of... (11 Replies)
Discussion started by: raidzero
11 Replies

7. Shell Programming and Scripting

the smallest number from 90% of highest numbers from all numbers in file

Hello All, I am having problem to find what is the smallest number from 90% of highest numbers from all numbers in file. I am having file with thousands of lines and hundreds of columns. I am familiar mainly with bash but I am open to whatever suggestion witch will lead to the solutions. If I... (11 Replies)
Discussion started by: Apfik
11 Replies

8. Shell Programming and Scripting

Finding number of strings in List

I have a list of strings stored in $Lst Example set Lst = "John Fred Kate Paul" I want to return 4 in this case. (1 Reply)
Discussion started by: kristinu
1 Replies

9. Shell Programming and Scripting

Finding strings

Hi I made a post earlier but now my problem has become a lot more complicated. So I have a file that looks like this: Name 1 13 94 1 AGGTT Name 1 31 44 1 TTCCG Name 1 13 94 2 AAAAATTTT Name 1 41 47 2 GGGGGGGGGGG So the file is tab delimited and what I want to do is find... (8 Replies)
Discussion started by: kylle345
8 Replies

10. Shell Programming and Scripting

Reading n number of lines after finding string

I am trying to search a file for a value: "Top 30 reject reasons" and want the next 30 lines after that and output in a text file. If I knew the line number, I can use a combination of head and tail commands to get my results, but this doesn't seem to work when I don't have a line number. I... (2 Replies)
Discussion started by: oriqin
2 Replies
Login or Register to Ask a Question