[英]How to search the full string in file which is passed as argument in shell script?
i am passing a argument and that argument i have to match in file and extract the information.我正在传递一个参数,我必须在文件中匹配该参数并提取信息。 Could you please how I can get it?
请问我怎样才能得到它?
Example:例子:
I have below details in file-我在文件中有以下详细信息-
iMedical_Refined_load_Procs_task_id=970113
HV_Rawlayer_Execution_Process=988835
iMedical_HV_Refined_Load=988836
DHS_RawLayer_Execution_Process=988833
iMedical_DHS_Refined_Load=988834
If I am passing 'hv' as argument so it should to pick 'iMedical_HV_Refined_Load' and give the result - '988836'如果我将“hv”作为参数传递,那么它应该选择“iMedical_HV_Refined_Load”并给出结果 - “988836”
If I am passing 'dhs' so it should pick - 'iMedical_DHS_Refined_Load' and give the result = '988834'如果我传递 'dhs',那么它应该选择 - 'iMedical_DHS_Refined_Load' 并给出结果 = '988834'
I tried below logic but its not giving the result correctly.我尝试了以下逻辑,但没有正确给出结果。 What Changes I need to do-
我需要做什么改变-
echo $1 | tr a-z A-Z
g=${1^^}
echo $g
echo $1
val=$(awk -F= -v s="$g" '$g ~ s{print $2}' /medaff/Scripts/Aggrify/sltconfig.cfg)
echo "TASK ID is $val"
Assuming your matching criteria is the first string after delimiter _
and the output needed is the numbers after the =
char, then you can try this sed
假设你的匹配条件是分隔符
_
之后的第一个字符串,需要的output是=
字符之后的数字,那么你可以试试这个sed
$ sed -n "/_$1/I{s/[^=]*=\(.*\)/\1/p}" input_file
$ read -r input
hv
$ sed -n "/_$input/I{s/[^=]*=\(.*\)/\1/p}" input_file
988836
$ read -r input
dhs
$ sed -n "/_$input/I{s/[^=]*=\(.*\)/\1/p}" input_file
988834
If I'm reading it right, 2 quick versions -如果我没看错的话,有 2 个快速版本 -
$: cat 1
awk -F= -v s="_${1^^}_" '$1~s{print $2}' file
$: cat 2
sed -En "/_${1^^}_/{s/^.*=//;p;}" file
Both basically the same logic.两者的逻辑基本相同。
In pure bash -在纯 bash -
$: cat 3
while IFS='=' read key val; do [[ "$key" =~ "_${1^^}_" ]] && echo "$val"; done < file
That's a lot less efficient, though.但这效率要低得多。
If you know for sure there will be only one hit, all these could be improved a bit by short-circuit exits, but on such a small sample it won't matter at all.如果您确定只会命中一次,那么所有这些都可以通过短路退出来稍微改善,但对于如此小的样本,这根本无关紧要。 If you have a larger dataset to read, then I strongly suggest you formalize your specs better than "in this set I should get...".
如果您有更大的数据集要阅读,那么我强烈建议您将规范形式化,而不是“在这个集合中我应该得到……”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.