Shell script to check if a paragraph/stream of lines exist in a file
Typically , these are not the same.
The grep var $result, contains new line (\n) characters inside, while the $line contains spaces.
If you set IFS=$"\n" before echo $result, you will be able to see the difference between them.
I had to insert some \n to $line (in the correct position) and now works fine:
#!/bin/bash
result=$(cat test.log | grep -A 2 "Published 1EO's")
IFS=$"\n"
echo $result
line=$(echo -e "Published 1EO's\nSave completed\nTrade saving save successful for trade 56945458|220841|b for MCR: CMDTY from source:ICE Tradecapture API retry count: 0 (From this line we check Company Name – CMDTY)")
echo "----------------------------------"
#echo $line | grep "\b$result\b"
echo $line
unset IFS
if [[ $line = $result ]]; then
echo "match"
else
echo "does not match"
fi
Result:
$./bashtest.sh
Published 1EO's
Save completed
Trade savi g save successful for trade 56945458|220841|b for MCR: CMDTY from source:ICE Tradecapture API retry cou t: 0 (From this li e we check Compa y Name – CMDTY)
----------------------------------
Published 1EO's
Save completed
Trade savi g save successful for trade 56945458|220841|b for MCR: CMDTY from source:ICE Tradecapture API retry cou t: 0 (From this li e we check Compa y Name – CMDTY)
match
Shell script to check if a paragraph exist in a file
With sed:
$ output=$(sed -n "/Published 3EO's/N;{/Save completed/N;{/Trade saving save successful for 123/p}}" file)
$ echo "$output"
Published 3EO's
Save completed
Trade saving save successful for 123
Shell script to check if a paragraph exist in a file
With sed:
$ output=$(sed -n "/Published 3EO's/N;{/Save completed/N;{/Trade saving save successful for 123/p}}" file)
$ echo "$output"
Published 3EO's
Save completed
Trade saving save successful for 123
How can I read words (instead of lines) from a file?
The read
command by default reads whole lines. So the solution is probably to read the whole line and then split it on whitespace with e.g. for
:
#!/bin/sh
while read line; do
for word in $line; do
echo "word = '$word'"
done
done <"myfile.txt"
bash text search: find if the content of one file exists in another file
try grep
cat b.txt|grep -f a.txt
Read n lines at a time using Bash
This is harder than it looks. The problem is how to keep the file handle.
The solution is to create another, new file handle which works like stdin
(file handle 0) but is independent and then read from that as you need.
#!/bin/bash
# Create dummy input
for i in $(seq 1 10) ; do echo $i >> input-file.txt ; done
# Create new file handle 5
exec 5< input-file.txt
# Now you can use "<&5" to read from this file
while read line1 <&5 ; do
read line2 <&5
read line3 <&5
read line4 <&5
echo "Four lines: $line1 $line2 $line3 $line4"
done
# Close file handle 5
exec 5<&-
Match two strings in one line with grep
You can use
grep 'string1' filename | grep 'string2'
Or
grep 'string1.*string2\|string2.*string1' filename
Related Topics
How to Run a Mips Binary on X86 Platform
How to Grep One String Occuring Multiple Times from Same File
Linux Kernel "Historical" Git Repository with Full History
Check Whether a Certain File Type/Extension Exists in Directory
Does Linux Kill Background Processes If We Close the Terminal from Which It Has Started
Fata[0000] Get Http:///Var/Run/Docker.Sock/V1.17/Version: Dial Unix /Var/Run/Docker.Sock
Find File Then Cd to That Directory in Linux
Docker Load and Save: "Archive/Tar: Invalid Tar Header"
Copying Files Based on Modification Date in Linux
About Process Control Block in Os
Linux Script Extract Information from Excel to Create Users
How to Recompile Just a Single Kernel Module
I Get "Dquote>" as a Result of Executing a Program in Linux Shell
Linux: Merging Multiple Files, Each on a New Line
Surround All Lines in a Text File with Quotes ('Something')