Need help with using a script variable in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help with using a script variable in awk
# 1  
Old 01-28-2013
Bug Need help with using a script variable in awk

Hi All,

Please help me with a issue in below code

Code:
head -1 sample.txt > header.txt
field_count=`head -1 sample.txt |  tr -dc '\t' | wc -c`
field_count=`expr $field_count + 1`
i=1
while [ $i -lt $field_count ]
do
j=`expr $i + 1`
field_name[$i]= `awk -F\t 'NR>=1{print ${$j}}' header.txt`
echo ${field_name[$i]}
i=`expr $i + 1`
done

I have issue with the awk statement , the last echo statement.

My requirement: to store the headers from tab delimited file into an array and print them
Note: I dont know how many headers my input file may have.

Regards,
Ajay
# 2  
Old 01-28-2013
Can you give us a representative sample content of file: sample.txt and desired output?
# 3  
Old 01-28-2013
You can achieve this with eval.
Try this:

Code:
head -1 sample.txt > header.txt
field_count=`head -1 sample.txt |  tr -dc '\t' | wc -c`
field_count=`expr $field_count + 1`
i=1
while [ $i -lt $field_count ]
do
j=`expr $i + 1`
CMD="awk -F\t 'NR>=1{print \$$j}' header.txt"
field_name[$i]=`eval $CMD`
echo ${field_name[$i]}
i=`expr $i + 1`
done

# 4  
Old 01-28-2013
What shell is that? Bash is as easy as:

Code:
mute@clt:~/temp$ cat sample.txt
first_name      last_name       street1 city    state   zip
neutron scott   123 e main st   ca      90210
mute@clt:~/temp$ ./headers
declare -a field_names='([0]="first_name" [1]="last_name" [2]="street1" [3]="city" [4]="state" [5]="zip")'
mute@clt:~/temp$ cat headers
#!/bin/bash

IFS=$'\t' read -ra field_names < <(head -1 sample.txt)

declare -p field_names

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How can I assign awk's variable to shell script's variable?

I have the following script, and I want to assign the output ($10 and $5) from awk to N and L: grdinfo data.grd | awk '{print $10,$5}'| read N L output from gridinfo data.grd is: data.grd 50 100 41 82 -2796 6944 0.016 0.016 3001 2461. where N and L is suppose to be 3001 and 100. I use... (8 Replies)
Discussion started by: geomarine
8 Replies

2. Shell Programming and Scripting

Variable input to awk script

Hi guys, I wrote the following function to compare two csv files column by column. However, sometimes the input needs to be sorted before parsing it to awk. I can do this by changing the awk arguments, but I would like to make this variable if possible. The below doesn't work since the... (3 Replies)
Discussion started by: Subbeh
3 Replies

3. Shell Programming and Scripting

How to pass variable from awk script to shell?

Hi, Please need to print the Rej variable outsite the awk script which is given below...please advised how to achieve it. #!/bin/bash echo "Enter DMU Pipe delimited File name for the Feed to be validated" read DMU_File echo "Enter Pre-DMU File name for the Feed" read Predum_file ... (3 Replies)
Discussion started by: pelethangjam
3 Replies

4. UNIX for Dummies Questions & Answers

Inserting a variable in awk script

I have file input.txt: >TX1-1 Freq 55 cattctgatgaatatttgtcctttagttgttatttgt >TX1-2 Freq 19 cattctgatgaatatttgtcctttagttgttatttgt >TX1-3 Freq 17 cattctgatgaatatttgtcctttagttgttatttgt >TX1-4 Freq 6 cattctgatgaatatttgtcctttagttgttatttgt >TX1-5 Freq 6 cattctgatgaatatttgtcctttagttgttatttgt ... (5 Replies)
Discussion started by: Xterra
5 Replies

5. 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/hqapi.sh 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 someScript.sh... (2 Replies)
Discussion started by: vivek d r
2 Replies

6. Shell Programming and Scripting

Passing shell variable to awk script

I want to pass a shell variable to awk script : # cat file PSAPSR3 3722000 91989.25 2 98 PSAPSR7 1562000 77000.1875 5 95 PSAPUNDO 92000 4087.5625 4 96 #... (8 Replies)
Discussion started by: Reboot
8 Replies

7. Shell Programming and Scripting

assign awk's variable to shell script's variable?

Dear All, we have a command output which looks like : Total 200 queues in 30000 Kbytes and we're going to get "200" and "30000" for further process. currently, i'm using : numA=echo $OUTPUT | awk '{print $2}' numB=echo $OUTPUT | awk '{print $5}' my question is : can I use just one... (4 Replies)
Discussion started by: tiger2000
4 Replies

8. Shell Programming and Scripting

How to pass a Awk variable to another script

Read parameter from a text file with one line which stored the date value like 20080831; below is the awk command I used gawk -F, "{getline RunDate;print $RunDate" text file When print $RunDate, it display 20080831 Would like to pass this variable to another script to use but not... (6 Replies)
Discussion started by: cbauw
6 Replies

9. Shell Programming and Scripting

Pass script variable value to AWK

HI all, some more mistery about AWK, I hope you can help me out: 1) I have a normal ksh script and sometime I call awk command. I set some variables in the script and I would like to use them up within AWK as well. Unfortunately AWK seems to forget all the variable values outside of its own... (1 Reply)
Discussion started by: BearCheese
1 Replies

10. UNIX for Advanced & Expert Users

Passing a variable into an awk script

Hello all, I'm trying to run a script of this format - for i in $(cat <file>); do grep $i <file1>|awk '{print $i, $1, $2}' It's not working - does anyone know how this can be done? Khoom (5 Replies)
Discussion started by: Khoomfire
5 Replies
Login or Register to Ask a Question