Changing Sequence of Columns


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Changing Sequence of Columns
# 1  
Old 09-24-2009
Changing Sequence of Columns

Hello,

I might require some help here.

I have a file, ITEMS with the below data :

Code:
A1 20 A2 A3
B1 40 B2 B3
C1 70 C2 C3
D1 90 D2 D3

I need the output as :

Code:
A2 A3 20
B2 B3 40
C2 C3 70
D2 D3 90

i.e, I need the sequence of columns to be changed and only 3 fields to be displayed (Not all 4).

Please help me out. Thanks.

Last edited by vgersh99; 09-24-2009 at 09:21 AM.. Reason: code tags, PLEASE!
# 2  
Old 09-24-2009
To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags [code] and [/code] by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums
# 3  
Old 09-24-2009
Code:
awk '{ print $3,$4,$2 }' file

where file contains your data

Quote:
Originally Posted by sushant172
Hello,

I might require some help here.

I have a file, ITEMS with the below data :

A1 20 A2 A3
B1 40 B2 B3
C1 70 C2 C3
D1 90 D2 D3

I need the output as :

A2 A3 20
B2 B3 40
C2 C3 70
D2 D3 90

i.e, I need the sequence of columns to be changed and only 3 fields to be displayed (Not all 4).

Please help me out. Thanks.
# 4  
Old 09-24-2009
Would there be any commands without using awk and just using the basic cut, paste,sort etc commands?
# 5  
Old 09-25-2009
Quote:
Originally Posted by sushant172
Would there be any commands without using awk and just using the basic cut, paste,sort etc commands?
Just out of curiosity, why not use awk? Seems odd to discard a great tool like that. Of course, if this is a homework assignment where the teacher has limited what you're allowed to use, that's different... we've all been there, I'm sure.
# 6  
Old 09-25-2009
You can use sed:
Code:
 sed 's/\(.\{2\}\) \(.\{2\}\) \(.\{2\}\) \(.\{2\}\)/\3 \4 \2/' file

..or just the shell
Code:
# cat file
A1 20 A2 A3
B1 40 B2 B3
C1 70 C2 C3
D1 90 D2 D3
# while read a b c d;do echo $c $d $b;done < file
A2 A3 20
B2 B3 40
C2 C3 70
D2 D3 90

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Transpose a sequence of symbols from row to one columns (no separators))

Hi all, I have a data file. It contains one header line followed by a new line which is one row of different characters without separators (charahters can be dots, dash, capital and small letters). What I need is ignoring header line to transpose these characters so they form a column. So, from... (4 Replies)
Discussion started by: kush
4 Replies

2. Shell Programming and Scripting

Compare 2 csv files by columns, then extract certain columns of matcing rows

Hi all, I'm pretty much a newbie to UNIX. I would appreciate any help with UNIX coding on comparing two large csv files (greater than 10 GB in size), and output a file with matching columns. I want to compare file1 and file2 by 'id' and 'chain' columns, then extract exact matching rows'... (5 Replies)
Discussion started by: bkane3
5 Replies

3. Shell Programming and Scripting

Changing integer columns to floating decimal columns

I have a document that has 7 columns. eg. $1 $2 $3 $4 $5 $6 $7 string string string string integer integer integer The 6th and 7th columns are a mix of integers and floating decimals (with 4 decimal places). I need to convert the last 2 columns so that all... (3 Replies)
Discussion started by: kadm
3 Replies

4. Shell Programming and Scripting

Combine columns from many files but keep them aligned in columns-shorter left column issue

Hello everyone, I searched the forum looking for answers to this but I could not pinpoint exactly what I need as I keep having trouble. I have many files each having two columns and hundreds of rows. first column is a string (can have many words) and the second column is a number.The files are... (5 Replies)
Discussion started by: isildur1234
5 Replies

5. Shell Programming and Scripting

find common entries and match the number with long sequence and cut that sequence in output

Hi all, I have a file like this ID 3BP5L_HUMAN Reviewed; 393 AA. AC Q7L8J4; Q96FI5; Q9BQH8; Q9C0E3; DT 05-FEB-2008, integrated into UniProtKB/Swiss-Prot. DT 05-JUL-2004, sequence version 1. DT 05-SEP-2012, entry version 71. FT COILED 59 140 ... (1 Reply)
Discussion started by: manigrover
1 Replies

6. Shell Programming and Scripting

Single command for add 2 columns and remove 2 columns in unix/performance tuning

Hi all, I have created a script which adding two columns and removing two columns for all files. Filename: Cust_information_1200_201010.txt Source Data: "1","Cust information","123","106001","street","1-203 high street" "1","Cust information","124","105001","street","1-203 high street" ... (0 Replies)
Discussion started by: onesuri
0 Replies

7. Shell Programming and Scripting

Changing file with rows and columns into just 1 column

Hi I need a bash shell script that will take text files with 4 rows and different numbers of columns in each row and convert each one into a text file with just one column. I then subtract 1.5 from each number in the column (I have that part already) The next step after that is I want to have... (10 Replies)
Discussion started by: ac130pilot
10 Replies

8. Shell Programming and Scripting

Changing the sequence number

Hi, I have a data as follow: 1 400 2 239 3 871 4 219 5 543 6 ... 7 ... .. ... .. ... 99 818 100 991 I want to replace the sequence number (column 1) that start from 150. The output should like this: 150 400 151 239 (3 Replies)
Discussion started by: nica
3 Replies

9. UNIX for Dummies Questions & Answers

Changing the order of columns in a script

I have the following script: (echo -n `curl http://www.example.com/scores.txt | grep mylocation`; date +%Y%m%d%H%M%S) >> myscores.txt This script works fine, except that it places the timestamp at the end of the file myscores.txt. How do add the timestamp as the first column and then a tab and... (4 Replies)
Discussion started by: figaro
4 Replies

10. Shell Programming and Scripting

Changing userID and Changing group and GID

Hello, I want to write a ksh script about changing UID and changing group with GID. There are multiple servers i want to perform that job. linux1 linux2 linux3 linux4 linux5 ...... . . . . . 1.) How can i enter "password" in script rather asking me? I was trying this... ssh... (2 Replies)
Discussion started by: deal732
2 Replies
Login or Register to Ask a Question