Getting values from a string


 
Thread Tools Search this Thread
Top Forums Programming Getting values from a string
# 1  
Old 11-01-2012
Getting values from a string

I have a string

Code:
s = " 1.0007126 6.8643880e+05 3.2924832e-09 9.4557639e+02  1.6407053 4.3481956e+03"

The numbers are separated by spaces and I want to get the first two numbers.

I am programming in Fortran, however a solution in c might be ok.

---------- Post updated 11-01-12 at 05:56 AM ---------- Previous update was 10-31-12 at 01:18 PM ----------

Done now
# 2  
Old 11-01-2012
Quote:
Originally Posted by kristinu
---------- Post updated 11-01-12 at 05:56 AM ---------- Previous update was 10-31-12 at 01:18 PM ----------

Done now
When you find solutions to your own problems, please post them (even if it's just a link to a web page that tells you what you want) so others googling in the future aren't left hanging.

I assume you discovered sscanf(), then?
# 3  
Old 11-01-2012
This is how I did it

Code:


open(unit=unit_bgnd, file=fin, form='formatted', status='old')
! Compute the number of lines in the file
nfl = 0
nhdr = 0
do
  read(unit_bgnd, '(A)', iostat=ios) s
  if (ios > 0) then
    write(*,*) ' Error reading file: ', fin
    exit
  else if (ios < 0) then
    write(*,'(A,I4)') '  No. of lines read from file: ', nfl
    exit
  else
    if (s(:1) == '#') nhdr = nhdr + 1
    nfl = nfl + 1
  end if
end do
! Move read position to beginning of file
rewind(unit_bgnd, iostat = ios)
nfv = nfl - nhdr
print *, 'nhdr, nfl, nv = ', nhdr, nfl, nfv
allocate(zfv(nfv))
allocate(cfv(nfv))
! Read values from the file containing Model S
ifv = 0
allocate(val(6))
do ifl = 1, nfl
  read(unit_bgnd, '(A)', iostat=ios) s
  print *, 's = ', s, ', s(1:1) = ', s(1:1)
  if (s(1:1) /= '#') then
      ifv = ifv + 1
      read(s, *) (val(i), i = 1, 6)
      print *, val(1), val(2)
      zfv(ifv) = val(1)  ! Gets the ratio radius
      cfv(ifv) = val(2)  ! Gets the sound speed in cm/sec
  endif
end do
deallocate(val)

# 4  
Old 11-01-2012
For what it's worth, you can do it in C with:

Code:
float a, b;
if (sscanf(s, " %f %f", &a, &b) != 2)
    printf("Error\n");
else
    printf("Values are %f and %f\n", a, b);

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

To Fetch values from String

Dears, I have string like below in my csv file <someTechnicalDetails><someTechnicalDetails>HSI</someTechnicalDetails,1234564 <someTechnicalDetails><someTechnicalDetails>Voice</someTechnicalDeta,12345673 <someTechnicalDetails><someTechnicalDetails>IPTV</someTechnicalDetai,12345673 and I... (2 Replies)
Discussion started by: mirwasim
2 Replies

2. Shell Programming and Scripting

Cutting string values from a file

My file looks like this.... User:SYSTEM,O/S User:oracle,Process:3408086,Machine:hostname ,Program:sqlplus@hostname (TNS V1-V,Logon Time:25-JUL-20 14 13:36 I want to get the date and time which is displayed after the 'Logon time'. (5 Replies)
Discussion started by: Nagesh_1985
5 Replies

3. UNIX for Dummies Questions & Answers

awk - How to join the string with values

Hi, I'm a beginner in awk script. I've been trying to figure how to concatenate two string in input file using awk after the value is calculated. I'm trying to get this format Apple 5.2(10) Orange 4.4(8) Watermelon 3.10(30) Berries 10.2(20) The input file with the format fruit... (2 Replies)
Discussion started by: KCApple
2 Replies

4. UNIX for Dummies Questions & Answers

How to append values to a string?

Hi, Requesting some help with a problem I am facing with string function in UNIX. I wish to create 2 string variables: 1st header string containing output_1, output_2, .. , output_<n> and 2nd data string containing the filename separated by colon (":") and corresponding filesize separated by... (6 Replies)
Discussion started by: vkumbhakarna
6 Replies

5. Shell Programming and Scripting

Replacing string values from a File

I want to replace string values from a file to a file For eg : File1 has 30 lines of string with values, those specific values needs to be changed in file2 and remaining values in file2 should be as it is. For example: From file (File1) cluster.name=secondaryCluster To replace File... (9 Replies)
Discussion started by: sriram003
9 Replies

6. AIX

How to append spaces to string values?

i/o file: abc,efg,xyz Required o/p file: "abc (Value + blank spaces=16) " ,"efg (Value +blank spaces=15) " ,"xyz (Value+ blank spaces =20) " In short input file value stores in result file with " i/p Value " added with spaces and are of fixed size like 16,15,20 How to do using... (2 Replies)
Discussion started by: AhmedLakadkutta
2 Replies

7. Shell Programming and Scripting

Matching multiple values in a string

I've been battling with parsing a comma-delimited string, and have had what I would call B- success. I'm using perl and trying to parse out specific identifiers from a string, into a new string. When things are "normal," my regex works fine. When things get complicated, my script fails... (1 Reply)
Discussion started by: linber2880
1 Replies

8. Shell Programming and Scripting

Replace blank values for string using sed

Hi everyone, I was wondering how could from a file where each row is separated by tabulations, the row values where are in blank replace them by a string or value. My file has this form: 26/01/09 13:45:00 0 0 26/01/09 14:00:00 1495.601318 0 26/01/09 14:15:00 1495.601318 0 ... (4 Replies)
Discussion started by: tonet
4 Replies

9. Shell Programming and Scripting

Append values before a string

hi all, i have variables a and b with values, like a="/var/tmp/new.sh /var/tmp/new2.sh" b="/TEST" how i need to append the value "/TEST" before the values for the variable "a" so that i get the output as /TEST/var/tmp/new.sh /TEST/var/tmp/new2.sh plz help me Regards, NG (2 Replies)
Discussion started by: Nandagopal
2 Replies

10. UNIX for Dummies Questions & Answers

to separate values from a string

Hi I would like to take input from user like username/password@connectstring I should be able to cut the username and password and connect string for example if someone enters like sam/sammy@ora1 my program should take sam as username sammy as password and ora1 as connectstring and... (3 Replies)
Discussion started by: ssuresh1999
3 Replies
Login or Register to Ask a Question