awk print variable then fields in variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk print variable then fields in variable
# 1  
Old 04-06-2013
awk print variable then fields in variable

i have this variable:

Code:
varT="1--2--3--5"

i want to use awk to print field 3 from this variable. i dont want to do the "echo $varT".

but here's my awk code:
Code:
awk -v valA="$varT" "BEGIN {print valA}"

this prints the entire line. i feel like i'm so close to getting what i want. i basically just want to fork another awk process or any other command.

i was thinking this would work, but it didn't:

Code:
awk -v valA="$varT" "BEGIN {print valA,$3}"

# 2  
Old 04-06-2013
If you want to do it that way you could try:
Code:
awk -v valA="$varT" 'BEGIN{split(valA,F,/--/);print F[3]}'

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 04-06-2013
Quote:
Originally Posted by Scrutinizer
If you want to do it that way you could try:
Code:
awk -v valA="$varT" 'BEGIN{split(valA,F,/--/);print F[3]}'

this worked perfectly. can i print the last field using this command? i tried:

Code:
awk -v valA="$varT" 'BEGIN{split(valA,F,/--/);print F[NF]}'

but i didn't get anything.
# 4  
Old 04-06-2013
Try:
Code:
awk -v valA="$varT" 'BEGIN {n=split(valA,F,/--/); print F[n]}'

This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 04-06-2013
AWK split function returns the number of elements created:
Code:
awk -v valA="$varT" 'BEGIN{n=split(valA,F,/--/);print F[n]}'

This User Gave Thanks to Yoda For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Awk: subset of fields as variable with sprint

Dear Unix Gurus, input: A|1|2|3|4|5 B|1|2|3|4|3 C|1|2|3|4|1 D|1|9|3|4|12 output: A_(5);B(3);C(1)|1|2|3|4 D_(12)|1|9|3|4 Details: If $2, $3, $3, $5 are identical, concatenate $1 and associated $NF together in the first field. But I am trying to do the above by passing the identical... (6 Replies)
Discussion started by: beca123456
6 Replies

2. Shell Programming and Scripting

awk processing of variable number of fields data file

Hy! I need to post-process some data files which have variable (and periodic) number of fields. For example, I need to square (data -> data*data) the folowing data file: -5.34281E-28 -3.69822E-29 8.19128E-29 9.55444E-29 8.16494E-29 6.23125E-29 4.42106E-29 2.94592E-29 1.84841E-29 ... (5 Replies)
Discussion started by: radudownload
5 Replies

3. Shell Programming and Scripting

Concatenating awk fields with variable

Hi, I am trying to do this:- FILE=application.log PID=12345 FILE=`echo $FILE | awk -F "." '{print $1 "$PID" $2}'` echo $FILE application$PIDlog I need the output to be application12345.log but I am not sure how to get the $PID variable into the output. I have tried various things... (3 Replies)
Discussion started by: sniper57
3 Replies

4. Shell Programming and Scripting

Variable won't print out - awk

done. (1 Reply)
Discussion started by: myaa02
1 Replies

5. Shell Programming and Scripting

awk print using a variable

hey, just want to ask how to do this. ex. g="hi i am john" h=`echo $g | awk '{print $2}'` echo $h OUTPUT is 'i' What if I want to use a variable instead of using '2', how do I do that? Because this one does not work: a=2 h=`echo $g | awk '{print ${$a}}'` this one also does not... (3 Replies)
Discussion started by: h0ujun
3 Replies

6. Shell Programming and Scripting

awk print variable

I have list of files: ls a.pdf b.pdf c.pdf and so on... and I have a file like this: cat file1 apple mango pear and so on... I want to rename my file like this: (7 Replies)
Discussion started by: zorrox
7 Replies

7. Shell Programming and Scripting

How to print a value in the variable using awk ?

:b:Hi All, I have a part of a script below: var1="value1" awk 'BEGIN {printf("%36s \n ","value1")}' Instead of directly giving the "value1" , I need to give using "var1" in the above awk statement. Is this possible? If so, what is the modified awk command? Thanks in advance JS (1 Reply)
Discussion started by: jisha
1 Replies

8. Shell Programming and Scripting

print variable in file using awk

hi, i have store variable in $var i want to print it in $3 of file using awk. how can i do it? file : var1,var2,var3 var5,var6,var7 var8,var9,var10 . . . i want to print $var in $3 ( for example var3 or var7 or var10) thanks (3 Replies)
Discussion started by: kamel.seg
3 Replies

9. Shell Programming and Scripting

can awk print column using a variable ??

i want to print the column file using awk or cut in dynamic manner like trmp=2;temp1=1;temp3=2 awk 'BEGIN{OFS=IFS="\t"} {print $temp,$temp1,$temp3}' client_data.txt or cut -f $temp1,$temp2,$temp3 -d"\t" file_name . but it is showing error , In awk can i use variable as in printing... (36 Replies)
Discussion started by: jambesh
36 Replies

10. Shell Programming and Scripting

print variable in awk

i read the variable ph from file and i wanna to print it in awk. example ph=`cat tmpbatch` tail h.txt|grep "| |"|awk -F"|" '{ print "@unpdx.sql",$5 }'"$ph" i try this but it does not work (8 Replies)
Discussion started by: kazanoova2
8 Replies
Login or Register to Ask a Question