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

$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

Related

How to read in variables from multi-line file?

I am trying to read from a file that is multiple lines with multiple variables per line. There are four variables: item, price, max_qty, and min_qty. The file looks like this:
item price
min_qty max_qty
I have tried:
while IFS=' ' read -r first second third fourth; do
echo "$first $second $third $fourth
done
This does not work. I want the output to be:
item price min_qty max_qty
I have also thought about somehow replacing the new lines with spaces and then reading from that line. I don't want to actually change the file though.
read twice:
while read -r first second && read -r third fourth; do
echo "$first $second $third $fourth"
done < file
If it is true that "the file looks like this" (two lines, two values per line, not a larger file with many pairs of lines like that), then you can do it with a single read by using the -d option to set the line delimiter to empty:
read -r -d '' first second third fourth <file
echo "$first $second $third $fourth"
The code prints 'item price min_qty max_qty' with the example file.
If you have errexit or the ERR trap set, the program will exit immediately, and silently, immediately after the read is executed. See Bash ignoring error for a particular command for ways of avoiding that.
The code will work with the default value of IFS (space+tab+newline). If IFS could be set to something different elsewhere in the program, set it explicitly with IFS=$' \t\n' read -r -d '' ....
The code will continue to work if you change the file format (all values on one line, one value per line, ...), assuming that it still contains only four values.
The code won't work if the file actually contains many line pairs. In that case the "read twice" solution by oguz ismail is good.

linux script to find specific words in file names

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

Understanding and Clarification on AIX and Batch File

I am new to AIX and I have trouble understanding the codes stated in the shell script as shown below, I have a few questions.
if [ "$OutChlName" != "" ] ; then
echo START CHANNEL \($OutChlName\)
fi
For the first line, what does the "" mean, does it mean null?
\($OutChlName\) - is there any way to convert this to a batch file format.
Is it right to say that fi is the end tag of if?
Thank you.
The echo is only wanted when the variable OutChkName is filled.
The string is compared with an empty string.
When you read man test, you can find the alternative if [ -n "$OutChlName" ].
echo with double quotes
Within quotes you do not need the backslashes.
echo "START CHANNEL ($OutChlName)"
My echo behaves different when the OutChlName variable has special characters like newlines or *. I think my syntax is a slight bugfix, but when you do not want to change the original behaviour, you can use
echo "START CHANNEL ("$OutChlName")"
Using backslashes is also a valid syntax (batch format).
fi ends if
Also esac ends case, and done ends do (Odd, it should have been od).

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