Reorder and insert column using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reorder and insert column using awk
# 1  
Old 08-31-2012
Reorder and insert column using awk

Hi friends,
I am beginner in shell scripting. I am trying to modify (Reorder and insert column) a BIG .txt file using awk to create a .bim file. My start file has 25 columns But I need to make a new file with only 5 columns from the start file and insert a new column in position 3 with value '0'.

To do this first I tried to reorder the columns and create and empty colum at 3rd column, then thought to input zero in it.
But first step didn't work

Code:
awk -F ' ' {printf "%s\t%s\t%s\t%s\t%s\t%s",$15,$2,$0,$16,$4,$5;} <test.txt> test.bim

but always getting error as
Code:
-bash: syntax error near unexpected token `}'

I tried different similar options but nothing worked.

Any help will be really great.
Thanks a lot,
Smitra
# 2  
Old 08-31-2012
you must put the awk code between quotes !

Code:
awk -F ' ' '{printf "%s\t%s\t%s\t%s\t%s\t%s",$15,$2,$0,$16,$4,$5;}' <test.txt> test.bim

This User Gave Thanks to delugeag For This Post:
# 3  
Old 08-31-2012
First of all, you need to quote your script to prevent processing by shell. Also, there is no need for -F' ' as the default FS (field separator) in awk is a space. Also, $0 means the whole input line and not zero. So, your line should be:
Code:
awk '{printf "%s\t%s\t%s\t%s\t%s\t%s",$15,$2,"0",$16,$4,$5;}' <test.txt> test.bim

This User Gave Thanks to elixir_sinari For This Post:
# 4  
Old 08-31-2012
Thanks a lot to both of you for your reply. Actually I don't have any methodical training in this so often makes silly mistakes.
Can you please tell in which cases do I need FS?
Thanks,
Mitra
# 5  
Old 08-31-2012
When your field separator is something other than whitespace (e.g. if you were processing a simple CSV file you would use a comma instead).
This User Gave Thanks to CarloM For This Post:
# 6  
Old 08-31-2012
thanks CarloM Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to insert data into black column( Secound Column ) in excel (.XLSX) file using shell script?

Source Code of the original script is down below please run the script and try to solve this problem this is my data and I want it column wise 2019-03-20 13:00:00:000 2019-03-20 15:00:00:000 1 Operating System LAB 0 1 1 1 1 1 1 1 1 1 0 1 (5 Replies)
Discussion started by: Shubham1182
5 Replies

2. UNIX for Advanced & Expert Users

Insert a column in the beginning

Hi, I have been trying to see how i can insert a column in the beginning to my html table This is how it looks Name Age Sid 32 John 33 Mary 34 I want to insert a column Job before Name column, so it looks like Job Name Age IT Sid 32 Doctor... (3 Replies)
Discussion started by: sidnow
3 Replies

3. Shell Programming and Scripting

How to insert new column with awk?

Hello guys, I'm new to shell programming, I would like to insert two new columns in a space separated file having this format using awk, starting from the third row: A B C D E F 1 ; A B C D E F 1 ; A B C D E F 1 ; A B C D E F 1 ; Basically, the resulting file should have the following... (3 Replies)
Discussion started by: transat
3 Replies

4. Shell Programming and Scripting

awk to reorder lines in file

The output of an awk script is the below file. Line 1,3 that starts with the Ion... need to be under line 2,4 that starts with R_. The awk runs but no output results. Thank you :). file IonXpress_007 MEV37 R_2016_09_20_12_47_36_user_S5-00580-7-Medexome IonXpress_007 MEV40... (6 Replies)
Discussion started by: cmccabe
6 Replies

5. Shell Programming and Scripting

How to insert a column inside a dataset with awk?

Hello folks I have a file called fill1.txt which contains: 1 2 2 1 1 2 1 2 my other file is called fill2.txt which contains: 1 2 1 2 2 2 1 2 1 2 1 1 2 1 1 2 1 1 1 1 2 2 2 1 1 2 2 1 1 2 1 1 1 2 2 2 1 2 2 1 Now, I am looking for a awk command which could insert fill1.txt between... (1 Reply)
Discussion started by: sajmar
1 Replies

6. Shell Programming and Scripting

Insert data in first column(if blank) from previous line first column

Dear Team I need to insert field(which is need to taken from previous line's first field) in first column if its blank. I had tried using sed but not find the way. Detail input and output file as below. Kindly help for same. INPUT: SCGR SC DEV DEV1 NUMDEV DCP ... (7 Replies)
Discussion started by: jaydeep_sadaria
7 Replies

7. UNIX for Dummies Questions & Answers

Reorder column in Unix

I have a flat file with the 3 columns and separate by tab delimiter, and the last column with special character. A B C D F G Now I would like to swap the third column with second column to following format: A B C D F G I know this cannot be done in Unix command with using cut... (3 Replies)
Discussion started by: newbie2011
3 Replies

8. UNIX for Dummies Questions & Answers

insert a column into file

hi everybody! Could u show me how to do following: I have files (in total 22) .txt that contain one column of values (the total number of values varies from file to file from 40.0000 to 10.000). For example: 742429 758311 995669 1008567 1011278 1011521 1020428 1021403 ... And I need... (2 Replies)
Discussion started by: kush
2 Replies

9. Shell Programming and Scripting

Insert a text from a specific row into a specific column using SED or AWK

Hi, I am having trouble converting a text file. I have been working for this whole day now, still i couldn't make it. Here is how the text file looks: _______________________________________________________ DEVICE STATUS INFORMATION FOR LOCATION 1: OPER STATES: Disabled E:Enabled ... (5 Replies)
Discussion started by: Issemael
5 Replies
Login or Register to Ask a Question