home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / unix / question / 10178 < prev    next >
Encoding:
Internet Message Format  |  1992-08-19  |  2.6 KB

  1. Path: sparky!uunet!ferkel.ucsb.edu!taco!lll-winken!sun-barr!cs.utexas.edu!wupost!darwin.sura.net!convex!news.utdallas.edu!corpgate!bnrgate!bcars267!bmdhh243!agc
  2. From: agc@bmdhh298.bnr.ca (Alan Carter)
  3. Newsgroups: comp.unix.questions
  4. Subject: Re: grep for number of character string
  5. Message-ID: <1992Aug19.134310.19038@bnr.uk>
  6. Date: 19 Aug 92 13:43:10 GMT
  7. References: <1992Aug18.140346.3939@nlm.nih.gov>
  8. Sender: news@bnr.uk (News Administrator)
  9. Organization: BNR-Europe-Limited, Maidenhead, England
  10. Lines: 78
  11. Nntp-Posting-Host: bmdhh298
  12.  
  13. In article <1992Aug18.140346.3939@nlm.nih.gov>, berman@nlm.nih.gov (Lew Berman) writes:
  14. |> How can I determine if a value is a string or an integer within a 
  15. |> bourne shell script?  For example, if a file exists such that
  16. |> each line may contain a number (integer) or a string (which can
  17. |> be alpha-numeric) how do I determine which it is?  That is to say,
  18. |> if I cat the file into an argument and then run this data through a
  19. |> for loop how do I test each element?
  20. |> 
  21. |> Example data file:
  22. |> 
  23. |> 345
  24. |> 5467
  25. |> 234
  26. |> 2
  27. |> 3
  28. |> 456
  29. |> 730
  30. |> aabbccdd
  31. |> sffeetr$%^
  32. |> 4566KKLKLK        <- not a number
  33. |> lij56            <- not a number
  34.  
  35. Sorry, can't send e-mail. The bit you want to fill in your script is:
  36.  
  37.     #! /bin/sh
  38.     data=`cat $1`
  39.  
  40.     for i in $data
  41.     do
  42.         if echo $i | grep -v "[^0-9]" > /dev/null
  43.         then
  44.             echo "$i is a number"
  45.         fi
  46.     done
  47.  
  48. Slightly odd logic; if it does not contain any characters that are 
  49. not numeric, then it must be a number. Word of warning though: that 
  50. 'data=`cat $1`' trick could burst the shell variable buffer. 
  51. Better to say:
  52.  
  53.     #! /bin/sh
  54.  
  55.     cat $1 | while read i
  56.     do
  57.         if test ! -z "$i"
  58.         then
  59.             if echo $i | grep -v "[^0-9]" > /dev/null
  60.             then
  61.                 echo "$i is a number"
  62.             fi
  63.         fi
  64.     done
  65.  
  66. and use a pipe. Note the extra check  'if test... fi'. This is needed 
  67. to get the same behaviour, as your original script automagically 
  68. removed blank lines. The pipe flavour doesn't, and they don't contain 
  69. any characters that are not numeric :-)
  70.  
  71. Or best yet, fire up 1 awk instead of many greps and tests, let it chew 
  72. the file and give the output to you for further processing:
  73.  
  74.  
  75.     #! /bin/sh
  76.  
  77.     awk '/^[0-9]+$/' $1 | while read i
  78.     do
  79.                 echo "$i is a number"
  80.     done
  81.  
  82.    Regards, Alan
  83.  
  84. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  85.    Maidenhead itself is too snobby to be pleasant. It is the haunt of the
  86.    river swell and his overdressed female companion. It is the town of showy
  87.    hotels, patronized chiefly by dudes and ballet girls. 
  88.  
  89.    Three Men In A Boat, Jerome K. Jerome, 1889
  90. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  91.