finding specific pattern in linux [duplicate] - linux

This question already has answers here:
Print only matching word, not entire line through grep
(2 answers)
Closed 5 years ago.
I want to find specific pattern in all the files in a directory and copy them to another line
For E.g
I want to find LOG_WARNING in one file XYZ and copy them to another file.
LOG_WARNING (abc, xyz,("WARNING: Error in sending concurrent_ to pdm\n"));
command i have used is :
grep -rin "LOG_WARNING.*" file_name.c > output.txt
but it is not copying till the semicolon, please note that other texts are available in next line. I want to copy till ;(semi-colon)

grep -rh "LOG_WARNING" * > out.txt
This will match the pattern in all the files inside the directory.
Since you mentioned that the texts that are present after the ';' are on the next line, I have provided this command.
This will match the pattern and print the entire line, till the ';'.
Else,
try this
grep -roPh 'LOG_WARNING[^;]*;' * > out.txt

Related

grep weird behaviour in linux command line [duplicate]

This question already has answers here:
Using the star sign in grep
(12 answers)
Closed 1 year ago.
I have a file test.txt which contains this text data
# cat test.txt
*#=>*#
if I use grep to check if the string is in the file using this way
# grep "*#=>*#" test.txt
#
it returns nothing..
while if I grep a partial string search
# grep "*#=>" test.txt
# *#=>*#
it works correctly ..
Why in the first case do grep return nothing ?
The asterisk is special to grep, it means "the previous token is repeated zero or more times". So >* would match an empty string or > or >> etc.
To match an asterisk, backslash it in the pattern:
grep '\*#=>\*#' test.txt
(The first asterisk follows no token, so the backslash is optional.)

how to grep a string from a particular line? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I want to search for a String by navigating to a particular line, How to do this in shell scripting?
For example,
I have
this is the first line
this is the Second line
This is the Third line
Now here i would want to look for string "Third" by going to 3rd line.
Any help is appreciated, Thank you.
Try stringing together cat, sed, and grep.
sed '3!d' filename | grep Third
The unnamed or anonymous pipe (|) and redirection (<, >) are powerful features of many shells. They allow one to combine a set of commands to perform a more complex function.
In the case of this question there were two clear steps,
1) Operate on a specific line of a file (e.g. filter a file)
2) Search the output of the filter for a specific string
Recognizing that there were two steps is a strong indicator that two commands will need to be combined. Therefore, the problem can be solved by finding a solution to each step and then combining them in to one command with pipes and redirection.
If you know about the Stream Editor (sed), it may come to your mind when thinking about how to accomplish the first step of filtering the file. If not searching for, "linux get a specific line of a file" this OS question comes up high in the search results.
$ cat tmp.txt
this is the first line
this is the Second line
This is the Third. line
$ sed '3!d' tmp.txt
This is the Third. line
Knowing that grep can be search for lines with the string of interest the next challenge is to figure out how to get the output of sed as the input to grep. The pipe (|) solves this problem.
sed '3!d' filename | grep Third
Example output:
$ sed '3!d' tmp.txt | grep Third
This is the Third. line
$
Another powerful concept in shell scripting is the exit status. The grep command will set the exit status to 0 when a match is found and 1 when a match is not found. The shell stores the exit status in a special variable named $? (for bash). Therefore, one could use the exit status to conditionally determine the next step in the shell script. The example below does not implement conditions (like if, else). The example below shows the exit status value using the echo command.
$ sed '3!d' tmp.txt | grep Third
This is the Third. line
$ echo $?
0
$ sed '3!d' tmp.txt | grep third
$ echo $?
1
$

Excluding/ignoring (Not Deleting) first and last line based on line number [duplicate]

This question already has answers here:
Delete first and last line or record from file using sed
(5 answers)
Closed 4 years ago.
I'm using sed command to edit some text file.
How can i exclude the first and last line in each text file for being edited.
I went through sed gnu manual but i only found commands to match line ranges i.e. 1,$ or to exclude ranges i.e. 1,$!. i just need to exclude line # 1 and last line $. i'm not sure if its possible to select a range i.e 2, $-1?
Here's my code.
sed -e '1,$ s/.*/<p>&<\/p>/'' file.txt
You can't use maths in sed addresses. But you can tell sed to do nothing on the first and last line:
sed -e '1n; $n; s/.*/<p>&<\/p>/'
where n means "read the next line of input into the pattern space" (in case of the last line, it won't read anything).
(The two single quotes at the end of the expression are probably a typo, right?)

How to find the diffrence between 2 files [duplicate]

This question already has answers here:
Fast way of finding lines in one file that are not in another?
(11 answers)
Closed 4 years ago.
I have 2 txt files.
The first txt file contains something like this: direction:left, move:right
The second txt file contains something like this: direction:right, move:right
Note: on both txt files, everything is on one line.
I want to be able to get the difference between those two txt files. So in the example above, it would return "right".
I tried using grep, comm, and diff. Those didn't work, because instead of printing the exact difference it just printed the different line, I just want the different phrase.
How do I do this in bash?
Use grep -F -x -v -f fileB fileA | cut -d':' -f2.
This works by using each line in fileB as a pattern (-f fileB) and treating it as a plain string to match (not a regular regex) (-F). You force the match to happen on the whole line (-x) and print out only the lines that don't match (-v). Therefore you are printing out the lines in fileA that don't contain the same data as any line in fileB.
Then cut -d':' -f2 splits the string with : as the delimiter and gets the second value.

Check if an array element is in a file [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am writing a bash script to check if an array element is in a file.
For example:
I have an array of errors errors=("1234" "5678" "9999")
I have a file that contains patterns of strings
123400 452612 9999A0 1010EB
I am looking to loop over the file that contains the errors and check to see if any of the array elements matches any string pattern in the file. If it does then give me back the exact array pattern it matched in the file for further processing.
Any ideas on how I can do this?
Here's a way where you only need to invoke grep once:
$ grep -oFf <(printf "%s\n" "${errors[#]}") file
1234
9999
The -f option is to specify a file that contains the pattern. I use a process substitution to "contain" the patterns, one per line.
The -F option specifies plain-text matching: I assume your "errors" array won't contain regular expressions.
Sounds like you just want a loop:
for error in "${errors[#]}"; do
if grep -qE "(^| )$error( |\$)" file; then
# $error was found in the file
fi
done
This matches the error preceded by the start of the line or a space, and followed by a space or the end of the line.
I made an effort to not match appearances of the errors within substrings but if you don't care, then you could change the grep command to this:
grep -qF "$error" file
This will return success if the error string occurs anywhere on the line.
The script goes like this,
#/bin/bash
errors=("1234" "5678" "9999")
for error in "${errors[#]}"
do
grep -o "$error" file
done
For a sample file,
$ cat file
123400 452612 9999A0 1010EB
The script produces an output
$ ./script.sh
1234
9999
meaning the above two keys from the array have matched in the file. The -o flag in grep is to identify only the matching parts from the array. An excerpt from the man grep page.
-o, --only-matching
Print only the matched (non-empty) parts of a matching line, with each such part on a separate output line.

Resources