hey all, im new at shell scripting and need to find out a simple way to create text files, with random words in them. then needing to input 3 of these files arranging their lines in alphabetical order
and after that creating an output file with the last lines sorted in reverse alphabetical order.

i really am a beginner so could you please explain as you help.
thanks in advance

Ok first are we talking Linux or windows shell?

So sorry, linux

Good then this will be easy.

The text files with random words I am assuming you can make for your self. But lets say you have the three files called text1.txt text2.txt and text3.txt. Linux has a built in sort command called sort that will do what you are trying to do. To use it you either pipe the output of another program to it or redirect a file in to it.

** Note in my examples the $ is your prompt and not part of the command.

For example to sort the contents of text1.txt you could cat the file and pipe it to sort like this:

$ cat text1.txt | sort

The results will be displayed on your screen. To write the output to a file you simply redirect the output to a file name. The file does not need to exist and using a single redirect if it exists it will be overwritten.

$ cat text1.txt | sort > sortedfile.txt

To sort all three files together you simply list them one after the other:

$ cat text1.txt text2.txt text3.txt | sort > big_sortedfile.txt

By default sort uses the first word of each line and sorts alphanumerically which means that 2 is larger than 10 because the first letter (2) is larger than the first letter of 10 (1). There is a flag to tell it to look for numerical values but you did not ask about that.

I have also been told to use the 'tail' command, should I do that or is your way simpler.
Also how do I go about taking the last word of each text file and creating an output file with those words in reverse alphabetical order ?

SOrry misread that part. This is where tail would come in. tail is used to view the last 10 (or a number you select) of lines from a file. to view the last line of a file you would use:

$ tail -1 myfile

and a script to read the last lines from a list of files and then sort those lines could look like this:

$ for x in text1.txt text2.txt text3.txt
> do
> tail -1 $x >> out1
> done
$ cat out1 | sort > out2

The first part runs a little for loop to write the last line of each file (you can list as many files names as you want separated by a space or tab) to a new file and then the last line sorts that file and writes it to another file.

Thank you so much I really appreciate all of your help. One last thing, hope you don't mind me asking.. If I want the script to check that only 3 files are given as input, if more or less are given as input I want to display an error message. How do I go about displaying this error message if more or less than 3 files are given as input or if any of the input files do not exist ?

Thanks again so much

You might want to look into any of the O'Reilly books on the bash shell. The Bash cookbook is a good reference. It will give you all of these examples.

Use the shell built-in variable ${#}. Here’s some scripting to enforce an exact count
of three arguments:
#!/usr/bin/env bash
# cookbook filename: check_arg_count
#
# Check for the correct # of arguments:
# Use this syntax or use: if [ $# -lt 3 ]
if (( $# < 3 ))
then
printf "%b" "Error. Not enough arguments.\n" >&2
printf "%b" "usage: myscript file1 op file2\n" >&2
exit 1
elif (( $# > 3 ))
then
printf "%b" "Error. Too many arguments.\n" >&2
printf "%b" "usage: myscript file1 op file2\n" >&2
exit 2
else
printf "%b" "Argument count correct.
Proceeding...\n"
fi

Use the various file characteristic tests in the test command as part of
your if statements. Your specific problems might be solved with scripting
that looks something like this. This tests for the existence of a file:

if [ -e myfile ]
then do something
fi.


Unary operators that check file characteristics
Option Description
-b File is block special device (for files like /dev/hda1)
-c File is character special (for files like /dev/tty)
-d File is a directory
-e File exists
-f File is a regular file
-g File has its set-group-ID bit set
-h File is a symbolic link (same as -L)
-G File is owned by the effective group ID
-k File has its sticky bit set
-L File is a symbolic link (same as -h)
-O File is owned by the effective user ID
-p File is a named pipe
-r File is readable
-s File has a size greater than zero
-S File is a socket
-u File has its set-user-ID bit set
-w File is writable
-x File is executable

thank you for all of your help i will definately do that.
take care

Hi I'm also new in shell scripting, and if you can help me with this problem that I have i would be very grateful. How do I check with shell scrip before i create an output file it checks if that file exist or not. If the file exists, then another file name has to be used.
Thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.