Solved

Change order of data columns

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

How To Reduce Deployment Times With Pre-Baked AMIs

Even if we can't include all the files in the base image, we can sometimes include some of the larger files that we would otherwise have to download, and we can also sometimes remove the most time-consuming steps. This can help a lot with reducing deployment times.

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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

624 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