copying columns with headers' specific pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting copying columns with headers' specific pattern
# 8  
Old 06-01-2010
Quote:
Originally Posted by jacks
it keeps selecting all columns.
Hi, could you indicate where your input file differs from my test sample?

Code:
$ cat infile
*sml1   *sml3   *smln7  sm*fk9  smllf56 *tr     yty
1       2       3       4       5       6       7
a       b       c       d       e       f       g

Code:
$ awk '{for (i=1;i<=NF;i++) {if (NR==1 && $i~/^*/) A[i]; if (i in A) printf $i"\t"} print ""}' infile
*sml1   *sml3   *smln7  *tr
1       2       3       6
a       b       c       f



---------- Post updated at 13:15 ---------- Previous update was at 13:13 ----------

I tried it with your newly supplied input and I get this:
Code:
$ awk '{for (i=1;i<=NF;i++) {if (NR==1 && $i~/^*/) A[i]; if (i in A) printf $i"\t"} print ""}' infile2
*smt_0018       *smlf_0042      *sugk_0053
11.2902 10.7395 7.9672
8.98292 9.69812 7.7105
7.71334 8.05211 7.57295
9.07925 8.96038 8.1141
10.1289 9.17119 7.52616
11.5821 8.76748 8.19344
9.18122 10.1511 13.4099

# 9  
Old 06-01-2010
This is my output with your file:
Code:
$ cat file
*smt_0018 smf_0031 *smlf_0042 *sugk_0053
11.2902 12.4807 10.7395 7.9672
8.98292 9.03125 9.69812 7.7105
7.71334 8.28308 8.05211 7.57295
9.07925 9.17628 8.96038 8.1141
10.1289 8.9778 9.17119 7.52616
11.5821 9.54838 8.76748 8.19344
9.18122 9.71467 10.1511 13.4099
$
$ tr ' ' '\n' < file | grep '^\*'                             
*smt_0018
*smlf_0042
*sugk_0053

# 10  
Old 06-01-2010
Hi,
I did not understand how it differs.

$ cat sample
*sml_0018 *smmt_0031 sdmt_0042 *srmt_0053
1 1 1 1
11.2902 12.4807 10.7395 7.9672
8.98292 9.03125 9.69812 7.7105
7.71334 8.28308 8.05211 7.57295
9.07925 9.17628 8.96038 8.1141
10.1289 8.9778 9.17119 7.52616
11.5821 9.54838 8.76748 8.19344
9.18122 9.71467 10.1511 13.4099



$ awk '{for (i=1;i<=NF;i++) {if (NR==1 && $i~/^*/) A[i]; if (i in A) printf $i"\t"} print ""}' sample
*sml_0018 *smmt_0031 sdmt_0042 *srmt_0053
1 1 1 1
11.2902 12.4807 10.7395 7.9672
8.98292 9.03125 9.69812 7.7105
7.71334 8.28308 8.05211 7.57295
9.07925 9.17628 8.96038 8.1141
10.1289 8.9778 9.17119 7.52616
11.5821 9.54838 8.76748 8.19344
9.18122 9.71467 10.1511 13.4099

Last edited by jacks; 06-01-2010 at 08:28 AM..
# 11  
Old 06-01-2010
Using your latest sample I get:
Code:
$ awk '{for (i=1;i<=NF;i++) {if (NR==1 && $i~/^*/) A[i]; if (i in A) printf $i"\t"} print ""}' sample
*sml_0018       *smmt_0031      *srmt_0053
1       1       1
11.2902 12.4807 7.9672
8.98292 9.03125 7.7105
7.71334 8.28308 7.57295
9.07925 9.17628 8.1141
10.1289 8.9778  7.52616
11.5821 9.54838 8.19344
9.18122 9.71467 13.4099

What do you get if you escape the *:
Code:
awk '{for (i=1;i<=NF;i++) {if (NR==1 && $i~/^\*/) A[i]; if (i in A) printf $i"\t"} print ""}' infile

Are you on Solaris? Then you need to use nawk or /usr/xpg4/bin/awk
This User Gave Thanks to Scrutinizer For This Post:
# 12  
Old 06-01-2010
Oh my, didn't realize it's a tab delimited file, try:
Code:
tr '\t' '\n' < file | grep '^\*' > newfile

Please use code tags.
These 2 Users Gave Thanks to Franklin52 For This Post:
# 13  
Old 06-01-2010
Hi Scrutinizer,

This works nice :

awk '{for (i=1;i<=NF;i++) {if (NR==1 && $i~/^\*/) A[i]; if (i in A) printf $i"\t"} print ""}' infile

Can you please tell me why it worked this time ?


Thanks to you and Franklin.
# 14  
Old 06-01-2010
Hi Jacks,

Glad we found the culprit. * is a wildcard character (it repeats the preceding character). If one needs to use it literally (to match the character "*" it would typically need to be escaped by a backslash character. However since in this particular the * is first character the pattern it could not possibly be a wildcard and hence it should not need an escape. But not every implementation of awk sees it that way apparantly.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copying specific file types to specific folders

I am trying to write a script that cycles through a folder containing many folders and when inside each one it's supposed to copy all the .fna.gz files to a folder elsewhere if the file and the respective folder have the same name. for fldr in /home/playground/genomes/* ; do find .... (8 Replies)
Discussion started by: Mr_Keystrokes
8 Replies

2. Shell Programming and Scripting

Copying files with a specific pattern

Hi All I am trying to copy files from one location to another and given below are some sample ones: aaa_bbb_ccc_ddd_cost_code_20140330.gz aaa_bbb_ccc_ddd_revenue_zone_20140329.gz aaa_bbb_ccc_ddd_benefit_extract_20140330.csv.gz aaa_bbb_ccc_ddd_profit_zone_20150509.csv.gz... (17 Replies)
Discussion started by: swasid
17 Replies

3. Shell Programming and Scripting

CSv2dat file headers and columns order

Dear all, I have a csv file which is transformed to .dat. I have an awk file which is supposing to do the mapping of the dat file. the code from the awk file is the one below. The content of the dat file is looking like this (tab separated): ODT AGE CDT CO SEX TIME ... (9 Replies)
Discussion started by: grikoss
9 Replies

4. UNIX for Dummies Questions & Answers

Printing lines with specific strings at specific columns

Hi I have a file which is tab-delimited. Now, I'd like to print the lines which have "chr6" string in both first and second columns. Could anybody help? (3 Replies)
Discussion started by: a_bahreini
3 Replies

5. Shell Programming and Scripting

Can't figure out how to find specific characters in specific columns

I am trying to find a specific set of characters in a long file. I only want to find the characters in column 265 for 4 bytes. Is there a search for that? I tried cut but couldn't get it to work. Ex. I want to find '9999' in column 265 for 4 bytes. If it is in there, I want it to print... (12 Replies)
Discussion started by: Drenhead
12 Replies

6. Shell Programming and Scripting

Transpose multipe columns to rows and adding headers

Hi, I found the following awk script to transpose multiple (3) columns to multiple rows: #=== BEGIN {FS=","} { for (i=1;i<=NF;i++) { arr=$i; if(nf<= NF) nf=NF; } nr=NR } END { for(i=1;i<=nf;i++) { (8 Replies)
Discussion started by: Gery
8 Replies

7. Shell Programming and Scripting

Merging of files with different headers to make combined headers file

Hi , I have a typical situation. I have 4 files and with different headers (number of headers is varible ). I need to make such a merged file which will have headers combined from all files (comman coluns should appear once only). For example - File 1 H1|H2|H3|H4 11|12|13|14 21|22|23|23... (1 Reply)
Discussion started by: marut_ashu
1 Replies

8. Shell Programming and Scripting

merge columns into one line after a specific pattern

Hi all, im a linux newbie, plz help! I have a file - box -------- Fox-2 -------- UF29 zip42 -------- zf-CW SNF2_N Heli_Z -------- Fox -------- Kel_1 box (3 Replies)
Discussion started by: sam_2921
3 Replies

9. UNIX for Dummies Questions & Answers

copying a pattern of files in one directory into other with new pattern names...

Hi, I have to copy a set of files abc* in /path/ to /path1/ as abc*_bkp. The list of files appear as follows in /path/: abc1 xyszd abc2 re2345 abcx .. . abcxyz I have to copy them (abc* files only) into /path1/ as: abc1_bkp abc2_bkp abcx_bkp .. . (6 Replies)
Discussion started by: new_learner
6 Replies

10. Shell Programming and Scripting

Copying specific files from remote m/c to specific folders

Hi All, I am trying to rsync some of the latest files from remote m/c to my local linux box. Folder structure in my remote m/c looks like this /pub/Nightly/Package/ROLL/WIN /pub/Nightly/Package/SOLL/sol /pub/Nightly/Package/SOLL/linux Each of the folder contains gzip files which on daily... (0 Replies)
Discussion started by: jhoomsharabi
0 Replies
Login or Register to Ask a Question