linux script to find specific words in file names - linux

I need help writing a script to do the following stated below in part a.
The following code will output all of the words found in $filename, each word on a separate line.
for word in “cat $filename”
do
echo $word
done
a. Write a new script which receives two parameters. The first is a file’s name ($1 instead of $filename) and the second is a word you want to search for ($2). Inside the for loop, instead of echo $word, use an if statement to compare $2 to $word. If they are equal, add one to a variable called COUNT. Before the for loop, initialize COUNT to 0 and after the for loop, output a message that tells the user how many times $2 appeared in $1. That is, output $COUNT, $2 and $1 in an echo statement but make sure you have some literal words in here so that the output actually makes sense to the user. HINTS: to compare two strings, use the notation [ $string1 == $string2 ]. To add one to a variable, use the notation X=$((X+1)). If every instruction is on a separate line, you do not need any semicolons. Test your script on /etc/fstab with the word defaults (7 occurrences should be found)
This is what I got so far, but it does not work right. It says it finds 0 occurrences of the word "defaults" in /etc/fstab. I am sure my code is wrong but can't figure out the problem. Help is appreciated.
count=0
echo “what word do you want to search for?: “
read two
for word in “cat $1”
do
if [ “$two” == “$word” ]; then
count=$((count+1))
fi
done
echo $two appeared $count times in $1

You need to use command substitution, you were looping over this string: cat first_parameter.
for word in $(cat "$1")
Better way to do this using grep, paraphrasing How do I count the number of occurrences of a word in a text file with the command line?
grep -o "\<$two\>" "$1" | wc -l

Related

Wordlist Generator in Bash

I am trying to create a wordlist consisting of the same password followed by a 4-digit numeric pin. The pin goes through every possible combination of 10,000 variations. The desired output should be like this:
UoMYTrfrBFHyQXmg6gzctqAwOmw1IohZ 1111
UoMYTrfrBFHyQXmg6gzctqAwOmw1IohZ 1112
UoMYTrfrBFHyQXmg6gzctqAwOmw1IohZ 1113
and so on.
I created a shell script that almost get this, but awk doesn't seem to like having a variable passed through it, and seems to just print out every combination when called. This is the shell script:
#!/bin/bash
# Creates 10,000 lines of the bandit24pass and every possible combination
# Of 4 digits pin
USER="UoMYTrfrBFHyQXmg6gzctqAwOmw1IohZ"
PASS=$( echo {1,2,3,4,5,6,7,8,9}{1,2,3,4,5,6,7,8,9}{1,2,3,4,5,6,7,8,9}{1,2,3,4,5,6,7,8,9} | awk '{print $I}' )
for I in {1..10000};
do
echo "$USER $PASS"
done
I though $I would translate to $1 for the first run of the loop, and increment upwards through each iteration. Any help would be greatly appreciated.
I though $I would translate to $1 for the first run of the loop, and increment upwards through each iteration.
No, command substitutions are expanded once; like, when you do foo=$(echo), foo is an empty line, not a reference to echo.
This whole task could be achieved by a single call to printf btw.
printf 'UoMYTrfrBFHyQXmg6gzctqAwOmw1IohZ %s\n' {1111..9999}
Tyr this
$echo $user
UoMYTrfrBFHyQXmg6gzctqAwOmw1IohZ
$for i in {1000..9999}; do echo $user $i; done;

If condition to check if two strings stored in a variable occur one after other in a file

$cat list
Hi
welcome
one
two
good evening
Value1="two"
value2="evening"
For above file values, output should be echo "values are present one after the other line"
Need to know the if condition command to check if both variable values occur one after the other in a file.
If both variable values occur one line after other in a file, then echo some statement.
for example:
$cat list
Hi
two
one
three
good evening
in above condition, both variable value are not present one after the other line so output should be echo "values are not present one after the other line"
With awk you could write something like this:
awk -F= '$1=="Value1"{l=NR}$1=="value2"&&NR==l+1{print "ok"}' file
#!/bin/bash
Value1="two"
value2="evening"
while read line; do
if [[ "$Value1" == *"$line"* ]];then
read anotherline
if [[ "$anotherline" == *"$value2"* ]];then
echo "values are present one after the other line"
fi
fi
done < list
If you want exact string match then remove * wildcards and use single brackets

Grep words containg 'n' number of letters given user input

I am trying to create a script (bash) that will take input (integer) from a user and grep all words containing that number of letters. I am okay with how grep basically works, but I am unsure how use input from user to determine the output
Here is what I started:
#!/bin/sh
echo " Content type: text/html"
echo
x=`expr $1`
I'm pretty sure the grep command would be as simple as grep^...integer from user$. Just don't know how to take use the user input. Thanks!
EDIT: I should have mentioned that "user input" would be entered as an argument (./script 6)
Run this script as ./script 6 and it will select all 6-letter words from the file text and display them:
#!/bin/sh
grep -Eo "\<[[:alpha:]]{$1}\>" text
Key parts of the regex:
\< signifies the start of a word.
[[:alpha:]]{$1} signifies $1 alphabetical characters. If you want an apostrophe, such as in don't, to be considered a valid word character, then add it inside the outer square backets like this: [[:alpha:]']{$1}
\> signifies the end of a word.
There are some limitations to grep's ability to understand human-language. For example, in the string don't, it considers the apostrophe to be a word boundary.
Example
I ran this script against the text of the question:
$ ./script.sh 9
basically
determine
mentioned
$ ./script.sh 10
containing
you can use read to accpet input from the user.
#!/bin/sh
echo $1 | grep ".\{$2\}"
now if yo call the script as ./script hello 5
The positional parameters $1 will be hello and $2 as 5
here the {m} matches lines with m lenght as . any character is matched for exactly m times

Line from bash command output stored in variable as string

I'm trying to find a solution to a problem analog to this one:
#command_A
A_output_Line_1
A_output_Line_2
A_output_Line_3
#command_B
B_output_Line_1
B_output_Line_2
Now I need to compare A_output_Line_2 and B_output_Line_1 and echo "Correct" if they are equal and "Not Correct" otherwise.
I guess the easiest way to do this is to copy a line of output in some variable and then after executing the two commands, simply compare the variables and echo something.
This I need to implement in a bash script and any information on how to get certain line of output stored in a variable would help me put the pieces together.
Also, it would be cool if anyone can tell me not only how to copy/store a line, but probably just a word or sequence like : line 1, bytes 4-12, stored like string in a variable.
I am not a complete beginner but also not anywhere near advanced linux bash user. Thanks to any help in advance and sorry for bad english!
An easier way might be to use diff, no?
Something like:
command_A > command_A.output
command_B > command_B.output
diff command_A.output command_B.output
This will work for comparing multiple strings.
But, since you want to know about single lines (and words in the lines) here are some pointers:
# first line of output of command_A
command_A | head -n 1
The -n 1 option says only to use the first line (default is 10 I think)
# second line of output of command_A
command_A | head -n 2 | tail -n 1
that will take the first two lines of the output of command_A and then the last of those two lines. Happy times :)
You can now store this information in a variable:
export output_A=`command_A | head -n 2 | tail -n 1`
export output_B=`command_B | head -n 1`
And then compare it:
if [ "$output_A" == "$output_B" ]; then echo 'Correct'; else echo 'Not Correct'; fi
To just get parts of a string, try looking into cut or (for more powerful stuff) sed and awk.
Also, just learing a good general purpose scripting language like python or ruby (even perl) can go a long way with this kind of problem.
Use the IFS (internal field separator) to separate on newlines and store the outputs in an array.
#!/bin/bash
IFS='
'
array_a=( $(./a.sh) )
array_b=( $(./b.sh) )
if [ "${array_a[1]}" = "${array_b[0]}" ]; then
echo "CORRECT"
else
echo "INCORRECT"
fi

Save one line of echo to variable in BASH?

Okay the answer to this may be really simple but I have been searching for a while and I can't figure it out. I have a variable called "tmessagef". The variable is formatted like:
value1*value2*vlaue3*value4*value5
The only part of the variable I want is value 5. I am currently using the following code but it only prints each value and doesn't save them to a variable:
OIFS=$IFS
IFS='*'
arr2=$tmessagef
for x in $arr2
do
echo "$x"
done
IFS=$OIFS
What I want to do is get the 5th line that the echo command produces and save that to a variable called "tmessage". How would I go about doing this?
Thanks in advance.
Array manipulation:
OIFS="$IFS" IFS='*' Y=($X)
x=${Y[${#Y[#]}-1]}
IFS="$OIFS"
For this very specific scenario (where you only want to extract the value at the very end), you can use parameter expansion
echo "${word##*\*}"
or assign it to a variable instead of using "echo".
Explanation:
## removes the longest substring anchored at the beginning that matches the pattern
* matches any number of any character
\* matches a literal asterisk
So basically, remove the longest substring that ends with an asterisk.
I believe mcalex's comment should answer it:
Change echo "$x" to tmessage="$x". At the end of the loop $val will contain the last value
IFS=* read -r _{,,,} tmessage _ <<<"$tmessagef"
or
[[ $tmessagef =~ ^(.*\*){4}(.*)\* ]]; tmessage=${BASH_REMATCH[2]}
Read http://mywiki.wooledge.org/BashFAQ/001 and the trillion other answers to this question.
Don't use echo for this. If you have output that needs saving, see: http://mywiki.wooledge.org/BashFAQ/002
Can you use cut?
VAL=`echo 'value1*value2*vlaue3*value4*value5' | cut -f5 -d'*'`

Resources