Visit Our UNIX and Linux User Community

setting up a flag inside awk

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting setting up a flag inside awk
# 1  
Old 01-02-2012
setting up a flag inside awk

Hi unix gurus....

i need to set a flag inside awk. i tried many methods for that but couldn't succeed Smilie
echo "flag= " $flag
echo "flag= " ${flag}

and so...but not able to show the flag. help me out.
i'm using a if condition inside awk... help me out with correct syntax to set a flag and use it so that i will be able to return a value back to main script.......
# 2  
Old 01-02-2012
Not clear. Do you mean you are trying to pass a variable to awk?

awk 'BEGIN { print flag }' flag="1"


awk -v glag="1" 'BEGIN { print flag }'

# 3  
Old 01-02-2012
thanks for your reply....but solved just before that..... Smilie
# 4  
Old 01-02-2012
Originally Posted by sukhdip
thanks for your reply....but solved just before that..... Smilie
Appreciate if you could explain what was the issue and how you solved it. It will help others in the future.

# 5  
Old 01-31-2012
Originally Posted by ahamed101
Appreciate if you could explain what was the issue and how you solved it. It will help others in the future.

hey Sorry, was busy, so replying late. Yeah I'd love to share that.
Just set a flag. Though directly were not able to set a flag. So whole command inside the flag. further took the flag value inside another var and cut the first field (as i need this one only) and used it further.may be it is the long way i used(there could be many ways) but it solved my problem Smilie
Here its:
flag=$( awk -F"|" -v var="$Property_Value" 'BEGIN{n=split(var,a,",")} NR > 1 { for(i=1;i<=n;i++) { if($a[i] ~ /^ *$/){printf("1") }} }' $file)
 echo "flag = "$flag
 var1=$(echo $flag | cut -c1)

# 6  
Old 01-31-2012
One can also try this Smilie

eval $(awk -F"|" -v var="$Property_Value" 'BEGIN{n=split(var,a,",")} NR > 1 { for(i=1;i<=n;i++) { if($a[i] ~ /^ *$/){printf("FLAG=1") }} }' $file)

If you do eval, then FLAG=1 will evaluated as shell command and FLAG will then become a variable.

This method using Eval and Awkis quite handy when you are trying to generate or evaluate variables that may get use by Shell.

So soimething like this:

eval $(awk '{print "X1="$1; print "X2="$2;}' someinputfile)

will give you two shell includable variable X1 and X2...

Last edited by knight_eon; 01-31-2012 at 05:27 AM.. Reason: Thanks itkamaraj for pointing this out :)
# 7  
Old 01-31-2012

use the [ code ] tag, while posting the data or script
This User Gave Thanks to itkamaraj For This Post:

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Proxy Server

Samba on AIX, issues setting read-only flag on files?

Hello, I am having issues setting the "read-only" flag via Windows Explorer on my AIX Samba share... I have on my AIX 7.1 system installed Samba 3.6.24 and configured, joined to our Windows domain successfully. The samba binaries I got from In my smb.conf I have... ... (1 Reply)
Discussion started by: c3rb3rus
1 Replies

2. Shell Programming and Scripting

Passing awk variable argument to a script which is being called inside awk

consider the script below sh /opt/hqe/hqapi1-client-5.0.0/bin/ alert list --host=localhost --port=7443 --user=hqadmin --password=hqadmin --secure=true >/tmp/alerts.xml awk -F'' '{for(i=1;i<=NF;i++){ if($i=="Alert id") { if(id!="") if(dt!=""){ cmd="sh (2 Replies)
Discussion started by: vivek d r
2 Replies

3. Shell Programming and Scripting

Trouble setting up flag ( getopt) for my script

do case $option in d ) CHEC=true;; # more option processing can go here \? ) echo "Unknown option: -$OPTARG" : ) echo "Missing option argument for -$OPTARG";; * ) echo "Unimplimented option: -$OPTARG";; esac done shift $(($OPTIND - 1)) (2 Replies)
Discussion started by: upenmishra
2 Replies

4. Shell Programming and Scripting

HELP with AWK one-liner. Need to employ an If condition inside AWK to check for array variable ?

Hello experts, I'm stuck with this script for three days now. Here's what i need. I need to split a large delimited (,) file into 2 files based on the value present in the last field. Samp: Something.csv bca,adc,asdf,123,12C bca,adc,asdf,123,13C def,adc,asdf,123,12A I need this split... (6 Replies)
Discussion started by: shell_boy23
6 Replies

5. Shell Programming and Scripting

awk: setting decimal

hello, when I type the following command awk -v varc="$i" '{ new=($1*varc)} { print new }'it gives outputs in 5 decimal. How can I set my outputs to 9 decimal by using awk. thanks (5 Replies)
Discussion started by: rpf
5 Replies

6. Shell Programming and Scripting

awk inside another awk statement

hi all, i have two files 1) a.txt one two three 2) abc "one" = 10 pqr "three" = 20 345 "two" = 0 this is what i want in third file (3 Replies)
Discussion started by: shishirkotkar
3 Replies

7. UNIX for Dummies Questions & Answers

Awk inside Awk expression

Hi, It can be used awk inside other Awk?. I need to get another text processing while other text process. Thank you. (2 Replies)
Discussion started by: pepeli30
2 Replies

8. Shell Programming and Scripting

using flag inside a for loop to check condition

I have a logic like this It initializes the flag variable as "T" at the beginning of the loop everytime Inside each loop it checks for two conditions and updates the flag variable as "A" or "B" In the end of the loop it checks for the value of the variable flag for "A" or "B" and execute... (4 Replies)
Discussion started by: codeman007
4 Replies

9. Shell Programming and Scripting

using array inside awk

Hi All, I have the following code sequence for reading some bulk file and moving the content to two different arrays. while read data do THREEG_PATTERN=`echo $data | egrep "3G"` if then NEW_THREEG_PATTERN=`echo $THREEG_PATTERN | cut -d " " -f2` ... (12 Replies)
Discussion started by: subin_bala
12 Replies

10. Shell Programming and Scripting

Setting the FS in awk

I am writing a POSIX script where I need to set the FS equal to a variable that was set prior to executing the awk command. Is there a way to set FS equal to a variable that has already been set in the script? Example LINE="FIELD1~FIELD2~FIELD3~FIELD4~FIELD5" (the separator could be... (4 Replies)
Discussion started by: 2reperry
4 Replies

Featured Tech Videos