Horizontal sorting of lines in a File: SED implementation?


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Horizontal sorting of lines in a File: SED implementation?
# 1  
Old 03-09-2007
Horizontal sorting of lines in a File: SED implementation?

Hi guys,

Do you know how can I sort a file horizontally per line???

sample input file:

zebra papa dog apple
yahoo kangaroo ape


sample output:

apple dog papa zebra
ape kangaroo yahoo

Please post if u know the sed implementation of this (or whatever implementation u may know of)

Thanks in advance!!! Smilie
# 2  
Old 03-09-2007
Code:
while read str
do
 echo $str | tr " " "\n" | sort | tr "\n" " "
 echo
done < file

or
Code:
perl -ane ' $,=" "; print sort @F; print "\n" ; ' file

# 3  
Old 03-09-2007
if you have Python, here's an alternative:
Code:
#!/usr/bin/python
for line in open("file"):
  print ' '.join(sorted(line.split()))

output:
Code:
apple dog papa zebra
ape kangaroo yahoo

# 4  
Old 03-09-2007
Nice!!

Wohoo! Nice one! thanks!
# 5  
Old 03-09-2007
Just an another try just using awk...

awk -F" " ' {
s=""
for(i=1; i<=NF; i++) { a[i]=$i; }
for(i=1; i<=NF; i++)
{
for(j = i+1; j<=NF; j++)
{
if (a[i] >= a[j])
{
temp = a[j];
a[j] = a[i];
a[i] = temp;
}
}
}
for(i=1; i<=NF; i++){ s = s" "a[i]; }
print s
}
' filename
# 6  
Old 03-09-2007
And in ksh:
Code:
#! /usr/bin/ksh
while read line ; do
        set -s +A  words $line
        echo ${words[*]}
done
exit 0

This User Gave Thanks to Perderabo For This Post:
# 7  
Old 03-15-2007
Quote:
Originally Posted by Perderabo
And in ksh:
Code:
#! /usr/bin/ksh
while read line ; do
        set -s +A  words $line
        echo ${words[*]}
done
exit 0

Good solution.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Convert a horizontal lines to vertical lines in a csv file

Hi.. I need some help in converting the below horizontal lines to vertical lines format. can anyone help me on this. input file Hour,1,2,3,4,5 90RT,106,111,111,112,111 output file Hour,90RT 1,106 2,111 3,111 4,112 5,111 (3 Replies)
Discussion started by: Raghuram717
3 Replies

2. Shell Programming and Scripting

Trying to take file numbers from a file, pass them to sed to change strings in corresponding lines

I have a bunch of file numbers in the file 'test': I'm trying the above command to change all the instances of "H" to "Na+" in the file testsds.pdb at the line numbers indicated in the file 'test'. I've tried the following and various similar alternatives but nothing is working: cat test |... (3 Replies)
Discussion started by: crunchgargoyle
3 Replies

3. UNIX for Dummies Questions & Answers

Deleting last 3 lines from a file via sed

Hi Anybody can help me to delete the last 3 lines from a text file via sed under SunOS 5.8? Thanks Aldar (4 Replies)
Discussion started by: aldar
4 Replies

4. Shell Programming and Scripting

Need perl or shell script to sort vertical lines to horizontal line in csv format

Need perl or shell script to sort vertical lines to horizontal line in csv format My file like below ------------------------- ================================================================================ PATH PINKY1000#I1-1-ZENTA1000-2#I7-1-ASON-SBR-UP-943113845 ... (4 Replies)
Discussion started by: sreedhargouda.h
4 Replies

5. Shell Programming and Scripting

change log vertical to horizontal lines

Hi, Need help unix command to change this : become this Anyone can help me?:wall: (2 Replies)
Discussion started by: justbow
2 Replies

6. Shell Programming and Scripting

Need help in changing vertical lines to horizontal line in a file

Hi, I have a file like below robert PREF: 3 AVAIL: henry PREF: 234 AVAIL: john PREF: 145,178 AVAIL: 123 matt PREF: 564,932 AVAIL: ten PREF: 389 AVAIL: kill (2 Replies)
Discussion started by: rocky1954
2 Replies

7. Shell Programming and Scripting

CSV file horizontal formatting

Hi folks, Please help me with csv file formatting which needs to be done in horizontal fashion. I have data coming in separate files every hour. What I need to do is extract three values into csv file. In the next hour, I need to extract the new values beside the three values which were... (1 Reply)
Discussion started by: vharsha
1 Replies

8. Linux

Horizontal Lines

Jan 18, 2010 14:15:31 GMT Hello, I get horizontal black lines after each line of text and every blank line is colored black. I am using HP Color LaserJet... (5 Replies)
Discussion started by: H_P
5 Replies

9. Programming

implementation of all sorting algorithms using fork

im new to programming c in unix this is program written by me i want each and every child to do a seperate work such implement a different sorting algorithm but im not getting the way i wrote as below...plz help me how can i do that #include<stdio.h> main() { int i; for(i=0;i<5;i++)... (1 Reply)
Discussion started by: chaitu07
1 Replies

10. Shell Programming and Scripting

sorting lines

i have another question.. i have this file: John Jones John Smith Jack Smith 100100 13 Helen Bold 100200 5 how can i use sort to make it look like : Helen Bold John Jones Jack Smith John Smith 100100 13 100200 5 (4 Replies)
Discussion started by: kion
4 Replies
Login or Register to Ask a Question