How to sort a content of a text file using a shell script?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to sort a content of a text file using a shell script?
# 1  
Old 12-04-2014
How to sort a content of a text file using a shell script?

I am new to shell scripting. I am interested how to know how to sort a content of a file using shell scripting.

I've attached the 'Input file' and the 'expected output' to this thread.

Details provided in the expected output file will provide details on how the sort needs to be done.

Basically I need the count (in many parts) of the data in the output. The input file contains 565075 entries hence the addition of all the count of <application> should be equal to 565075 (refer to output file attached for details).

Help me!

Last edited by nkarthik_mnnit; 12-04-2014 at 06:16 AM..
# 2  
Old 12-04-2014
What you want to achieve is way beyond sorting. So, text manipulation languages like awk, perl etc. would be candidates. And, I'd propose you put the specification to your request into the post, not into an attached file.
# 3  
Old 12-04-2014
I am unable to use 'awk'. I would not be interested to use perl here. I would like this to be done on an unix environment using a shell script. Someone please help me with the output script.

Last edited by nkarthik_mnnit; 12-04-2014 at 06:38 AM..
# 4  
Old 12-04-2014
Is this a homework assignment? Posts requesting help and not allowing use of utilities like awk (which is a standard UNIX utility available on all UNIX and Linux systems) frequently means that your professor is placing certain restriction on the tools to be used for an assignment. Homework assignments must follow the rules for the homework forum.

If this is not a homework assignment, as RudiC suggested, please post the requirements for your task in a post; not hidden in two attached files that will consume over 1.75Mb of space on the system of any volunteers here who might be willing to help you solve your problem.
# 5  
Old 12-04-2014
From a distance, putting the data into an associative array intrinsically sorts it. Putting it intoa normal, integer indexed array allows you to sort it using simple sorts like the bubble sort or a bisection insertion sort.

The right solution is divining what the professor wants. Better is OK if prof has a sense of humor or honor.
# 6  
Old 12-08-2014
Here is the sample input.

Code:
fap0089-josh.baker
fap00233-adrian.edwards
fap00293-bob.boyle
fap00293-bob.jones
fap002-brian.lopez
fap00293-colby.morris
fap00293-cole.mitchell
psf0354-SKOWALSKI
psf0354-SLEE
psf0382-SLOWE
psf0391-SNOMURA
psf0354-SPATEL
psf0364-SRICHARDS
psf0354-SSEIBERT
psf0354-SSIRAH
bsi0004-STRAN
bsi0894-STURBIC
unit054-SUNDERWOOD
user1
user2

Here, the data is of the format <app><env>-<user>

bsi, fap, unit etc are 'app'
the numeric following the app name is the 'env'
the name after the hypen is the 'user'

There might also be other values not of the format given above. e.g user1,user2

I need the data to be sorted in the following format

Code:
app name            number of env          total count
-----------            ----------------           ------------

NOTE : If 'app' value is fap,bsi etc, it should list all the unique environments in the 'env' column. e.g: the value of env is 5 for app name 'fap' as the number of unique env for fap is 5.

Total count should give the number of entries under the particular app. e.g: fap = 6, bsi = 2

The entries not falling under any app (not of the format '<app><env>-<user>'), e.g: user1, user2 should be put under the separate count list named 'non-app'. Here, the non-app count is 2.
---------------------------------------------------------------------------------------------------
I have 565075 entries which needs to be sorted. A sample script which will sort entries for such huge input will help me.

This is not a homework. I'm running my own business and this will help me with it.

Badly require this forum's expert help!
Moderator's Comments:
Mod Comment Please use CODE tags for all sample input, output, and code samples. Without CODE tags, sequences of spaces and tabs are converted to a single space by normal HTML processing.

Last edited by Don Cragun; 12-08-2014 at 07:12 AM.. Reason: Add CODE tags.
# 7  
Old 12-08-2014
For the sample input in post #6, what is the output that you want from sorting that sample input data?

And, again, why can't you use awk for this? It seems that it would be very helpful for this assignment.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Shell script/awk to sort text

1. The problem statement, all variables and given/known data: I have a file with a fragment of a novel, which I have to clear from punctuation and sort all the words contained one per line and non duplicated, all this going to a file called "palabras". Here is fragment of the input file: ... (4 Replies)
Discussion started by: ektorzoza
4 Replies

2. Shell Programming and Scripting

Shell Script to Dynamically Extract file content based on Parameters from a pdf file

Hi Guru's, I am new to shell scripting. I have a unique requirement: The system generates a single pdf(/tmp/ABC.pdf) file with Invoices for Multiple Customers, the format is something like this: Page1 >> Customer 1 >>Invoice1 + invoice 2 >> Page1 end Page2 >> Customer 2 >>Invoice 3 + Invoice 4... (3 Replies)
Discussion started by: DIps
3 Replies

3. Shell Programming and Scripting

Shell script to monitor new file in a directory and mail the file content

Hi I am looking for a help in designing a bash script on linux which can do below:- 1) Look in a specific directory for any new files 2) Mail the content of the new file Appreciate any help Regards Neha (5 Replies)
Discussion started by: neha0785
5 Replies

4. Shell Programming and Scripting

Script to create a text file whose content is the text of another files

Hello everyone, I work under Ubuntu 11.10 (c-shell) I need a script to create a new text file whose content is the text of another text files that are in the directory $DIRMAIL at this moment. I will show you an example: - On the one hand, there is a directory $DIRMAIL where there are... (1 Reply)
Discussion started by: tenteyu
1 Replies

5. Shell Programming and Scripting

Need to build Shell Script to search content of a text file into a folder consist several files

Have to read one file say sourcefile containing several words and having another folder containing several files. Now read the first word of Sourcefile & search it into the folder consisting sevral files, and create another file with result. We hhave to pick the filename of the file in which... (3 Replies)
Discussion started by: mukesh.baranwal
3 Replies

6. Shell Programming and Scripting

Shell script to remove some content in a file

How can I remove all data that contain domain e.g zzgh@something.com, sdd@something.com.my and gg@something.my in one file? so that i only have data without the domain in the file. Here is the file structure "test.out" more test.out 1 zzztop@b.com 1 zzzulll 1 zzzullll@s.com.my ... (4 Replies)
Discussion started by: Mr_47
4 Replies

7. UNIX for Dummies Questions & Answers

creating text file with content from script

hi, can somebody tell me how I can create a text file with content from Bash script. The file should be prefilled with information such as current date and time then leaving the user ability to input more data right below those prefilled content. thank you :) (0 Replies)
Discussion started by: s3270226
0 Replies

8. Shell Programming and Scripting

Problem getting the content of a file in a shell script variable

Hi, I have a text file that has a long multi-line db2 CTE query. Now I want to store all the contents of this file (i.e. the entire query) in a shell script variable. I am trying to achieve it by this: query = `cat /Folder/SomeFile.txt` But when I echo the contents of this file by saying echo... (4 Replies)
Discussion started by: DushyantG
4 Replies

9. Shell Programming and Scripting

Sort content of text file based on date?

I now have a 230,000+ lines long text file formatted in segments like this: Is there a way to sort this file to have everything in chronological order, based on the date and time in the text? In this example, I would like the result to be: (19 Replies)
Discussion started by: KidCactus
19 Replies

10. Shell Programming and Scripting

shell script to edit the content of a file

Hi I need some help using shell script to edit a file. My original file has the following format: /txt/email/myemail.txt /txt/email/myemail2.txt /pdf/email/myemail.pdf /pdf/email/myemail2.pdf /doc/email/myemail.doc /doc/email/myemail2.doc I need to read each line. If the path is... (3 Replies)
Discussion started by: tiger99
3 Replies
Login or Register to Ask a Question