can awk print column using a variable ??


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting can awk print column using a variable ??
# 29  
Old 09-18-2006
If the file is not separated by tab space then you have to give delimiter
cut -d" " -f1 jam2.txt > one.txt
# 30  
Old 09-20-2006
my requirement got slightly changed now
problem is like : I have a look up file which contain the fixed
e.g

T0011.csv (Look up file)
--------------------------
col1,col2,col3,col4,col5,col6,col7,col8,col9

I have client file which conatin column

T0011.csv (Same as look up file name but in diff path i.e in srcFile folder)
----------------------------------------------------------------
col4 col6
510 erre
510 tru64
510 xxxx


------------Final file should be-----
T0011.csv
-----------------------
col1 col2 col3 col4 col5 col6 col7 col8 col9
510 erre
510 tru64
510 xxxx


here the only change i need is the script should look the client file column in lookup file
it should insert the data of the client file to final table which actually conain all the column of look up file.
here clint file contain col4 and col6 so it should insert the data in the col4 and col6 in sequence as client did not supply the rest of the column all other column should have value blank.

Please assist me urgent !
# 31  
Old 09-20-2006
jambesh is there any posibility that data file may have columns at random sequence like
Code:
col6      col4

or will be in the sequence like col4 col6
# 32  
Old 09-25-2006
Yes Dhruv Data file can be in any order but final file should be according to the lookup sequence order and if data file contain c3,c1 and look up file contain c1,c2,c3,c4,c5,c6
then final file should be c1,c2,c3,c4,c5,c6 column but c2,c4,c5,c6 should conatin blank and c1 and c3 column contain data from client data.

------

Paste command has limitation of pasting only 12 file max to a singler file.
if a lookupfile has more then 12 column then it is throwing error too many argument or bad argumnet ..
how to come around this situation ??
paste -d| temdata* > final_data
here if there is more then 12 tempdata file ..paste will fail.
# 33  
Old 09-26-2006
How many columns lookup file will be having?what i want to ask is if we know the the number of columns lookup file will be having we can work on paste command.
# 34  
Old 09-26-2006
can we write one script like join_all which can pick up n number of file and join the column of each file in sequence ??
each file having one column it could be all space or data column.
if we can do that then this can be short out
# 35  
Old 09-26-2006
A possible solution using awk:
Code:
#
# Lookup file, first line 
#
NR==1 {
   LookupColsCount = split($0, LookupCols, /,/);
   FinalHeader     = $0;
   gsub(/,/, OFS, FinalHeader);
   next;
}

#
# Client file, first line
#
FNR == 1 && FNR != NR {
   ClientColsCount = NF;
   for (i=1; i<=NF; i++) 
      ClientCols[$i] = i;

   FinalColsCount = LookupColsCount;
   for (i=1; i<=LookupColsCount; i++) {
      col_id = LookupCols[i];
      if (col_id in ClientCols)
         FinalCols[i] = ClientCols[col_id];
      else 
         FinalCols[i] = 0;
   }

   print FinalHeader;
   next;
}

#
# Client file, datas
#
FNR != NR {
   FinalLine = "";
   for (i=1; i<=FinalColsCount; i++) {
      if (i > 1) FinalLine = FinalLine OFS;
      col_num = FinalCols[i];
      if (col_num != 0)
         FinalLine = FinalLine $col_num;
   }
   print FinalLine;
}

Execute this awk script with the lookup and client files as input.
You can choose the Output Field Separator with the -v option (the default value is space).
In the following example, OFS is set to '|' :
Code:
awk -v OFS='|' -f script.awk  T0011.csv srcFile/T0011.csv

The output is
Code:
col1|col2|col3|col4|col5|col6|col7|col8|col9
|||510||erre|||
|||510||tru64|||
|||510||xxxx|||


Jean-Pierre.

Last edited by aigles; 09-26-2006 at 08:33 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need awk or Shell script to compare Column-1 of two different CSV files and print if column-1 matche

Example: I have files in below format file 1: zxc,133,joe@example.com cst,222,xyz@example1.com File 2 Contains: hxd hcd jws zxc cst File 1 has 50000 lines and file 2 has around 30000 lines : Expected Output has to be : hxd hcd jws (5 Replies)
Discussion started by: TestPractice
5 Replies

2. Shell Programming and Scripting

Awk: Print count for column in a file using awk

Hi, I have the following input in a file & need output as mentioned below(need counter of every occurance of field which is to be increased by 1). Input: 919143110065 919143110065 919143110052 918648846132 919143110012 918648873782 919143110152 919143110152 919143110152... (2 Replies)
Discussion started by: siramitsharma
2 Replies

3. Shell Programming and Scripting

awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi, My input files is like this axis1 0 1 10 axis2 0 1 5 axis1 1 2 -4 axis2 2 3 -3 axis1 3 4 5 axis2 3 4 -1 axis1 4 5 -6 axis2 4 5 1 Now, these are my following tasks 1. Print a first column for every two rows that has the same value followed by a string. 2. Match on the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

4. Shell Programming and Scripting

awk print variable then fields in variable

i have this variable: 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: 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... (4 Replies)
Discussion started by: SkySmart
4 Replies

5. Shell Programming and Scripting

awk command to print only selected rows in a particular column specified by column name

Dear All, I have a data file input.csv like below. (Only five column shown here for example.) Data1,StepNo,Data2,Data3,Data4 2,1,3,4,5 3,1,5,6,7 3,2,4,5,6 5,3,5,5,6 From this I want the below output Data1,StepNo,Data2,Data3,Data4 2,1,3,4,5 3,1,5,6,7 where the second column... (4 Replies)
Discussion started by: ks_reddy
4 Replies

6. Solaris

awk - Print variable number of colums from a starting column

Hi guys, I usualy am able to google awk stuff but I can't find it so far and there are so many awking gurus here that I will give it a shot. I want to print $1;$3;"$5 up to the $NF". In other words, I can have 1000 colums, but need to have $5 up to the end. I started with the idea of... (2 Replies)
Discussion started by: plmachiavel
2 Replies

7. Shell Programming and Scripting

Print column with awk

Hi Masters , Need your help regarding the below issue . I am creating a new file with awk where diff rows in a particular column will different according to the user i/p. If user i/p is 10,11 and 12 and requests for 6 rows then in a particular column will print with 10 10 11 11 12... (6 Replies)
Discussion started by: Pratik4891
6 Replies

8. Shell Programming and Scripting

find expression with awk in only one column, and if it fits, print whole column

Hi. How do I find an expression with awk in only one column, and if it fits, then print that whole column. 1 apple oranges 2 bannanas pears 3 cats dogs 4 hesaid shesaid echo "which number:" read NUMBER (user inputs number 2 for this example) awk " /$NUMBER/ {field to search is field... (2 Replies)
Discussion started by: glev2005
2 Replies

9. Shell Programming and Scripting

to print column using awk

suppose u have a file aaaaa aswn 33332 aasdxe sssee 627627 qqhksd dljlljkl 38682682 so have a output 3332 627627 38682682 (2 Replies)
Discussion started by: cdfd123
2 Replies

10. UNIX for Dummies Questions & Answers

Print last 4 columns (variable column #)

I have rows of data with variable number of columns. Is there an easy way to print out the last 4 columns or rather the 4th and 3rd last column? data looks like this: 24 20:51 N 9 10.00 Overcast OVC110 27 11 30.04 1017.7 24 19:51 N 7 10.00 Mostly Cloudy BKN110 28 15... (19 Replies)
Discussion started by: Da_Duck
19 Replies
Login or Register to Ask a Question