Solved

Change order of data columns

Posted on 2004-09-01
3
171 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Trying to install MonoDevelop from sourde on Ubuntu 14.04 trusty 10 629
benchmark write speed to disk on linux 2 77
how does yum update work 2 73
Identify Linux loader 67 190
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 an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.
Suggested Courses

751 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