Sorting row in a giving file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sorting row in a giving file
# 1  
Old 12-08-2011
Sorting row in a giving file

hi I would like to sort rows in a givin file except the first colomn (or first element of a row) just like the following example

Code:
file input : 

1  3 8 5
2  2 8 1
3  9 8 10

Code:
file output : 

1  8 5 3
2  8 2 1
3  10 9 8

thanks in advance
# 2  
Old 12-08-2011
Code:
perl -ne '@x=split/\s+/,$_; print shift @x; print "  "; @x=reverse sort{$a<=>$b}@x; print "@x\n"' input.txt;

output:
Code:
1  8 5 3
2  8 2 1
3  10 9 8

# 3  
Old 12-08-2011
Try...
Code:
while read a b; do printf '%s  ' $a; echo $b | tr ' ' '\n' | sort -nr | paste -s -d ' '; done < file1

# 4  
Old 12-08-2011
thanks a loooooooot

---------- Post updated at 12:32 AM ---------- Previous update was at 12:09 AM ----------

thanks ygor

last thing
if I want to store the first ans second column in a file, first and 3th column in a second file ans finally first and 4th column in a 3th file how can i do this
# 5  
Old 12-08-2011
Not really a one-liner anymore. Same code as before, but with a tee to awk...
Code:
while read a b
do
    printf '%s  ' $a
    echo $b | tr ' ' '\n' | sort -nr | paste -s -d ' '
done < file1 | tee outfile1 |\
    awk '{for(x=2;x<=NF;x++) print $1, $x > "outfile" x}'

Which gives...
Code:
==> outfile1 <==
1  8 5 3
2  8 2 1
3  10 9 8

==> outfile2 <==
1 8
2 8
3 10

==> outfile3 <==
1 5
2 2
3 9

==> outfile4 <==
1 3
2 1
3 8

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search row by row from one file to another file if match is found print few colums of file 2

this is the requirement list.txt table1 table2 table3 testfile.txt name#place#data#select * from table1 name2#place2#data2#select * from table 10 innerjoin table3 name2#place2#data2#select * from table 10 output name place table1 name2 place table3 i tried using awk (7 Replies)
Discussion started by: vamsekumar
7 Replies

2. Shell Programming and Scripting

Read row number from 1 file and print that row of second file

Hi. How can I read row number from one file and print that corresponding record present at that row in another file. eg file1 1 3 5 7 9 file2 11111 22222 33333 44444 55555 66666 77777 88888 99999 (3 Replies)
Discussion started by: Abhiraj Singh
3 Replies

3. UNIX for Dummies Questions & Answers

awk to print first row with forth column and last row with fifth column in each file

file with this content awk 'NR==1 {print $4} && NR==2 {print $5}' file The error is shown with syntax error; what can be done (4 Replies)
Discussion started by: cdfd123
4 Replies

4. Shell Programming and Scripting

Replace Second row with First row in a File

Hi, I need to replace first row to second row: Input: A JACK WAS_${HH}_JACK .... Output should be: JACK WAS_A_JACK .... I tried below code.. awk '{TEMP= (NR==1)}; {sub(/${HH}/$TEMP/)}' (2 Replies)
Discussion started by: kmsekhar
2 Replies

5. Shell Programming and Scripting

Subtracting each row from the first row in a single column file using awk

Hi Friends, I have a single column data like below. 1 2 3 4 5 I need the output like below. 0 1 2 3 4 where each row (including first row) subtracting from first row and the result should print below like the way shown in output file. Thanks Sid (11 Replies)
Discussion started by: ks_reddy
11 Replies

6. Shell Programming and Scripting

Get a group of line from different file and put them into one file row by row

hi guys, today i'm stuck in a new problem. the title explain more or less but a particular had been omitted. So i'm going to describe below the situation with an example. I have different html files and each of them have a consecutive lines group inside that i want to extract. example: ... (8 Replies)
Discussion started by: sbobotex
8 Replies

7. Shell Programming and Scripting

Append Second Row to First Row @ end in a file

Hi I want to append line 2n to 2n-1 line where n=1...LastLine in my file. eg: Actual File: Hello Everyone Welcome TO Unix I need Your help Required File: HelloEveryone WelcomeTO Unix I needYour help (5 Replies)
Discussion started by: dashing201
5 Replies

8. Shell Programming and Scripting

Transposing column to row, joining with another file, then sorting columns

Hello! I am very new to Linux and I do not know where to begin... I have a column with >64,000 elements (that are not in numberical order) like this: name 2 5 9 . . . 64,000 I would like to transpose this column into a row that will later become the header of a very large file... (2 Replies)
Discussion started by: doobedoo
2 Replies

9. Shell Programming and Scripting

Giving input to a c++ file

My C++ program creates a nxn matrix with given value. For e.g if the input is 10 it will creates a matrix of 10x10 now what i want is the script should run program and give input values in a variation of 1000. Say first matrix of 1000 then 2000 , 3000 ..... 10000. I tried using for loop but unable... (2 Replies)
Discussion started by: tonyaim83
2 Replies

10. Shell Programming and Scripting

Changing the column for a row in a text file and adding another row

Hi, I want to write a shell script which increments a particular column in a row from a text file and then adds another row below the current row with the incremented value . For Eg . if the input file has a row : abc xyz lmn 89 lm nk o p I would like the script to create something like... (9 Replies)
Discussion started by: aYankeeFan
9 Replies
Login or Register to Ask a Question