Find and print number after string in C


 
Thread Tools Search this Thread
Top Forums Programming Find and print number after string in C
# 1  
Old 05-26-2011
Find and print number after string in C

I'm trying find and print a number after a specific user passed string in each line of a text file using C (as requested by the powers that be). I've pieced together enough to read the file, find the string and print the line it was found on but I’m not sure where to even start in terms of finding the number after the string. Any tips would be much appreciated.

The below code outputs the line number where the string is found:
Code:
/* usage exename textfile searchstring */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[]) 
{ 
FILE *file; 
char line[BUFSIZ]; 
int linen = 1; 
file = fopen(argv[1], "r"); 
if(file != NULL) { 
  while(fgets(line, sizeof(line), file)){ 
     if(strstr(line, argv[2]) != NULL) { 
        printf("Found %s %s %i\n",argv[2],"@ line",linen); 
     }
 ++linen;     
 } 
} 
else { 
  perror(argv[1]); 
  perror(argv[2]); 
} 
fclose(file); 
return 0; 
}

My text file looks like below. I'm trying to print the number as is the file (2 decimals) after "val=".
Code:
123 test=123 val=233.33 x=111 y=222
123 test=123 val=.33 x=111 
123 xr=3 test=123 x=111 y=222
123 test=123 val=233. x=111 y=222
123 test=123 val=233.33 x=111 y=222
 
Output from current code:
Found val @ line 1
Found val @ line 2
Found val @ line 4
Found val @ line 5


Last edited by pludi; 05-27-2011 at 06:53 AM..
# 2  
Old 05-27-2011
This code has worked for one line string, you can use match() in your code

Code:
#include <stdio.h>
#include <string.h>

int match(char *ts,char *pattern);

int main()
{

        char *str="123 test=123 val=233.33 x=111 y=222 senth";
        char *ptt="senth";


        match(str,ptt);

}

int match(char *ts,char *pattern)
{

        char *pt=pattern;

        while(1)
        {
                printf ("comparing %c %c\n",*ts,*pt);
                while( *pt && *ts++ == *pt++)
                {
                        printf ("comparing %c %c\n",*ts,*pt);
                        ;
                }
                if(*pt == '\0')
                {
                        printf("match found %s\n",ts-strlen(pattern)-1);
                        break;
                }
                else if(*ts== '\0')
                {
                        printf("match not found \n");
                        // pattern not found
                        break;
                }
                        pt=(char *)pattern;
        }

}

# 3  
Old 05-27-2011
store the pointer returned by strstr(), look for the '=' sign, and after that location perform a sscanf() for instance.
# 4  
Old 06-06-2011
Thanks so much everyone!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to find number in a field then print the line and the number

Hi I want to use awk to match where field 3 contains a number within string - then print the line and just the number as a new field. The source file is pipe delimited and looks something like 1|net|ABC Letr1|1530||| 1|net|EXP_1040 ABC|1121||| 1|net|EXP_TG1224|1122||| 1|net|R_North|1123|||... (5 Replies)
Discussion started by: Mudshark
5 Replies

2. Shell Programming and Scripting

Find a string and print all lines upto another string

Ok I would like to do the following file test contains the following lines. between the lines ABC there may be any amount of lines up to the next ABC entry. I want to grep for the filename.txt entry and print the lines in between (and including that line) up to and including the last line... (3 Replies)
Discussion started by: revaroo
3 Replies

3. Shell Programming and Scripting

Print String N times the number before it

Hey All, I want want to print a string N times the number N before it. Like i have "20 hello". so i want to print hello hello hello . . . . . 20 times.. Please help me.. I am not able o figure out.. how to do the same? (8 Replies)
Discussion started by: jaituteja
8 Replies

4. Shell Programming and Scripting

awk find and print next string

Can I do this in one awk session. Solution I have is poor. I want to return the number after PID. echo "Start: 12345 is used by PID:11111 username" | awk -F: '{print $3}' | awk '{print $1}' (6 Replies)
Discussion started by: u20sr
6 Replies

5. Shell Programming and Scripting

awk - find number in string

I am trying to go through a file that has a few million lines. I want to only pull lines that contain a number anywhere in the ninth field, but it has to be after a "/" character. Here is my awk: awk -F\| '$9 ~ /\/*{1,}*/ {print $0}' file1 > file2 However, it is just printing out every... (3 Replies)
Discussion started by: dagamier
3 Replies

6. Shell Programming and Scripting

find string nth occurrence in file and print line number

Hi I have requirement to find nth occurrence in a file and capture data from with in lines (between lines) Data in File. <QUOTE> <SESSION> <ATTRIBUTE NAME='Parameter Filename' VALUE='file1.parm'/> <ATTRIBUTE NAME='Service Name' VALUE='None'/> </SESSION> <SESSION> <ATTRIBUTE... (6 Replies)
Discussion started by: tmalik79
6 Replies

7. UNIX for Dummies Questions & Answers

find string and and print another string

i have a file that looks like this ABC123 aaaaaaaaaaaaaaasssssssssssssssffhhh ABC234 EMPTY ABC652 jhfffffffffffffffffffffffffffffffffffkkkkkkkkkkkk i want to grep "EMPTY" and print ABC234 (3 Replies)
Discussion started by: engr.jay
3 Replies

8. Shell Programming and Scripting

Find longest string and print it

Hello all, I need to find the longest string in a select field and print that field. I have tried a few different methods and I always end up one step from where I need to be. Methods thus far: nawk '{if (length($1) > long) long=length($1); if(length($1)==long) print $1}' The above... (6 Replies)
Discussion started by: SEinT
6 Replies

9. Shell Programming and Scripting

How to print the number of lines from a file, the starting string should be passed`

Hi , I have file, which has the below content: line 100 a b c d line300 a s d f s line200 a s d a (3 Replies)
Discussion started by: little_wonder
3 Replies

10. Shell Programming and Scripting

way to print all the string till we get a space and a number

Is there any way to print all the string till we get a space and a number and store it a variable for eg we have string java.io.IOException: An existing connection was forcibly closed by the remote host 12 All I want is to store "java.io.IOException: An existing connection was forcibly closed... (13 Replies)
Discussion started by: villain41
13 Replies
Login or Register to Ask a Question