Re-order columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Re-order columns
# 1  
Old 12-10-2014
Re-order columns

I am trying to reorder columns based on a value in a field in the attached file. Thank you Smilie.


Code:
 If $11 is not equal to "unknown", then in a new text file $4, $11, $7, $13

For example, row 24 $11 is not unknown

A_16_P39089234, chr10:76765556-76765615, KAT6B, 0.9337
# 2  
Old 12-10-2014
What have you tried?
# 3  
Old 12-10-2014
Code:
 awk 'BEGIN{OFS=FS=","}NR==FNR{a[ $11 <> "unknown"];next} in a{print $4,$11,$7,$13}' test.txt > newfile.txt


Thank you Smilie.

Last edited by cmccabe; 12-10-2014 at 06:48 PM..
# 4  
Old 12-10-2014
Try:
Code:
awk 'BEGIN{OFS=FS=","}$11 != "unknown"{print $4,$11,$7,$13}' test.txt > newfile.txt

# 5  
Old 12-11-2014
I have attached the input file (file.txt) as well as the output (newfile.txt).

Code:
 awk 'BEGIN{OFS=FS=","}$11 != "unknown"{print $4,$3,$7,$13}' file.txt > newfile.txt

Code:
 The output doesn't look correct and I'm not sure why.  If $11 = "unknown" then that entire record doesn't need to be copied to the newfile.txt 

For example, row 24 $11 is not unknown, so the record is copied to newfile.txt this way:

A_16_P39089234, chr10:76765556-76765615, KAT6B, 0.9337

In row 2 $11 equals "unknown", so it is not copied to newfile.txt.  The starting spreadsheet is excel so there is a header row in it that can be removed.

Thank you Smilie.
# 6  
Old 12-11-2014
Please be aware that your spec changes and is sloppily phrased. In post #1, you want fields $4, $11, $7, $13 to be printed, in #5 it's $4,$3,$7,$13. The input file is <TAB> delimited; why do you use FS="," ? And, the pattern to be matched is "Unknown", not "unknown".
Do you really want answers created with the same "care" that you put into your request?

However, try
Code:
 awk '$11 != "Unknown"  && $11 != "" {print $4,$3,$7,$13}' FS="\t" OFS="," file.txt

This by no means comes near to what you attached as a sample output file, so I hope that I inferred well enough what you requested...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Evaluate 2 columns, add sum IF two columns satisfy the condition

HI All, I'm embedding SQL query in Script which gives following output: Assignee Group Total ABC Group1 17 PQR Group2 5 PQR Group3 6 XYZ Group1 10 XYZ Group3 5 I have saved the above output in a file. How do i sum up the contents of this output so as to get following output: ... (4 Replies)
Discussion started by: Khushbu
4 Replies

2. Shell Programming and Scripting

Compare 2 csv files by columns, then extract certain columns of matcing rows

Hi all, I'm pretty much a newbie to UNIX. I would appreciate any help with UNIX coding on comparing two large csv files (greater than 10 GB in size), and output a file with matching columns. I want to compare file1 and file2 by 'id' and 'chain' columns, then extract exact matching rows'... (5 Replies)
Discussion started by: bkane3
5 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. Shell Programming and Scripting

Evaluate 2 columns, add sum IF two columns match on two rows

Hi all, I know this sounds suspiciously like a homework course; but, it is not. My goal is to take a file, and match my "ID" column to the "Date" column, if those conditions are true, add the total number of minutes worked and place it in this file, while not printing the original rows that I... (6 Replies)
Discussion started by: mtucker6784
6 Replies

5. Shell Programming and Scripting

Deleting all the fields(columns) from a .csv file if all rows in that columns are blanks

Hi Friends, I have come across some files where some of the columns don not have data. Key, Data1,Data2,Data3,Data4,Data5 A,5,6,,10,, A,3,4,,3,, B,1,,4,5,, B,2,,3,4,, If we see the above data on Data5 column do not have any row got filled. So remove only that column(Here Data5) and... (4 Replies)
Discussion started by: ks_reddy
4 Replies

6. Shell Programming and Scripting

Combine columns from many files but keep them aligned in columns-shorter left column issue

Hello everyone, I searched the forum looking for answers to this but I could not pinpoint exactly what I need as I keep having trouble. I have many files each having two columns and hundreds of rows. first column is a string (can have many words) and the second column is a number.The files are... (5 Replies)
Discussion started by: isildur1234
5 Replies

7. UNIX for Advanced & Expert Users

Help in Deleting columns and Renaming Mutliple columns in a .Csv File

Hi All, i have a .Csv file in the below format startTime, endTime, delta, gName, rName, rNumber, m2239max, m2239min, m2239avg, m100016509avg, m100019240max, metric3min, m100019240avg, propValues 11-Mar-2012 00:00:00, 11-Mar-2012 00:05:00, 300.0, vma3550a, a-1_CPU Index<1>, 200237463, 0.0,... (9 Replies)
Discussion started by: mahi_mayu069
9 Replies

8. Shell Programming and Scripting

Single command for add 2 columns and remove 2 columns in unix/performance tuning

Hi all, I have created a script which adding two columns and removing two columns for all files. Filename: Cust_information_1200_201010.txt Source Data: "1","Cust information","123","106001","street","1-203 high street" "1","Cust information","124","105001","street","1-203 high street" ... (0 Replies)
Discussion started by: onesuri
0 Replies

9. Shell Programming and Scripting

Change many columns position/order

Hi everyone, Please some help over here. (I´m using cygwing) I have files with 40 columns and 2000 lines in average. I´m trying to change the order position as follow. Original columns position:... (3 Replies)
Discussion started by: cgkmal
3 Replies

10. UNIX for Dummies Questions & Answers

Changing the order of columns in a script

I have the following script: (echo -n `curl http://www.example.com/scores.txt | grep mylocation`; date +%Y%m%d%H%M%S) >> myscores.txt This script works fine, except that it places the timestamp at the end of the file myscores.txt. How do add the timestamp as the first column and then a tab and... (4 Replies)
Discussion started by: figaro
4 Replies
Login or Register to Ask a Question