Pointer for current field???


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Pointer for current field???
# 1  
Old 07-27-2011
Pointer for current field???

I'm writing my first bash function, and I'm having a little bit of trouble getting used to the syntax. I have been told that it is very similar to Java, but I'm still not quite getting it.

The situation that I am dealing with is checking the values on input fields. There will be anywhere between two and five fields, so the code has to be able to adapt. The fields with either be a string, or a number. This is what I have so far...I am also using the match function, which I have never used before.

This function takes place within a file, which is used as a set of rules to be executed in an awk statement.
Code:
#is there a way to make a pointer for the field you are currently looking at?

function get(){
 #do bash functions take on parameters?
 #I don't know if it will be executed like get($i) or $i.get()?

if ( match( CURRENT FIELD? /‘0' | [1-9][0-9]*/)
     do something
if ( match( CURRENT FIELD? /[a-z]+/)
     do something else
}

So, my question is, is there a way to have a pointer for the current field? I need to check each one, one by one, every time. I haven't been able to find this answer anywhere else, so I'm hoping someone can help me out.

Also, does the function in bash take parameters? Or would it be executed get( current field variable...???) or currentfieldvariable.get()?

I don't really need help with the "do something else" part of the code, because I know how to do it.
# 2  
Old 07-28-2011
Have you tried:
Code:
function get(){
  echo "Inside of get <${1}>"
}
# For constant parameters:
for mEach in $(echo "abc def ghi"); do
  echo "Now calling get with ${mEach}"
  get ${mEach}
done

# For command line parameters:
for mEach in $@; do
  echo "Now calling get with ${mEach}"
  get ${mEach}
done

# 3  
Old 07-28-2011
BASH has variable references ala ${!VARNAME}, if VARNAME is ABC will give you the value of $ABC

Setting the variable through a reference is harder though, you have to build a shell statement and feed it into eval.

It would probably help to see your code, there may be ways to do this without using references of any sort.

Functions do indeed take parameters. Parameter 1 becomes $1, parameter 2 becomes $2, etc. You can use shift to put it in a loop -- it pops off the first parameter, making $2 into $1, $3 into $2, etc.
Code:
function func
{
        while [ "$#" -gt 0 ]
        do
                echo "Parameter is $1"
                shift
        done
}

func a "b c"


Last edited by Corona688; 07-29-2011 at 01:29 PM.. Reason: forgot the shift!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Display combination of 4 field uniqe record and along with concatenate 5th and 6th field.

Table ACN|NAME|CITY|CTY|NO1|NO2 115|AKKK|ASH|IND|10|15 115|AKKK|ASH|IND|20|20 115|AKKK|ASH|IND|30|35 115|AKKK|ASH|IND|30|35 112|ABC|FL|USA|15|15 112|ABC|FL|USA|25|20 112|ABC|FL|USA|25|45 i have written shell script using cut command and awk programming getting error correct it and add... (5 Replies)
Discussion started by: udhal
5 Replies

2. Linux

How do I format a Date field of a .CSV file with multiple commas in a string field?

I have a .CSV file (file.csv) whose data are all enclosed in double quotes. Sample format of the file is as below: column1,column2,column3,column4,column5,column6, column7, Column8, Column9, Column10 "12","B000QRIGJ4","4432","string with quotes, and with a comma, and colon: in... (3 Replies)
Discussion started by: dhruuv369
3 Replies

3. UNIX and Linux Applications

Reflection Macro: Use Current Date Information in specific field of report

Hi Everyone, New to the forum, and have my first question. I have the following Macro currently being used within Telnet to run a report for the date being entered. However, now I need the Macro to input the day after today to the the day after today. Example: If today is May 9, 2012 - the... (0 Replies)
Discussion started by: CipherEffect
0 Replies

4. Shell Programming and Scripting

Compare Field in Current Line with Field in Previous

Hi Guys I have the following file Essentially, I am trying to find the right awk/sed syntax in order to produce the following 3 distinct files from the file above: Basically, I want to print the lines of the file as long as the second field of the current line is equal to the... (9 Replies)
Discussion started by: moutaye
9 Replies

5. Shell Programming and Scripting

AWK: Pattern match between 2 files, then compare a field in file1 as > or < field in file2

First, thanks for the help in previous posts... couldn't have gotten where I am now without it! So here is what I have, I use AWK to match $1 and $2 as 1 string in file1 to $1 and $2 as 1 string in file2. Now I'm wondering if I can extend this AWK command to incorporate the following: If $1... (4 Replies)
Discussion started by: right_coaster
4 Replies

6. Shell Programming and Scripting

csv file field needs to be changed current system date with awk

HI, I have csv file with records as shown below. 4102,Bangalore,G10,21,08/17/2011 09:28:33:188,99,08/17/2011 09:27:33:881,08/17/2011... (1 Reply)
Discussion started by: raghavendra.nsn
1 Replies

7. Shell Programming and Scripting

awk, comma as field separator and text inside double quotes as a field.

Hi, all I need to get fields in a line that are separated by commas, some of the fields are enclosed with double quotes, and they are supposed to be treated as a single field even if there are commas inside the quotes. sample input: for this line, 5 fields are supposed to be extracted, they... (8 Replies)
Discussion started by: kevintse
8 Replies

8. UNIX for Dummies Questions & Answers

comparing field to current year

Hi, I have a comma delimited file that contains name, account number, and account date/time(example record below). I want to pull off all the records that have an account date greater than 8/1 of the current year, and create a new file with those records. So for this year, it would take... (6 Replies)
Discussion started by: keeferb
6 Replies

9. Shell Programming and Scripting

Sort alpha on 1st field, numerical on 2nd field (sci notation)

I want to sort alphabetically on the first field and sort in descending numerical order on the 2nd field. With a normal "sort -r -n" it does this: abc ||| 5e-05 ||| bla abc ||| 3 ||| ble def ||| 1 ||| abc def ||| 0.2 ||| def As you can see it ignores the fact that 5e-05 is actually 0.00005... (1 Reply)
Discussion started by: FrancoisCN
1 Replies

10. Programming

pass a pointer-to-pointer, or return a pointer?

If one wants to get a start address of a array or a string or a block of memory via a function, there are at least two methods to achieve it: (1) one is to pass a pointer-to-pointer parameter, like: int my_malloc(int size, char **pmem) { *pmem=(char *)malloc(size); if(*pmem==NULL)... (11 Replies)
Discussion started by: aaronwong
11 Replies
Login or Register to Ask a Question