Print column postion


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print column postion
# 1  
Old 03-15-2013
Print column postion

Hi,

I am using the below code for printing the column position of a particular string

Code:
nawk -v srch="DName" -F '\t' '{for(j=1; j<=NF; j++) { if($j==srch) print j } exit }' input_file.txt

The above code prints the column position of the input string DName (that is 1)

Now the problem is, the above code is working fine for the data that starts from 1st line in a tab delimited file, but it is not working for files where the data is starting from 2 or 3 rd row.

Can anybody plz help.

Thanks

Last edited by Scrutinizer; 03-16-2013 at 05:46 AM.. Reason: quote -> code tags
# 2  
Old 03-15-2013
Please provide representative examples how your input file can look like
# 3  
Old 03-15-2013
Code:
$ awk 'match ($0, srch) {print NR, RSTART; exit}' srch="DName" file

# 4  
Old 03-15-2013
There is no need of exit here!!!

exit makes the command to stop processing after the first row.
# 5  
Old 03-15-2013
Hi,

its working for the below code
Code:
$ awk 'match ($0, srch) {print NR, RSTART; exit}' srch="DName" file

The sample data of my input file looks like:
ACCENT_INPUT
Code:
Pcode    Dealer Name    Comp    INF    Date    Year    
TN16 9BE    Ambrose Fisher Ltd    Franklin UK Mid Cap Fund A Inc GBP    GB00B3ZGH246    5/31/2012    2012  (starts from 2nd row
TN16 9GF    Baggette and Company Wealth Management Limited    Templeton Global Bond A Mdis GBP H1    LU0316492692    5/31/2012    2012

Thanks

---------- Post updated at 08:45 AM ---------- Previous update was at 08:29 AM ----------

in the same way, plz let me know if it is possible to insert a dummy column (column has a header but no data) in nth position?

Thanks

---------- Post updated at 08:48 AM ---------- Previous update was at 08:45 AM ----------

The below code works for data starting from 1st row
Code:
nawk -v p="2" -v col="Dummy" ' {BEGIN { FS=OFS="\t" } { NR==$0?$p=col"\t"$p:$p="\t"$p } } 1 ' input_file.txt> temp_file.txt

but not for data starting from 2 or 3 row

Last edited by Scrutinizer; 03-16-2013 at 05:47 AM.. Reason: code tags + changed quote tags to code tags
# 6  
Old 03-15-2013
You need a criteria to determine at which line your data start.

Since we do not know how your input files can look like (we only have few examples) we can't guess which generic rule should be chosen so that it would work for all the different input you can have.

Could we assume that your data always start at the first lines beginning with "TN" ?
# 7  
Old 03-15-2013
There is no particular pattern for the data, it could be in any format and start at any row.
Is it possible to get the line number where the column header starts, atleast

Thanks

---------- Post updated at 09:26 AM ---------- Previous update was at 09:22 AM ----------

how about this?

Code:
 nawk 'NR>1{print NR; exit}' input_file.txt

but the result is 2, whereas the data is starting from line 3

thanks

Moderator's Comments:
Mod Comment Please use code tags, not quote tags for code and data

Last edited by Scrutinizer; 03-16-2013 at 05:48 AM.. Reason: quote tags to code tags
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

Help with compare two column and print out column with smallest number

Input file : 5 20 500 2 20 41 41 0 23 1 Desired output : 5 2 20 0 1 By comparing column 1 and 2 in each line, I hope can print out the column with smallest number. I did try the following code, but it don't look good :( (2 Replies)
Discussion started by: perl_beginner
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

Print every 5 4th column values as separate row with different first column

Hi, I have the following file, chr1 100 200 20 chr1 201 300 22 chr1 220 345 23 chr1 230 456 33.5 chr1 243 567 90 chr1 345 600 20 chr1 430 619 21.78 chr1 870 910 112.3 chr1 914 920 12 chr1 930 999 13 My output would be peak1 20 22 23 33.5 90 peak2 20 21.78 112.3 12 13 Here the... (3 Replies)
Discussion started by: jacobs.smith
3 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. Shell Programming and Scripting

comparing column of two different files and print the column from in order of 2nd file

Hi friends, My file is like: Second file is : I need to print the rows present in file one, but in order present in second file....I used while read gh;do awk ' $1=="' $gh'" {print >> FILENAME"output"} ' cat listoffirstfile done < secondfile but the output I am... (14 Replies)
Discussion started by: CAch
14 Replies

7. Shell Programming and Scripting

print first few lines, then apply regex on a specific column to print results.

abc.dat tty cpu tin tout us sy wt id 0 0 7 3 19 71 extended device statistics r/s w/s kr/s kw/s wait actv wsvc_t asvc_t %w %b device 0.0 133.2 0.0 682.9 0.0 1.0 0.0 7.2 0 79 c1t0d0 0.2 180.4 0.1 5471.2 3.0 2.8 16.4 15.6 15 52 aaaaaa1-xx I want to skip first 5 line... (4 Replies)
Discussion started by: kchinnam
4 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

Question about sort specific column and print other column at the same time !

Hi, This is my input file: ali 5 usa abc abu 4 uk bca alan 6 brazil bac pinky 10 utah sdc My desired output: pinky 10 utah sdc alan 6 brazil bac ali 5 usa abc abu 4 uk bca Based on the column two, I want to do the descending order and print out other related column at the... (3 Replies)
Discussion started by: patrick87
3 Replies

10. Shell Programming and Scripting

VI editor,column postion

In VI editor ctrl + g is used indicate the line number on which the cursor is placed...similarly is there a way to determine the column number of the cursor position..? (1 Reply)
Discussion started by: vijay_0209
1 Replies
Login or Register to Ask a Question