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 ??
# 8  
Old 09-18-2006
Jambesh try this one.....here jam1.txt is lookup file and jam2.txt is data file needs to be rearranged.you need to create temp directory in directory where you will run this script.May be this will help...and jamt will be the output file
Code:
maxcol=`sed '1q' jam1.txt | awk -F" " '{print NF}'`
col_look=`sed '1q' jam1.txt`
col_seq=`sed '1q' jam2.txt`
set -A look $col_look
set -A seq $col_seq
set -A pos

i=0
while [ $i -lt $maxcol ]
  do
   j=0
   while [ $j -lt $maxcol ]
   do
   [[ "${look[$i]}" = "${seq[$j]}" ]] && pos[$i]=`expr $j + 1` 
   j=`expr $j + 1`
  done
i=`expr $i + 1`
done

k=0
while [ $k -lt $maxcol ]
do
cut -d ' ' -f${pos[$k]} jam2.txt>temp/tempjam${k}  
 k=`expr $k + 1`
done

paste temp/tempjam* >jamt
rm -f temp/tempjam*

p.s.
output file columns will be tab delimeter if you want to make it space delimited replace "paste command with
Code:
paste -d ' ' temp/tempjam* >jamt


Last edited by Dhruva; 09-18-2006 at 03:33 AM..
# 9  
Old 09-18-2006
Dhruv,

Here is my jam2.txt file

cat jam2.txt
MANDT SERAIL EQTYP SERSCHA SEREX BSTVP
510 hsgdfs 44 sercha sex1 bst233
510 bg 89 fg 23 98
510 gh 89 we sew mn

jam1.txt contain look up column

MANDT SERAIL SERSCHA SEREX EQTYP BSTVP

when i run your script it produce jamt but upon cat the file
i got the out put in a garbled form not readable

out put of jam1.txt should be jampt and it should contain
MANDT SERAIL SERSCHA SEREX EQTYP BSTVP
510 hsgdfs
510 bg
510 gh

this way ...but i got the result like ...

MANDT SERAIL EQTYP SERSCHA SEREX BSTVP MANDT SERAIL EQTYP SERSCHA SEREX BSTVP MANDT SERAIL EQTYP SERSCHA SEREX BSTVP MANDT SERAIL EQTYP SERSCHA SEREX BSTVP MANDT SERAIL EQTYP SERSCHA SEREX BSTVP MANDT SERAIL EQTYP SERSCHA SEREX BSTVP
510 hsgdfs 44 sercha sex1 bst233 510 hsgdfs 44 sercha sex1 bst233 510 hsgdfs 44 sercha sex1 bst233 510 hsgdfs 44 sercha sex1 bst233 510 hsgdfs 44 sercha sex1 bst233 510 hsgdfs 44 sercha sex1 bst233
510 bg 89 fg 23 98 510 bg 89 fg 23 98 510 bg 89 fg 23 98 510 bg 89 fg 23 98 510 bg 89 fg 23 98 510 bg 89 fg 23 98
510 gh 89 we sew mn 510 gh 89 we sew mn 510 gh 89 we sew mn 510 gh 89 we sew mn 510 gh 89 we sew mn 510 gh 89 we sew mn

unreadable ------please assist me asap im in stuck last two days
# 10  
Old 09-18-2006
f1=${ccseq[0]};f2=${ccseq[1]};f3=${ccseq[2]};f4=${ccseq[3]};f5=${ccseq[4]};f6=${ccseq[5]}

awk -v c1=$f1 -v c2=$f2 -v c3=$f3 -v c4=$f4 -v c5=$f5 -v c6=$f6 'BEGIN{OFS=FS="\t"} {print $c1,$c2,$c3,$c4,$c5,$c6}' ${fname}

what is the problem with this two line ??? i could not get ? any idea to correct it so that it will print the desired column ????
# 11  
Old 09-18-2006
i have checked with the below files and found output file jamt.Please see i had updated my post with paste command.you remove rm command in script and check what is going into tempjam files.
jam1.txt:::
Code:
col1 col2 col3 col4
11 22 33 44
111 222 333 444

jam2.txt::::
Code:
col3 col4 col2 col1
33 44 22 11
333 444 222 111
3 4 2 1

jamt::::
Code:
col1 col2 col3 col4
11 22 33 44
111 222 333 444
1 2 3 4

# 12  
Old 09-18-2006
Dhruv ,
Here is sample out put of each of temjam0 , tempjam1 tempjam2,....
tempjam0 tempjam1 tempjam2 tempjam3 tempjam4 tempjam5
$ cat tempjam0
MANDT SERAIL EQTYP SERSCHA SEREX BSTVP
510 hsgdfs 44 sercha sex1 bst233
510 bg 89 fg 23 98
510 gh 89 we sew mn

$ cat tempjam1
MANDT SERAIL EQTYP SERSCHA SEREX BSTVP
510 hsgdfs 44 sercha sex1 bst233
510 bg 89 fg 23 98
510 gh 89 we sew mn

$ cat tempjam2
MANDT SERAIL EQTYP SERSCHA SEREX BSTVP
510 hsgdfs 44 sercha sex1 bst233
510 bg 89 fg 23 98
510 gh 89 we sew mn

$ cat tempjam3
MANDT SERAIL EQTYP SERSCHA SEREX BSTVP
510 hsgdfs 44 sercha sex1 bst233
510 bg 89 fg 23 98
510 gh 89 we sew mn

$ cat tempjam4
MANDT SERAIL EQTYP SERSCHA SEREX BSTVP
510 hsgdfs 44 sercha sex1 bst233
510 bg 89 fg 23 98
510 gh 89 we sew mn

$ cat tempjam5
MANDT SERAIL EQTYP SERSCHA SEREX BSTVP
510 hsgdfs 44 sercha sex1 bst233
510 bg 89 fg 23 98
510 gh 89 we sew mn
sequence is not changing in any of the file ...what is wrong ?
i guess each of the file should produce only one column ..right ?? but it is containing the whole file again surprise !!!!!!!!!!!!!!
# 13  
Old 09-18-2006
yes only one column in each file.problem here is with cut command.
run the below commands on command line one by one and check what is going in files.The field delimeter is single space.
Make sure there is a single space in ' ' (single quotes in below command)

Code:
cut -d ' ' -f1 jam2.txt>temp/tempjam0
cut -d ' ' -f2 jam2.txt>temp/tempjam1
cut -d ' ' -f3 jam2.txt>temp/tempjam2

# 14  
Old 09-18-2006
dhruv,
You are cutting data using cut -d ' ' ??? it is a space delimeter but i think it should be tab delimited ??
is it the problem please assist me.
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