Sponsored Content
Top Forums UNIX for Dummies Questions & Answers How to sort a content of a text file using a shell script? Post 302928141 by ongoto on Tuesday 9th of December 2014 07:53:42 AM
Old 12-09-2014
This needs to be tested. You'll find that awk can do these kinds of jobs 10 times faster.
Code:
#!/bin/bash

# infile=sample.txt
infile=Input_File.txt

fmt="%-12s\t%-6s\t%-6s\n"
line="~~~~~~"
line2="~~~~~~~~~~~~"
printf $fmt "    App" "Env   " "Count "
printf $fmt "$line2" "$line" "$line"

grep -e ^.*[/A-Za-z].*[0-9].*-.*$ $infile | sed 's/^[/A-Za-z]*/&-/g' | awk -F '-' '{ print $1, $2 }' | sort > outfile
sort -u outfile -o outfile2
idx=($(cat outfile | cut -d ' ' -f 1 | sort -u))

for X in ${idx[*]};
do
    appcnt=$(grep -c "^$X " outfile)
    envcnt=$(grep -c "^$X " outfile2)
    envtot=$(( $envtot + $envcnt ))
    apptot=$(( $apptot + $appcnt ))
    printf $fmt "$X" "$envcnt" "$appcnt"
done

# next two lines added for speed up
# line commented out was for testing
fsz=$(cat $infile | wc -l)
unclass=$(( $fsz - $apptot ))
# unclass=$(grep -v -e ^.*[/A-Za-z].*[0-9].*-.*$ $infile | wc -l)
printf $fmt "Unclassified" "N/A" "$unclass"
printf $fmt "$line2" "$line" "$line"
printf $fmt "Totals" "$envtot" "$(( $apptot + $unclass ))"

# eof #

output
-------
    App         Env       Count 
~~~~~~~~~~~~    ~~~~~~    ~~~~~~
app             1         44    
bics            1         49    
fap             1254      475732
fapbs           1         160   
jde             77        4383  
la              2         3     
NMDemo          35        3754  
/NMDemo         1         160   
NMTalent        6         592   
ofm             1         592   
PRI             40        40    
psf             114       67601 
sbl             34        2890  
unit            166       7415  
Unclassified    N/A       1660  
~~~~~~~~~~~~    ~~~~~~    ~~~~~~
Totals          1733      565075


Last edited by ongoto; 12-10-2014 at 08:46 PM.. Reason: fixed error; /NMDemo now recognized
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
fmt(1)								   User Commands							    fmt(1)

NAME
fmt - simple text formatters SYNOPSIS
fmt [-cs] [-w width | -width] [inputfile...] DESCRIPTION
fmt is a simple text formatter that fills and joins lines to produce output lines of (up to) the number of characters specified in the -w width option. The default width is 72. fmt concatenates the inputfiles listed as arguments. If none are given, fmt formats text from the standard input. Blank lines are preserved in the output, as is the spacing between words. fmt does not fill nor split lines beginning with a `.' (dot), for compatibility with nroff(1). Nor does it fill or split a set of contiguous non-blank lines which is determined to be a mail header, the first line of which must begin with "From". Indentation is preserved in the output, and input lines with differing indentation are not joined (unless -c is used). fmt can also be used as an in-line text filter for vi(1). The vi command: !}fmt reformats the text between the cursor location and the end of the paragraph. OPTIONS
-c Crown margin mode. Preserve the indentation of the first two lines within a paragraph, and align the left margin of each subsequent line with that of the second line. This is useful for tagged paragraphs. -s Split lines only. Do not join short lines to form longer ones. This prevents sample lines of code, and other such formatted text, from being unduly combined. -w width | -width Fill output lines to up to width columns. OPERANDS
inputfile Input file. ENVIRONMENT VARIABLES
See environ(5) for a description of the LC_CTYPE environment variable that affects the execution of fmt. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWcsu | +-----------------------------+-----------------------------+ SEE ALSO
nroff(1), vi(1), attributes(5), environ(5) NOTES
The -width option is acceptable for BSD compatibility, but it may go away in future releases. SunOS 5.10 9 May 1997 fmt(1)
All times are GMT -4. The time now is 11:19 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy