Arrays and functions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Arrays and functions
# 15  
Old 08-13-2012
I'd go about that in a slightly different way.

1) Strip out all non-alphanumeric and whitespace characters
2) Change all whitespace into newlines
3) Sort
4) uniq -c
5) Sort again to get the top 50
6) head -n 50

Code:
for FILE in *.txt
do
        sed 's/[^a-zA-Z \r\t]//g' "$FILE" | 
                tr -s ' \r\t' '\n' | 
                sort |
                uniq -c |
                sort -n |
                head -n 50
done

This User Gave Thanks to Corona688 For This Post:
# 16  
Old 08-13-2012
My problem is that I need specific array of words that should get omitted and that array you see at the bottom of the modified one. It executes for one file at a time and it should do before sorting so that I get a more productive list. So, that should be chipped in before step 5. How to do that?
# 17  
Old 08-13-2012
Put them in exclude.txt and use grep -f -F.

Code:
for FILE in *.txt
do
        sed 's/[^a-zA-Z \r\t]//g' "$FILE" | 
                tr -s ' \r\t' '\n' | 
                grep -v -F -f exclude.txt |
                sort |
                uniq -c |
                sort -n |
                head -n 50
done

This User Gave Thanks to Corona688 For This Post:
# 18  
Old 08-13-2012
Re: In continuation

Code:
cat ${a[$i]} | sed 's/\([[:space:]]\)/\1 \n/g' | sed 's/^[ \t]*//' | sed \
's/[ \t]*$//' | sed '/^$/d' | grep -v -F -f exclude.lst | tr -cd '[:alnum:] [:space:]'| sort \
| uniq -c | sort -nr | head -n 50 >> lsf2dout.txt

I tried out but I think only grep worked for me. The $FILE would modify original file, I think so, but I want the results to be stored separate without affecting the source txt files in the folder. I think it is good.

---------- Post updated 08-14-12 at 12:19 AM ---------- Previous update was 08-13-12 at 11:43 PM ----------

I think the code below gives exactly what I need. Thanks!

Code:
#!/bin/bash
# program for data mining
#this program needs a file named exclude.lst

echo "This programs read text files and brings out the"
echo "word count of first 50 most used words in a file."

a=(*.txt)

for (( i = 0; i < ${#a[*]}; i++ ))
do
echo "a[$i] = ${a[$i]}"
done
echo ""

for (( i = 0; i < ${#a[*]}; i++ ))
do
printf "=======================" >> lsf2dout.txt
printf "\n" >> lsf2dout.txt
printf "Name of the file: ${a[$i]}" >> lsf2dout.txt
printf "\n" >> lsf2dout.txt

cat ${a[$i]} | sed 's/\([[:space:]]\)/\1 \n/g' | sed 's/^[ \t]*//' | sed \
's/[ \t]*$//' | sed '/^$/d' | grep -w -v -F -f exclude.lst | tr -cd '[:alnum:] [:space:]'| sort \
| uniq -c | sort -nr | head -n 100 >> lsf2dout.txt

printf "\n\n" >> lsf2dout.txt
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to execute functions or initiate functions as command line parameters for below requirement?

I have 7 functions those need to be executed as command line inputs, I tried with below code it’s not executing function. If I run the ./script 2 then fun2 should execute , how to initiate that function I tried case and if else also, how to initiate function from command line if then... (8 Replies)
Discussion started by: saku
8 Replies

2. Programming

question about int arrays and file pointer arrays

if i declare both but don't input any variables what values will the int array and file pointer array have on default, and if i want to reset any of the elements of both arrays to default, should i just set it to 0 or NULL or what? (1 Reply)
Discussion started by: omega666
1 Replies

3. Shell Programming and Scripting

i think i need functions ?

Hi, im making a little script but need some help Code i have so far is read -p 'Bot Nickname:' ecnick read -p 'Bot Username:' ecusername read -p 'Bot Realname:' ecrealname read -p 'Your Email:' ecemail echo '' echo Your bots nickname is set to $ecnick echo Your bots username is set to... (2 Replies)
Discussion started by: Gemster
2 Replies

4. UNIX for Dummies Questions & Answers

Help with functions

Hi, I am exploring with defining functions in my BASH shell scripts. However, I am bit confused about how to pass parameters to my functions. I was under the impression that you must do something like the following: Define a function called "sample_function": function sample_function {... (3 Replies)
Discussion started by: msb65
3 Replies

5. Shell Programming and Scripting

Need a little help with functions

I'm semi new to unix/linux and am trying to convert a program I wrote in C++ to a bash script. It's a program that prints Fibonacci's series. I have found scripts that will do it, but I'm trying persistently to get this one to work. The problem occurs when I try to return a value from the function.... (3 Replies)
Discussion started by: Glowworm
3 Replies

6. Shell Programming and Scripting

functions

I have korn shells where I want to create a function passing $1 to a function , determine my $STAT_ENV value, set the paths and return the paths for STATSH,STATPRM,STATSQR,STATSQL,STATCTL TO BE USED IN THE UNIX SCRIPT THE CALLED THE fucnction in the first place. Can someone tell me the best... (2 Replies)
Discussion started by: TimHortons
2 Replies

7. Web Development

PHP arrays in arrays

PHP question... I have an SQL query that's pulled back user IDs as a set of columns. Rather than IDs, I want to use their names. So I have an array of columns $col with values 1,7,3,12 etc and I've got an array $person with values "Fred", "Bert", "Tom" etc So what I want to do is display the... (3 Replies)
Discussion started by: JerryHone
3 Replies

8. Shell Programming and Scripting

Passing arrays between functions

Hi, I have a function that hold 3 arrayies. I need to pass them to another function as an input, for further use Could you please explain how to do that. Thanks (5 Replies)
Discussion started by: yoavbe
5 Replies

9. Shell Programming and Scripting

perl functions and arrays

Hi, First I will tell my objective of this function (function one). I have a table for ex: id passwd name -- ------ ----- 1 fdhgfs werwer 2 fsdfs sdfsdf 3 sdfs sdfsdf 4 fdsfs dssdf . . . . . . The id, passwd and name are the arguments for another function say two. (1 Reply)
Discussion started by: mercuryshipzz
1 Replies

10. Shell Programming and Scripting

Use of functions

Hi my shell is tcsh can I have functions in my shell scripting? Is the below shell script correct. Can I have two functions and call one of them as required. ---------- echo "functions" f1 f1 () { echo "hello" } f2 () (1 Reply)
Discussion started by: amitrajvarma
1 Replies
Login or Register to Ask a Question