awk processing of passed variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk processing of passed variables
# 1  
Old 03-18-2013
awk processing of passed variables

Currently have this:

Code:
set current=192.168.0.5
set servicehost = `echo $current | awk -F. '{print $4}'`
echo $numberoffields

5

..but would like to reduce # of variables and eliminate echo to have something like this:

Code:
set servicehost = `awk -v s="$current" -F. 'BEGIN{print $2}'`

But doesn't work. In a similar vein how to determine NR and NF on variables passed via -v .. if possible. For example how to replace the following:

Code:
set string = "hello world"
set numberoffields = `echo $string | awk '{printf("%s\n",NF)}'`
echo $numberoffields

2

..with something like this:

Code:
set string = "hello world"
set numberoffields = awk -v s=$string 'BEGIN{printf("%s\n",NF)}'
echo $numberoffields

0

Would be great if it returned 2 somehow :-)
# 2  
Old 03-18-2013
What shell is that?

In recent bash:
Code:
$ current=192.168.0.5
$ awk -v s="$current" -F. 'BEGIN{$0=s;print $2}'
168

and
Code:
$ awk -v s="$string" 'BEGIN{$0=s; printf("%s\n",NF)}'
2

# 3  
Old 03-18-2013
Ahh "$0=var" is what's missing. Duh. Thank you!
# 4  
Old 03-18-2013
Code:
# current=192.168.0.5
# eval set -- $(echo $current | tr '.' ' ')
# echo $1
192
# echo $2
168
# echo $3
0
# echo $4
5

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to create runtime variables based on the number of parameters passed in the script

Hi All, I have a script which intends to create as many variables at runtime, as the number of parameters passed to it. The script needs to save these parameter values in the variables created and print them abc.sh ---------- export Numbr_Parms=$# export a=1 while do export... (3 Replies)
Discussion started by: dev.devil.1983
3 Replies

2. UNIX for Dummies Questions & Answers

Multiple variables to be passed in a loop

Hi, I need to pass the multiple values of src1 to another variable. I managed to print it but not sure how to assign it to a variable in a loop. src1=01,02,03 echo $src1|awk 'BEGIN {FS=","} {for(i=1;i<=NF;i++) print $i}' I need to pass the value as src2=01 src2=02 src2=03 Thanks... (4 Replies)
Discussion started by: shash
4 Replies

3. Shell Programming and Scripting

Shell Variables passed to awk to return certain rows

Hi Forum. I have the following test.txt file and need to extract certain rows based on "starting position", "length of string" and "string to search for": 1a2b3d 2a3c4d ..... My script accepts 3 parameters: (starting col pos, length to search for, string to search for) and would like to... (4 Replies)
Discussion started by: pchang
4 Replies

4. Shell Programming and Scripting

Multiple bash variables passed into nawk

I have a file that has 2 fields called b_file: 11977 DAR.V3.20150209.1.CSV 3295 DAR.V3.20150209.1.CSV 1721 DAR.V2.20150210.1.CSV I need to search a sftplog using the field 1, but want to maintain the relationship between field 1 and 2. I am passing field 1 as a parameter in a bash loop. ... (14 Replies)
Discussion started by: smenago
14 Replies

5. Shell Programming and Scripting

awk - Why can't value of awk variables be passed to external functions ?

I wrote a very simple script to understand how to call user-defined functions from within awk after reading this post. function my_func_local { echo "In func $1" } export -f my_func_local echo $1 | awk -F"/" '{for (k=1;k<=NF;k++) { if ($k == "a" ) { system("my_local_func $k") } else{... (19 Replies)
Discussion started by: sreyan32
19 Replies

6. Programming

awk processing / Shell Script Processing to remove columns text file

Hello, I extracted a list of files in a directory with the command ls . However this is not my computer, so the ls functionality has been revamped so that it gives the filesizes in front like this : This is the output of ls command : I stored the output in a file filelist 1.1M... (5 Replies)
Discussion started by: ajayram
5 Replies

7. Shell Programming and Scripting

Processing text variables that have numbers in CShell

Hello, I have a case where I need to process an environment variable text value in CShell to calculate the product of 2 numbers stated in the variable string having the format "<first_number>Letter x<second_number>". Ex: $VAR = "24x20" I need to set $VAR_PRODUCT = "Product of 20 and 24"... (3 Replies)
Discussion started by: mohy
3 Replies

8. Shell Programming and Scripting

AWK: Retrieving names of variables passed with -v

I'm an experienced awk user, but this one has me stumped. I have an awk script which is called from a UNIX command line as you'd expect: myscript.awk -v foo=$1 -v bar=$2 filename My question is this: is there a mechanism for determining the names of the -v variables within a script? ... (3 Replies)
Discussion started by: John Mac
3 Replies

9. UNIX for Dummies Questions & Answers

variable passed to awk

Does anybody know how to print a variable passed to awk command? awk -F"|" 'BEGIN {print $job,"\n","Question \n"} {print $1,$2$4," ",$3}' "job=$job1" file1 I am trying to pass job the variable job1. the output is blank. ?? (3 Replies)
Discussion started by: whatisthis
3 Replies

10. UNIX for Dummies Questions & Answers

variable passed to awk

Anybody know what's wrong with this syntax? awk -v job="$job" 'BEGIN { FS="|"} {print $1,$2," ",$4," ",$3\n,$5,"\n"}' list It's keeping give me this message: awk: syntax error near line 1 awk: bailing out near line 1 It seems awk has problem with my BEGIN command. Any... (8 Replies)
Discussion started by: whatisthis
8 Replies
Login or Register to Ask a Question