Solved

Change order of data columns

Posted on 2004-09-01
3
164 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

707 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

17 Experts available now in Live!

Get 1:1 Help Now