awk -- telling the difference between strings and integers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk -- telling the difference between strings and integers
# 1  
Old 07-01-2013
awk -- telling the difference between strings and integers

This should be a really easy question.

My input file will have a few fields that are strings in the first line, which I will extract and save as variables. The rest of the fields on every line will be integers and floating point numbers. Can awk tell the difference somehow? That is, is there some way I can use if ( $i is not a string ) then {do stuff}? Or is that impossible? Thanks a lot!
# 2  
Old 07-01-2013
This might help you understand ...
https://www.unix.com/shell-programmin...anumerics.html

Shows how to determine what kind of characters or numbers are present in a field.
# 3  
Old 07-01-2013
Ahhhhh. Regular expressions are still kinda tricky for me, but such a powerful tool! I shoulda thought to turn there. Thanks for your quick help.
# 4  
Old 07-01-2013
Be careful as -1.E2 is a number, too, but will not be matched by the regexes [0-9] nor [[:digit:]].
Adding +0 to force a field to become a number also can be misleading, as awk then will use any leading digits and ignore the residual non-digit characters. Still it may be promising to use sth like if ($1 == $1+0) do_stuff.
# 5  
Old 07-01-2013
Fortunately, the project I'm working on will be simple enough that just checking for numbers should do the trick. However, * will also be used as a wildcard, and some numbers will be in the format xxx.xx.x.xx.xx or something similar. Should be a good exercise to teach me how to use regular expressions anyway!
# 6  
Old 07-01-2013
You can just add '.' inside the list of characters to match '[...]', which BTW the list operator also escapes it from being a one-character-match-all metacharacter in all regex contexts. There are four or more flavors of regex, so in low/old and high/new contexts \< does not mean left side of word, and you need to use \b: Regex Tutorial - \b Word Boundaries Some grumpy old UNIX guys say POSIX team got railroaded by the PERL guys to change regex libraries to work their way. But one cannot live in the UNIX/LINUX/C/C++/JAVA world long without needing some regex experience.

Sometimes it gets kinky, like controls to make * shy not greedy (matches min pattern), similar to the difference between the ksh/bash ${name#pattern} (shy) versus ${name##pattern} (aggressive). One common way to make regex * less greedy is to say something like '[^;]*' not '.*', so it stops at the first ';'.
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 calculate difference of split and sum the difference

In the awk I am trying to subtract the difference $3-$2 of each matching $4 before the first _ (underscore) and print that value in $13. I think the awk will do that, but added comments. What I am not sure off is how to add a line or lines that will add sum each matching $13 value and put it in... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. UNIX for Dummies Questions & Answers

Strings to integers?

Hi, I'm totally new at this, so help will be appreciated. I have a directory with a bunch of files in it. The files are named xinteger_yinteger_zinteger.vtk (eg, x3_y0_z-1.vtk). I want to read the filenames and then assign the integers to variables that I then can use in expressions. So, for... (6 Replies)
Discussion started by: jhsinger
6 Replies

3. Shell Programming and Scripting

Finding difference in between two array's of strings

Hi, Can anybody help me in finding the difference between two array elements with the help of code pls. purge=("Purge Concurrent Request and/or Manager Data" "Purge Signon Audit data" "Purge Obsolete Workflow Runtime Data" "Purge Logs and Closed System Alerts") purge_1=("Purge Obsolete... (3 Replies)
Discussion started by: Y.balakrishna
3 Replies

4. UNIX for Dummies Questions & Answers

Des/awk for change format and adding integers in a column of data?

Greetings! I need a quick way to change the format in a table of data Here is an example of the input: 10 72 Value=177 VDB=0.0245 Value4=0,0,171,0 10 274 Value=238 VDB=0.0433 Value4=29,0,205,0 10 312 Value=222 VDB=0.0384 Value4=8,0,190,19 10 540 Value=405 VDB=0.0391 Value4=13,30,153,195... (3 Replies)
Discussion started by: Twinklefingers
3 Replies

5. Shell Programming and Scripting

Grep float/integers but skip some integers

Hi, I am working in bash in Mac OSX, I have following 'input.txt' file: <INFO> HypoTestTool: >>> Done running HypoTestInverter on the workspace combined <INFO> HypoTestTool: The computed upper limit is: 11 +/- 1.02651 <INFO> HypoTestTool: expected limit (median) 11 <INFO> HypoTestTool: ... (13 Replies)
Discussion started by: Asif Siddique
13 Replies

6. Shell Programming and Scripting

Comparison treating strings as zero integers

I'm trying to write a bash script to perform basic arithmetic operations but I want to run a comparison on the arguments first to check that they're a number greater than zero. I want an error to pop up if the arguments args aren't >= 0 so I have: if ! ]; then echo "bad number: $1" fi ... (14 Replies)
Discussion started by: TierAngst
14 Replies

7. Shell Programming and Scripting

using awk to strip out decimal point and trailing integers

Hey guys, I've got an awk string that does a simple calculation which works well. However, I'd really like to be able to strip the decimal point and the trailing ints from it - any ideas on how I'd change the awk string to be able to do that ? I've changed it countless times and each time it... (14 Replies)
Discussion started by: jimbob01
14 Replies

8. Shell Programming and Scripting

Strings to integers in an arithmetic loop

Hi all, Could someone please advise what is the correct syntax for my little script to process a table of values? The table is as follows: 0.002432 20.827656 0.006432 23.120364 0.010432 25.914184 0.014432 20.442655 0.018432 20.015243 0.022432 21.579517 0.026432 18.886874... (9 Replies)
Discussion started by: euval
9 Replies

9. Linux

x25trace - whats this telling me?

Hi people, In need of help, though aren't we all :rolleyes: On a linux server with x25 cards going off to many switches. For one that should be connecting off to Brussels is scrolling the below. No other info being given. Does anyone have any idea what this is telling me? Normally with... (1 Reply)
Discussion started by: nhatch
1 Replies

10. Programming

Telling apart serial from // port

I'm writing a journal_write() function and I want it to: - be a possible drop in replacement for write() - write entries to the journal ;-) Could be a regular file (journal.txt), a serial printer or a // printer. - handle printer status if needed. fstat() tells weather or not we're dealing with... (2 Replies)
Discussion started by: starless
2 Replies
Login or Register to Ask a Question