How can I read variable text files through cat command?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How can I read variable text files through cat command?
# 1  
Old 03-29-2012
How can I read variable text files through cat command?

Hi.
I'm just wondering how can I read variable text files through cat command.
I made a shell script to count frequency of words and array them with variable conditions. It's only working for one file that I wrote in script now. But I want to make it working for every text file when I execute script in the terminal like this : './*.sh -a filename.txt' , './*.sh -a filename2.txt', './*.sh -a filename3.txt'

Here is the code. Give me some advices plz. Thanks
Code:
#!/bin/bash

ALPHABETICALLY=0
FREQUENTLY=0
DESCENDING=0
while [ $# -gt 0 ] ; do
        case $1 in
                "-h"|"--help")
                        echo "Usage $0 [-a|-n] [-r] <file-name>" >&2
                        exit 0
                        ;;
                "-a")
                ALPHABETICALLY=1        #-a specified, change the var
                cat test.txt|tr -d '[:punct:]'|sed 's/ /\
/g'|tr -s '\n'|tr '[A-Z]' '[a-z]'|sort|uniq -c

                shift                   #shift cmdline arguments to the left
                        ;;
                "-ar")
                ALPHABETICALLY=1
                DESCENDING=1
               cat test.txt|cat test.txt|tr -d '[:punct:]'|sed 's/ /\
/g'|tr -s '\n'|tr '[A-Z]' '[a-z]'|sort -r|uniq -c
                shift
                    ;;
                "-n")
                FREQUENTLY=1
                echo "$input"|tr -d '[:punct:]'|sed 's/ /\
/g'|tr -s '\n'|tr '[A-Z]' '[a-z]'|sort -n|uniq -c|sort -n
                shift
                     ;;
                "-nr")
                FREQUENTLY=1
                DESCENDING=1
                cat test.txt|tr -d '[:punct:]'|sed 's/ /\
/g'|tr -s '\n'|tr '[A-Z]' '[a-z]'|sort -n|uniq -c|sort -nr
                shift
                ;;
        esac
done

# 2  
Old 03-29-2012
You need to switch to using getopts in order to process all those command line arguments...this will give you the ability to specify the filename as a variable argument...
# 3  
Old 03-29-2012
getopts won't let him use long arguments like --help, though

I think this can be fixed simply enough, anyway. Ditch the argument with shift first, then use "$@" to get all remaining options.

Code:
#!/bin/bash

ALPHABETICALLY=0
FREQUENTLY=0
DESCENDING=0
while [ $# -gt 0 ] ; do
        case $1 in
                "-h"|"--help")
                        echo "Usage $0 [-a|-n] [-r] <file-name>" >&2
                        exit 0
                        ;;
                "-a")
                ALPHABETICALLY=1        #-a specified, change the var
                shift # Shift first, to ditch -a
                cat "$@" |tr -d '[:punct:]'|sed 's/ /\
/g'|tr -s '\n'|tr '[A-Z]' '[a-z]'|sort|uniq -c
                        ;;
                "-ar")
                shift # Shift first, to ditch -ar
                ALPHABETICALLY=1
                DESCENDING=1
               cat "$@"|cat test.txt|tr -d '[:punct:]'|sed 's/ /\
/g'|tr -s '\n'|tr '[A-Z]' '[a-z]'|sort -r|uniq -c
                    ;;
                "-n")
                FREQUENTLY=1
                shift # Shift first, to ditch -n
                echo "$input"|tr -d '[:punct:]'|sed 's/ /\
/g'|tr -s '\n'|tr '[A-Z]' '[a-z]'|sort -n|uniq -c|sort -n
                     ;;
                "-nr")
                FREQUENTLY=1
                DESCENDING=1
                shift # Shift first, to ditch -nr
                cat "$@"|tr -d '[:punct:]'|sed 's/ /\
/g'|tr -s '\n'|tr '[A-Z]' '[a-z]'|sort -n|uniq -c|sort -nr
                shift
                ;;
        esac
done

I'd have built the program a little differently though, putting the actual operations outside the loop, but it looks like you're planning on doing that anyway. You don't have to test for every combination of -a, -ar, -n, -nr though, you can process the 'a', 'r', 'n' individually:

Code:
while [ "$#" -gt 0 ]
do
        case "$1" in
        --help) # long argument
                echo "stuff" >&2
                exit 0
                ;;

        --*)
                echo "Unknown long argument $1" >&2
                exit 1
                ;;

        -[^-]*) # Short arguments
                for((N=1; N<${#1}; N++)
                do
                        # Test each individual letter
                        case "${1:$N:1}" in
                        a)     echo "Got short option -a" >&2
                                ;;
                        *)
                                echo "Unknown short option ${1:$N:1}" >&2
                                exit 1
                                ;;
                         esac
                done
                ;;

        *)     echo "File arguments reached, stop shifting" >&2
                break
                ;;
        esac

        shift
done

if [ "$#" -eq 0 ]
then
        echo "No file arguments given" >&2
        exit 1
fi

echo "Operating on files" "$@"

do_stuff

exit 0

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Not able read data with cat command

I am trying to read some data form .key file with cat command..but its not displaying data correctly..tried with other commands also still no use.i tried with CAT with SED combination but no use.. Please help me with the command or script. (10 Replies)
Discussion started by: kish_rock
10 Replies

2. Shell Programming and Scripting

Cat files listed in text file and redirect to new directory with same filename

I have a directory that is restricted and I cannot just copy the files need, but I can cat them and redirect them to a new directory. The files all have the date listed in them. If I perform a long listing and grep for the date (150620) I can redirect that output to a text file. Now I need to... (5 Replies)
Discussion started by: trigger467
5 Replies

3. Shell Programming and Scripting

Read text between regexps and write into files based on a field in the text

Hi, I have a huge file that has data something like shown below: huge_file.txt start regexp Name=Name1 Title=Analyst Address=Address1 Department=Finance end regexp some text some text start regexp Name=Name2 Title=Controller Address=Address2 Department=Finance end regexp (7 Replies)
Discussion started by: r3d3
7 Replies

4. Shell Programming and Scripting

Read n lines from a text files getting n from within the text file

I dont even have a sample script cause I dont know where to start from. My data lookes like this > sat#16 #data: 15 site:UNZA baseline: 205.9151 0.008 -165.2465 35.8109 40.6685 21.9148 121.1446 26.4629 -18.4976 33.8722 0.017 -165.2243 48.2201 40.6908 ... (8 Replies)
Discussion started by: malandisa
8 Replies

5. Shell Programming and Scripting

Directing cat or grep command in variable

Hi, I still have the problem with directing information from cat or grep to a variable. For instance: XMSG "$(date +%Y_%m_%d)_error_report.txt" "$(cat "$(date +%Y_%m_%d)_error_report.txt")" &Works! The text received by cat is directed to my function. If it is written like this, my... (2 Replies)
Discussion started by: haukee
2 Replies

6. UNIX for Dummies Questions & Answers

format values in a text file using cat command

Hello... Is it possible that we can change the format of the values we entered in a text file using cat? Like for example, I will create a text file names.txt using cat and as I save the names and view the text file... the format will be like this ... (5 Replies)
Discussion started by: kpopfreakghecky
5 Replies

7. Fedora

How to read a text file and assign the values in the same to a variable in loop

Hi, I have a text file with multiple lines, each having data in the below format <DOB>,<ADDRESS> I have to write a script which reads each line in the text file in loop, assign the values to these variables and do some further processing in it. Using the following code prints the... (1 Reply)
Discussion started by: manishab00
1 Replies

8. Shell Programming and Scripting

Read files, lines into array, cat vs open

Hi Everyone, I have a file: a.txt a,b,c,d,6,6,6 1,2,3,d,6,6,6 4,5,6,6,6,6,6 #!/usr/bin/perl use warnings; use strict; my @array = (); ### Load file into array for my $i (split '\n', `cat /tmp/a.txt`) { push @array, ; } It works. But my a.txt have 1million lines, and... (2 Replies)
Discussion started by: jimmy_y
2 Replies

9. Shell Programming and Scripting

cat in the command line doesn't match cat in the script

Hello, So I sorted my file as I was supposed to: sort -n -r -k 2 -k 1 file1 | uniq > file2 and when I wrote > cat file2 in the command line, I got what I was expecting, but in the script itself ... sort -n -r -k 2 -k 1 averages | uniq > temp cat file2 It wrote a whole... (21 Replies)
Discussion started by: shira
21 Replies

10. Shell Programming and Scripting

Cat and read command

Here's what the shell (korn) script is doing: 1. Prompt for input 2. Read a file using the cat and the while read command. 3. Get a field on the record and hold that value in a variable. 4. Within the process, I then need to prompt the user again to ask them if they are sure they want to make... (3 Replies)
Discussion started by: mango
3 Replies
Login or Register to Ask a Question