Reorder column in Unix


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Reorder column in Unix
# 1  
Old 09-22-2012
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 [abc3]
C D [def4]
F G [ghi5]

Now I would like to swap the third column with second column to following format:
A [abc3] B
C [def4] D
F [mip5] G

I know this cannot be done in Unix command with using cut command.
Please advise.
# 2  
Old 09-22-2012
Code:
while read a b c ; do echo "$a\t$c\t$b" ; done < infile


Last edited by rdrtx1; 09-22-2012 at 01:23 PM..
# 3  
Old 09-22-2012
Try with sed:
sed 's/\(\t[^\t]*\)\(\t[^\t]*\)/\2\1/' <file
--
Bye
# 4  
Old 09-22-2012
awk 'BEGIN {FS="\t";OFS="\t"} {print $1, $3, $2}' file
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

UNIX script to compare 3rd column value with first column and display

Hello Team, My source data (INput) is like below EPIC1 router EPIC2 Targetdefinition Exp1 Expres rtr1 Router SQL SrcQual Exp1 Expres rtr1 Router EPIC1 Targetdefinition My output like SQL SrcQual Exp1 Expres Exp1 Expres rtr1 Router rtr1 Router EPIC1 Targetdefinition... (5 Replies)
Discussion started by: sekhar.lsb
5 Replies

2. Shell Programming and Scripting

Reorder of fields

I need to reorder the fields of an input file. I was using the following awk statement but I am stumped on how to get it exactly as I need. I need the output to be tab delimited except for the CITY, NAME and ID. Those last set of columns should be a single field separated by a space. And they... (5 Replies)
Discussion started by: ncwxpanther
5 Replies

3. 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

4. UNIX for Advanced & Expert Users

Copy a column to another column in UNIX fixedwidth file

Hi All, I have a fixedwidth file of length 3000. Now i want to copy a column of 4 chars i.e( length 1678-1681) to column 1127 – 1171 to the same file. Please let me know how can i achive using a single command in fixed width file. Also source column length is 4 chars and target column length... (4 Replies)
Discussion started by: kiranparsha
4 Replies

5. Shell Programming and Scripting

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... (5 Replies)
Discussion started by: smitra
5 Replies

6. Shell Programming and Scripting

Reorder the Cut characters

Hi, I have a fixed width flatfile, I want to view this file specific to it's character position and in order I want to...example as below ABCDE.txt 01COLTSMANNING18 02PATS BRADY 12 03PACKSROGERS 12I used unix cut command to see specific field based on length but unable to order them as... (6 Replies)
Discussion started by: okkadu
6 Replies

7. Shell Programming and Scripting

for each different entry in column 1 extract maximum values from column 2 in unix/awk

Hello, I have 2 columns (1st column has multiple entries but the corresponding values in the column 2 may be the same or different.) however I want to extract unique values for each entry in column 1 by assigning the max value from column 2 SDF4 -0.211654 SDF4 0.978068 ... (1 Reply)
Discussion started by: Diya123
1 Replies

8. Shell Programming and Scripting

reading comma separated data and reorder

hey guys! i need to read data from a file that are comma separated then reorder them in another file to be generated for example: x,y,z a,b,c l,m,n o,p,q and transform this into: x,a,l,o y,b,m,p z,c,n,q Will appreciate your fast reply Regards! (5 Replies)
Discussion started by: maiooi90
5 Replies

9. Shell Programming and Scripting

Bash script to reorder csv

hi guys, im fairly new to unix and bash scripts and therefore your help would really be appreciated. i need to write a bash script that will take a csv file, and reorder the data and output to another csv file. The source csv file will look something like this: HEAD,671061,Add,SS... (3 Replies)
Discussion started by: daz_20
3 Replies

10. Shell Programming and Scripting

Reorder the sequence of line groupings/QIF export

Hi All, I need to reorder the sequence of line groupings - specifically the output from a bank QIF (Quicken Interchange Format) export. Sample is like this: !Type:Bank D12/05/2008 T-10.00 N1 Details of Charge 1 ^ D07/05/2008 T-20.00 N2 Details of Charge 2 ^ D17/04/2008 T-30.00 (0 Replies)
Discussion started by: mark101
0 Replies
Login or Register to Ask a Question