awk to extract value from column using variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk to extract value from column using variable
# 1  
Old 05-17-2016
awk to extract value from column using variable

I am having trouble extracting the value in the columns declared in a variable. I have tried several different variation of awk but both give me the column number and not the actual value of that column.

Any suggestions?

Neither of the "extract" variables below are performing as desired


at the command line
Code:
script.sh 2016 04 01 2016 04 05

script.sh
Code:
byear=$1
bmonth=${2}
bday=${3}
eyear=${4}
emonth=${5}
eday=${6}


bjulian=`date -d "$1/$2/$3" +%j`
ejulian=`date -d "$4/$5/$6" +%j`
firstcolumn=`expr ${bday} + 1`
lastcolumn=`expr ${eday} + 1`

x=`expr ${bjulian} - 1`
until [ $x -eq "$ejulian" ] ; do
for i in `seq $firstcolumn $lastcolumn`; do
extract=$(awk '{print ('$i')}'  input.file)
#extract=$(awk -v myvar="$i" 'BEGIN{print myvar}'  input.file)
((x++))
echo "$x" "$extract"
done
done
exit

input.file
Code:
blah   13     25      0    18      0     0    178 ..........

Current Output
Code:
92 2
93 3
94 4
95 5
96 6

Desired Output
Code:
92 13
93 25
94 0
95 18
96 0

# 2  
Old 05-17-2016
Code:
 extract=$(awk '{print $fld}' fld=$i input.file)

This User Gave Thanks to rdrtx1 For This Post:
# 3  
Old 05-17-2016
Prefix it with $ in awk:
Code:
extract=$(awk '{print ($'$i')}'  input.file)

or
Code:
extract=$(awk -v myvar=$i '{print ($myvar)}'  input.file)

This User Gave Thanks to MadeInGermany For This Post:
# 4  
Old 05-17-2016
$ is the operator for column in awk.

Code:
$ awk -v COL=3 '{ print $COL }' <<EOF
A B C D E
EOF

C

$

[edit] Okay, 3 people beat me to it. I still like my example anyway Smilie
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 05-17-2016
Thanks to all that replied. Great examples!
# 6  
Old 05-17-2016
With bash builtins you can perhaps avoid awk and improve efficiency:
Code:
...
read -a arr < input.file
for i in seq `$firstcolumn $lastcolumn`; do
extract=${arr[i-1]}
((x++))
echo "$x" "$extract"
done
...

---------- Post updated at 03:26 PM ---------- Previous update was at 02:57 PM ----------

If the input.file is static it can be read once before the outer loop.
Plus some more optimization ideas:
Code:
...
read -a arr < input.file
x=$bjulian
until [ $x -gt $ejulian ]; do
  i=$firstcolumn
  until [ $i -gt $lastcolumn ]; do
    extract=${arr[i-1]}
    echo "$x" "$extract"
    ((x++))
    ((i++))
  done
done
...

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

awk - Extract 4 lines in Column to Rows Tab Delimited between tags

I have tried the following to no avail. xargs -n8 < test.txt awk '{if(NR%6!=0){p=""}else{p="\n"};printf $0" "p}' Mod_Alm_log.txt > test.txt I have tried different variations of the above, the problem is mixes lines together. And it includes the tags "%a and %A" I need them to be all tab... (16 Replies)
Discussion started by: mytouchsr
16 Replies

2. Shell Programming and Scripting

Compare files & extract column awk

I have two tab delimited files as given below: File_1: PV16 E1 865 2814 1950 PV16 E2 2756 3853 1098 PV16 E4 3333 3620 288 PV16 E5 3850 4101 252 PV16 E6 83 559 477 PV16 E7 562 858 297 PV16 L2 4237 5658 ... (10 Replies)
Discussion started by: vaibhavvsk
10 Replies

3. Shell Programming and Scripting

extract column with awk

This is a example TXT file: ID CDR1 CDR2 CDR3 1 CDR1 CDR3 2 CDR1 CDR2 CDR3 3 CDR3 What I want to do is extract the fourth column with awk, however, there are some blank space in some field. I get wrong result when it print out awk result. awk '{print $1,$4}'... (8 Replies)
Discussion started by: xshang
8 Replies

4. Shell Programming and Scripting

Need to extract data from Column having variable length column

Hi , I need to extract data from below mentioned data, having no delimiter and havin no fixed column length. For example: Member nbr Ref no date 10000 1000 10202012 200000 2000 11202012 Output: to update DB with memeber nbr on basis of ref no. ... (6 Replies)
Discussion started by: ns64110
6 Replies

5. Shell Programming and Scripting

Printing a variable column using awk

Hi everyone, Ok here's the scenario. I have a control file like this. component1,file1,file2,file3,file4,file5 component2,file1,file2,file3,file4,file5I want to do a while loop here to read all files for each component. file_count=2 while ] do file_name=`cat list.txt | grep... (2 Replies)
Discussion started by: The Gamemaster
2 Replies

6. Shell Programming and Scripting

for each different entry in column 1 extract maximum values from column 2 in unix/awk

Hello, I have 2 columns (1st column has multiple entries but the corresponding values in the column 2 may be the same or different.) however I want to extract unique values for each entry in column 1 by assigning the max value from column 2 SDF4 -0.211654 SDF4 0.978068 ... (1 Reply)
Discussion started by: Diya123
1 Replies

7. Shell Programming and Scripting

awk/sed to extract column bases on partial match

Hi I have a log file which has outputs like the one below conn=24,196 op=1 RESULT err=0 tag=0 nentries=9 etime=3,712 dbtime=0 mem=486,183,328/2,147,483,648 Now most of the time I am only interested in the time ( the first column) and a column that begins with etime i.e... (8 Replies)
Discussion started by: pkabali
8 Replies

8. UNIX for Dummies Questions & Answers

Using GREP/AWK to extract a number and store it as a variable

Hello, I have a question regarding the awk command. Here is the line I need to grep: 1 F= -.13250138E+03 E0= -.13249556E+03 d E =-.174650E-01 mag= 35.2157 Instead of displaying the number in red I would like to store it as a variable such as X. Is there a way to do this? Thanks for any... (3 Replies)
Discussion started by: modey3
3 Replies

9. Shell Programming and Scripting

How to extract a column from two different files in AWK?

Hi guys, I need help in extracting one column of numbers from two different files and display it in a output file. In specific, I want to extrac the column no.2 ($2) from each file, file1.txt, file2.txt. Then place both extracted columns in a one file, out.txt. the line command I use to... (7 Replies)
Discussion started by: solracq
7 Replies

10. 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
Login or Register to Ask a Question