Solved

Change order of data columns

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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 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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

740 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