using unix variable in select column in awk


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers using unix variable in select column in awk
# 1  
Old 03-26-2010
using unix variable in select column in awk

Hi,

I have file on below pattern, and i want to write a generic awk to handle all cases.

input_file:
col1~col2~col3~col4~col5~col6

I need to generate 4 files something like this
File1:
col1~~col2~~col3

File2:
col1~~col2~~col4

File3:
col1~~col2~~col5

File4:
col1~~col2~~col6

I have written

Code:
nawk -v ff=$column_no -F'~' 'BEGIN { OFS="~~"} { print $1,$2,ff > "'$out_file_name'" } ' input_file

I have written above awk under for loop so that this column_no get incremented everytime, so that it picks up different colum for each iteration. [[ and also out_file_name variable get changed in each iteration]]

when i execute this, I get output as

File 1:
col1~~col2~~3 <== prints number "3" instead of picking thrid column value from the input file.

[[ Same thing for all the files ]]

Am i overlooking awk to achieve my request? Pls correct me if doing something wrong or sytax problem.

I knew it can be achieved by writting 4times. But my actual case it is more than 12. So thought of writting professional way, to handle all possible cases.

Kind Regards,
Balaji.

Note I tried another approach also
Code:
nawk -F'~' 'BEGIN { OFS="~~"} { print $1,$2,"'$column_no'" > "'$out_file_name'" } ' input_file

but hasnt pass syntax validation.

---------- Post updated at 11:06 AM ---------- Previous update was at 10:31 AM ----------

I have acheived through below code.

Code:
cut -d'~' -f 1,2,$column_no input_file | nawk -F'~' 'BEGIN { OFS="~~"} { print $1,$2,$3 > "'$file_name'"}'

Let me know if there is further best way to code this.
# 2  
Old 03-26-2010
Try...
Code:
nawk 'BEGIN {FS="~";OFS="~~"}{print $1, $2, $ff}' ff=$column_no input_file > $out_file_name

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to select lines with maximum value of each record based on column value

Hello, I want to get the maximum value of each record separated by empty line based on the 3rd column of each row within each record? Input: A1 chr5D 634 7 82 707 A2 chr5D 637 6 82 713 A3 chr5D 637 5 82 713 A4 chr5D 626 1 82 704... (4 Replies)
Discussion started by: yifangt
4 Replies

2. 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

3. Shell Programming and Scripting

grep/awk column or variable?

Hi, I'm running via PuTTY, in a BASH shell to do my work. I'm running calculations where steps are reported like this every 100 steps: NSTEP = 249900 TIME(PS) = 249.900 TEMP(K) = 299.94 PRESS = 21.1 Etot = -12912.5557 EKtot = 4996.8780 EPtot = -17909.4336 ... (6 Replies)
Discussion started by: Oriksagtaria
6 Replies

4. Shell Programming and Scripting

Assigning value of a select count(*) from tablename ( run on db2 ) to a unix variable

Hi All, I have browsed through the forums for a similar topic, but there is no topic which explains about this problem, when the backend is DB2. I want to assign the output of a select count(*) from tablename to a unix variable in a shell script. I am using ksh. The database used to... (3 Replies)
Discussion started by: Siddarth
3 Replies

5. Shell Programming and Scripting

Awk - how to select the column based on day of month

Problem, How can you pass today's date (eg) 18 into Awk and select the field representing the 18th column? I have an output that I want to interact with based on what day it is information=0:0 192:0 5436:0 22:99 0:0 0:0 1234:0 1359:09 DAY=date'+%d' numbrs=`$information | awk... (2 Replies)
Discussion started by: Gizmo1007
2 Replies

6. Shell Programming and Scripting

awk to select rows based on condition on column

I have got a file like this 003ABC00281020091005000100042.810001 ... (8 Replies)
Discussion started by: Maruti
8 Replies

7. Shell Programming and Scripting

How to redirect value from sql select statment to unix variable

Hi, I need to get the value from the table using the sql command and store this value into the unix variable so that i can use this value for furthure use.. Please can any body help me in this regards Thanks & Regards Abdul Hafeez Shaik (17 Replies)
Discussion started by: abdulhafeez
17 Replies

8. Shell Programming and Scripting

Using a variable to select records with awk

As part of a bigger task, I had to read thru a file and separate records into various batches based on a field. Specifically, separate records based on the value in the batch field as defined below. The batch field left-justified numbers. The datafile is here > cat infile 12345 1 John Smith ... (5 Replies)
Discussion started by: joeyg
5 Replies

9. UNIX for Dummies Questions & Answers

select count(*) in sqlplus into variable unix shell

Need to select count(*) from table to check for zero result in unix script (2 Replies)
Discussion started by: struggle
2 Replies

10. Shell Programming and Scripting

awk to select a column from particular line number

The awk command awk -F: '{print $1}' test1 gives the first columns of all the lines in file ,is there some command to get a particular column from particular line . Any help is appreciated. thanks arif (4 Replies)
Discussion started by: mab_arif16
4 Replies
Login or Register to Ask a Question