2uzhan.com
Advertisement
Now Place:2uzhan.com » Order and make a single line of items to install with apt-get

Order and make a single line of items to install with apt-get

Ubuntu @ September 1, 2012   Views:0

Hi.

If I had a large list of games to install and they were in this format:
sudo apt-get install viruskiller warzone2100
pouetchess gnome-mahjongg
qmc2
trigger
zod

... How would I order them like so (in a non-stop line with spaces between each game to install) and in alphabetical order with a command?
sudo apt-get install gnome-mahjongg pouetchess qmc2 trigger viruskiller warzone2100 zod

Edit: To make things easier lets ignore the "sudo apt-get install" part as that can be added last.

--------------Solutions-------------

Code:

sudo apt-get install `cat list.txt | sort | tr "\n" " "`

The sorting part is probably unnecessary, though...Hi Bachstelze.

I want them sorted in the text file (if possible). Then I can copy the full ordered line into the terminal.

I ran just this part:

Code:

cat list.txt | sort | tr "\n" " "

And got this (all is fine bar pouetchess and gnome-mahjongg being in the wrong order):

Code:

pouetchess gnome-mahjongg qmc2 trigger viruskiller warzone2100 zod

pouetchess and gnome-mahjongg are on the same line in the input file, so sort treats them as one word. If you want them sorted you can run another pass of tr before sort, replacing all spaces with newlines.

To get a sorted file, just redirect the output of sort:

Code:

cat list.txt | sort > list.sorted.txt

Code:

cat list.txt | sort | tr "\n" " "

# Turned this
viruskiller warzone2100
pouetchess gnome-mahjongg
qmc2
trigger
zod

# into this
pouetchess gnome-mahjongg qmc2 trigger viruskiller warzone2100 zod
# Then I copied it into list.txt and ran

Code:

cat list.txt | sort > list.sorted.txt

# Nothing changed.

-----------------------------------------------------

Code:

cat list.txt | sort > list.sorted.txt

# Turned this:
viruskiller warzone2100
pouetchess gnome-mahjongg
qmc2
trigger
zod

# into this:
pouetchess gnome-mahjongg
qmc2
trigger
viruskiller warzone2100
zod


try

Code:

sudo apt-get install $( cat list.txt | tr " " "\n" | sort )

it will replace all spaces with newlines for sorting purposes, but these newlines will get flattened either way when the result is passed to apt-get ( embedded command not shielded by "" )

to illustrate:

Code:

$ echo $'a\nb c'
a
b c
$ echo $( echo $'a\nb c' )  # unquoted, doesn't preserve formatting, all whitespaces become spaces
a b c
$ echo "$( echo $'a\nb c' )"  # quoted, preserves formatting
a
b c

I just ran the part

Code:

( cat list.txt | tr " " "\n" | sort )

and copied the terminal output list into a text file (for future use with apt). Cheers to you Vaphell.

Tags:
© 2018 2uzhan.com Contact