To Write a shell script that accepts the name of a text file and finds the number of sentences, number of words, and number of words that start with a vowel.
I had done trying with the below code. every time i try it shows the error
**enter code here
echo"Enter File Name:"
read file
lc=$(wc --lines $file)
wc=$(wc --words $file)
vow=$(grep -o -i" [^AEIOUaeiou]" | wc --words $file)
echo "Lines" $lc
echo "Words" $wc
echo "Vowels Words" $vow**
Here:
echo "Enter File Name:"
read file
lc=$(cat "${file}"|wc -l)
wc=$(cat "${file}"|wc -w)
vow=$(cat "${file}"|grep -o " [AEIOUaeiou][^ ]\+ " |wc -l)
echo "Lines: $lc"
echo "Words: $wc"
echo "Vowels Words: $vow"
Related
#!/usr/bin/ksh
PATH=$PATH:/usr/bin/sed
export PATH
#echo -n "Enter file name : "
read name
if [ -e $name ]
then
var1="$(wc -l < $name)"
print "Total lines in file including header and trailer : $var1"
var2=$(tail -1 $name|cut -c11-20)
print $var2
var3="$((10#${var2}))"
print "Total record count as per Trailer record: $var3"
val=$(($var1-2))
print "total records w/o header and trailer is : $val"
if [[ $val -eq $var3 ]];then
echo " "
echo "Trailer record information for total record count is true"
else
echo "Trailer record count is erroneous"
fi
#echo -n "Please provide journal date string : "
#read journal
#journal=$(tail -n2 $name|head -n1|cut -c42-45)
#print "grep -c \"$journal\" $name"
journal1=$(tail -n2 $name|head -n1|cut -c42-45)
print $journal1
journal=$("/bin/sed '1d;\$d' $name|cut -c42-45|grep -c \"$journal1\"")
print "sed '1d;\$d' $name|cut -c42-45|grep -c \"$journal1\""
print $journal
if [[ $journal -eq $var3 ]];then
echo "All journals are valid"
else
echo "Please check journals manually."
fi
else
echo "File does not exists"
fi
when i execut ethe above script i get sed command not found error. Same sed command is running on command prompt. Can someone help me out? I have tried using PATH where sed command is found.
when i execute "whereis" for sed. i get this : sed: /usr/bin/sed
Try with little steps, like
echo "Almost solved this"| sed 's/Almost/We have/'
I think the sed in this line will be found with your normal PATH.
Look at the strange sedline in the script.
journal=$("sed '1d;\$d' $name|cut -c42-45|grep -c \"$journal1\"")
will try to assign the stdout of the command to the variable journal. And the command is
"sed '1d;\$d' $name|cut -c42-45|grep -c \"$journal1\""
This is not sed with options piped to other commands, but a very long filename sed '1d;$d' sample.dat|cut -c42-45|grep -c "2016".
This is a completely different from
sed '1d;\$d' $name|cut -c42-45|grep -c \"$journal1\"
or better
sed '1d;$d' "$name" |cut -c42-45|grep -c "$journal1"
When the header and tail line are small, you can avoid sed and $journal1 with
if [ $(cut -c42-45 "$name"| sort -u| wc -l) -eq 1 ]; then
echo "All records have the same journal"
else
echo "Something funny..."
fi
And please indent your file next time.
I have my below shell script which searches for a string inside a file and returns the count. Not sure why it's getting stuck in the middle. Please can anyone explain.
#!/bin/bash
read -p "Enter file to be searched: " $file
read -p "Enter the word you want to search for: " $word
count=$(grep -o "^${word}:" $file | wc -l)
echo "The count for `$word`: " $count
OUTPUT:
luckee#zarvis:~/scripts$ ./wordsearch.sh
Enter file to be searched: apple.txt
Enter the word you want to search for: apple
^C
read needs to be passed a variable name. file, not $file.
#!/bin/bash
read -p "Enter file to be searched: " file
read -p "Enter the word you want to search for: " word
count=$(grep -o -e "$word" "$file" | wc -l)
echo "The count for $word: $count"
What was happening previously is that your file variable was empty, so your code was running:
count=$(grep -o "^${word}:" | wc -l)
...with no input specified, so it would wait forever for stdin.
By the way -- you don't need wc for this; grep can emit a counter itself, using the -c argument (also called --count in the GNU implementation). If you want that counter to go by words rather than lines, one can use tr to put each word on its own line:
count=$(tr '[[:space:]]' '\n' <"$file" | grep -c -e "$word")
I have this script where I ask for 4 patterns and then use those in a grep command. That is, I want to see if a line matches any of the patterns.
echo -n "Enter pattern1"
read pat1
echo -n "Enter pattern2"
read pat2
echo -n "Enter pattern3"
read pat3
echo -n "Enter pattern4"
read pat4
cat somefile.txt | grep $pat1 | grep $pat2 | grep $pat3 | grep $pat4
The problem I'm running into is that if the user doesn't supply one of the patterns (which I want to allow) the grep command doesn't work.
So, is there a way to have grep ignore one of the patterns if it's returned empty?
Your code has lots of problems:
Code duplication
Interactive asking for potentially unused information
using echo -n is not portable
useless use of cat
Here is what I wrote that is closer to what you should use instead:
i=1
printf %s "Enter pattern $i: "
read -r input
while [[ $input ]]; do
pattern+=(-e "$input")
let i++
printf %s "Enter pattern $i (Enter or Ctrl+D to stop entering patterns): "
read -r input
done
echo
grep "${pattern[#]}" somefile.txt
EDIT: This does not answer OP's question, this searches for multiple patterns with OR instead of AND...
Here is a working AND solution (it will stop prompting for patterns on the first empty one or after the 4th one):
pattern=
for i in {1..4}; do
printf %s "Enter pattern $i: "
read -r input
[[ $input ]] || break
pattern="${pattern:+"$pattern && "}/${input//\//\\/}/"
done
echo # skip a line
awk "$pattern" somefile.txt
Here are some links from which you can learn how to program in bash:
Bash Guide
Bash FAQ
Here I made a small script that take input from user searching some pattern from a file and displays required no of lines from that file where the pattern is found. Although this code is searching the pattern line wise due to standard grep practice. I mean if the pattern occurs twice on the same line, i want the output to print twice. Hope I make some sense.
#!/bin/sh
cat /dev/null>copy.txt
echo "Please enter the sentence you want to search:"
read "inputVar"
echo "Please enter the name of the file in which you want to search:"
read "inputFileName"
echo "Please enter the number of lines you want to copy:"
read "inputLineNumber"
[[-z "$inputLineNumber"]] || inputLineNumber=20
cat /dev/null > copy.txt
for N in `grep -n $inputVar $inputFileName | cut -d ":" -f1`
do
LIMIT=`expr $N + $inputLineNumber`
sed -n $N,${LIMIT}p $inputFileName >> copy.txt
echo "-----------------------" >> copy.txt
done
cat copy.txt
As I understood, the task is to count number of pattern occurrences in line. It can be done like so:
count=$((`echo "$line" | sed -e "s|$pattern|\n|g" | wc -l` - 1))
Suppose you have one file to read. Then, code will be following:
#!/bin/bash
file=$1
pattern="an."
#reading file line by line
cat -n $file | while read input
do
#storing line to $tmp
tmp=`echo $input | grep "$pattern"`
#counting occurrences count
count=$((`echo "$tmp" | sed -e "s|$pattern|\n|g" | wc -l` - 1))
#printing $tmp line $count times
for i in `seq 1 $count`
do
echo $tmp
done
done
I checked this for pattern "an." and input:
I pass here an example of many 'an' letters
an
ananas
an-an-as
Output is:
$ ./test.sh input
1 I pass here an example of many 'an' letters
1 I pass here an example of many 'an' letters
1 I pass here an example of many 'an' letters
3 ananas
4 an-an-as
4 an-an-as
Adapt this to your needs.
How about using awk?
Assume the pattern you are searching for is in variable $pattern and the file you are checking is $file
The
count=`awk 'BEGIN{n=0}{n+=split($0,a,"'$pattern'")-1}END {print n}' $file`
or for a line
count=`echo $line | awk '{n=split($0,a,"'$pattern'")-1;print n}`
Working in a shell script here, trying to count the number of words/characters/lines in a file without using the wc command. I can get the file broken into lines and count those easy enough, but I'm struggling here to get the words and the characters.
#define word_count function
count_stuff(){
c=0
w=0
l=0
local f="$1"
while read Line
do
l=`expr $line + 1`
# now that I have a line I want to break it into words and characters???
done < "$f"
echo "Number characters: $chars"
echo "Number words: $words"
echo "Number lines: $line"
}
As for characters, try this (adjust echo "test" to where you get your output from):
expr `echo "test" | sed "s/./ + 1/g;s/^/0/"`
As for lines, try this:
expr `echo -e "test\ntest\ntest" | sed "s/^.*$/./" | tr -d "\n" | sed "s/./ + 1/g;s/^/0/"`
===
As for your code, you want something like this to count words (if you want to go at it completely raw):
while read line ; do
set $line ;
while true ; do
[ -z $1 ] && break
l=`expr $l + 1`
shift ;
done ;
done
You can do this with the following Bash shell script:
count=0
for var in `cat $1`
do
count=`echo $count+1 | bc`
done
echo $count