Solved

Change order of data columns

Posted on 2004-09-01
3
165 Views
Last Modified: 2011-04-14
Hi all,
I have a file name file_1 contains 4 columns of data as the format below:
          a1   a2   a3   a4
          b1  b2   b3   b4
          -------------------- ( n lines)

This file has few thousand lines of data

I want to make a new file, named file_2, from the file_1 by changing the order of the data column, as the format below
       a3   a1   a2   a4
       b3   b1  b2   b4
      --------------------- (n lines)

I ve tried to use the following code:
data_list=`cut -d" " -f3,1,2,4  file_1`
echo $data_list > file_2

But this is what I have in file_2:

a3   a1   a2   a4   b3   b1  b2   b4  ---------------------

Is there any way to create file_2 with its data format (4 colums of data) as I described above? The solution should not use 'while do' loop since the file_1 is very large in size

Thanks
0
Comment
Question by:jetli
3 Comments
 
LVL 2

Expert Comment

by:sneeuw_chan
ID: 11957108
Using a 'sed' script would probably be easiest.

Let's see how much I remember of sed syntax:

sed -e's/\([^ ]*\) *\([^ ]*\) *\([^ ]*\) *\([^ ]*\)/\3 \1 \2 \4/' file_in > file_out

Hope this helps.
0
 
LVL 11

Assisted Solution

by:avizit
avizit earned 250 total points
ID: 11958827
simpler with awk


awk '{print $3, $1,  $2, $4}'  input_file > output_file

0
 
LVL 23

Accepted Solution

by:
Mysidia earned 250 total points
ID: 11959013
You basically have the right answer.. the backtic operation just munged up
the output

data_list=`cut -d" " -f3,1,2,4  file_1`
It's the ``s that are causing the result to go on one line

just do

cut -f3,1,2,4  file_1 > file_2


0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now