Issue
I have file named file.txt, i am trying to read string first search which matches my pattern search from the file. The problem here is with my command entire line is printed. where i am looking for that variable which matches with search pattern with its full variable name, in this example it is warning_duration=""; where my search pattern is duration *=.i have posted the command i tried to read result also with expected result.
Please help !!!
file.txt
warning_type="";warning_threshold="";warning_duration="";oemhp_power_micro_ver="";previous_warning_threshold="";
duration=19;
duration =1;
commands i tried :
cat file.txt | grep -m1 "duration *="
warning_type="";warning_threshold="";warning_duration="";oemhp_power_micro_ver="";previous_warning_threshold="";
cat file.txt | grep -oP -m1 "duration *="
duration=
expected result:-
warning_duration="";
Solution
You may use this grep
command:
grep -m1 -woE "[_[:alnum:]]*duration *=[^;]*" file
warning_duration=""
Details:
-o
: Only show matches-E
: Enable extended regex-w
: Word search[_[:alnum:]]*
: Match 0 or more of a_
or alphanumeric charactersduration *=
: Matchduration
followed by 0 or more spaces and=
[^;]*
: Match 0 or more of any character that are not;
Answered By - anubhava Answer Checked By - Terry (WPSolving Volunteer)