Visit Our UNIX and Linux User Community


Sort items on a single line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sort items on a single line
# 1  
Old 09-16-2009
Sort items on a single line

I am creating some documentation that includes a list of packages that are to be installed for a Debian Server.

This is a single line and I would like to sort the list of packages alphabetically.

Using a small example of the packages, the best I could come up with was as follows:

I create a file with the line in it.

Code:
echo "aspell-en lynx unzip bzip2 p7zip bzip2 gzip rar unrar tar zip unzip bash-completion" > /tmp/unsortedline.txt

I then use this command to try and sort the line.

Code:
tsort  /tmp/unsortedline.txt |sort -d |awk '{ str1=str1 $0 " "}END{ print str1 }'

However if you use my example above which has an odd number of items seperated by spaces you get this error

tsort: /tmp/unsortedline.txt: input contains an odd number of tokens

So I end up just putting another character in the file so there is an even amount and it then works and outputs the line alphabetically. I then remove the added character afterward.

Can anyone provide a more elegant way to do this?

Also, whilst on the subject if anyone has an idea on how I could insert " \" at the end of each line defined by a set amount of characters (72 for example) at the same time it would also be useful, e.g.

Code:
aspell-en bash-completion bzip2 \
gzip lynx p7zip rar tar unrar \
unzip zip

# 2  
Old 09-16-2009
Not sure if it's more elegant, but this works
Code:
cat /tmp/unsortedline.txt|xargs -n 1|sort|xargs

or to leave out duplicate names:
Code:
cat /tmp/unsortedline.txt|xargs -n 1|sort -u|xargs


Previous Thread | Next Thread
Test Your Knowledge in Computers #557
Difficulty: Easy
The most expert of software developers never make mistakes.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Multi line log files to single line format

I want to read the log file which was generate from other command . And the output was having multi line in log files for job name and server name. But i need to make all the logs on one line Source file 07/15/2018 17:02:00 TRANSLOG_1700 Server0005_SQL ... (2 Replies)
Discussion started by: ranjancom2000
2 Replies

2. UNIX for Dummies Questions & Answers

To find and display the middle line in a file using single line command.

Hi all, How can i display the middle line of a file using a single line command? (6 Replies)
Discussion started by: Lakme Pemmaiah
6 Replies

3. Shell Programming and Scripting

sed command to grep multiple pattern present in single line and delete that line

here is what i want to achieve.. i have a file with below contents cat fileName blah blah blah . .DROP this REJECT that . --sport 7800 -j REJECT --reject-with icmp-port-unreachable --dport 7800 -j REJECT --reject-with icmp-port-unreachable . . . more blah blah blah --dport 3306... (14 Replies)
Discussion started by: vivek d r
14 Replies

4. Shell Programming and Scripting

Formatting File having big single line into 95 Char Per Line

Hi All, I have 4 big files which contains one big line containing formatted character records, I need to format each file in such way that each File will have 95 Characters per line. Last line of each file will have newline character at end. Before:- File Name:- File1.dat 102 121340560... (10 Replies)
Discussion started by: lancesunny
10 Replies

5. Shell Programming and Scripting

Joining multi-line output to a single line in a group

Hi, My Oracle query is returing below o/p ---------------------------------------------------------- Ins trnas value a lkp1 x a lkp1 y b lkp1 a b lkp2 x b lkp2 y ... (7 Replies)
Discussion started by: gvk25
7 Replies

6. Shell Programming and Scripting

Multiple lines in a single column to be merged as a single line for a record

Hi, I have a requirement with, No~Dt~Notes 1~2011/08/1~"aaa bbb ccc ddd eee fff ggg hhh" Single column alone got splitted into multiple lines. I require the output as No~Dt~Notes 1~2011/08/1~"aaa<>bbb<>ccc<>ddd<>eee<>fff<>ggg<>hhh" mean to say those new lines to be... (1 Reply)
Discussion started by: Bhuvaneswari
1 Replies

7. Shell Programming and Scripting

Moving items to a new line

Hi, Let's I have the following strings (md5): 07177edf8261d28c6a003e583fcbe38c 0717c0037b3a20fc0f0998e673f228d5 0717d611a5d24374628b98e17fd00977,0717d611a5d24374628b98e17fd00977 07189a18afdae558bb5aadfe602e4a91 0719e97d481c239667f38a3e166bed74 071af3225fe50a1fdbb42c43aac313cc... (4 Replies)
Discussion started by: talfiq
4 Replies

8. UNIX for Dummies Questions & Answers

Alphabetical sort for multi line records contains in a single file

Hi all, I So, I've got a monster text document comprising a list of various company names and associated info just in a long list one after another. I need to sort them alphabetically by name... The text document looks like this: Company Name: the_first_company's_name_here Address:... (2 Replies)
Discussion started by: quee1763
2 Replies

9. Shell Programming and Scripting

awk between items including items

OS=HP-UX ksh The following works, except I want to include the <start> and <end> in the output. awk -F '<start>' 'BEGIN{RS="<end>"; OFS="\n"; ORS=""} {print $2} somefile.log' The following work in bash but not in ksh sed -n '/^<start>/,/^<end>/{/LABEL$/!p}' somefile.log (4 Replies)
Discussion started by: Ikon
4 Replies

10. Shell Programming and Scripting

single line input to multiple line output with sed

hey gents, I'm working on something that will use snmpwalk to query the devices on my network and retreive the device name, device IP, device model and device serial. I'm using Nmap for the enumeration and sed to clean up the results for use by snmpwalk. Once i get all the data organized I'm... (8 Replies)
Discussion started by: mitch
8 Replies

Featured Tech Videos