简体   繁体   English

通过检查多个条件来提取字符串的一部分-Shell脚本

[英]Extract portion of string by checking multiple conditions - Shell script

First time I am trying any shell script, the task which I need to perform is below: 第一次尝试任何shell脚本时,我需要执行的任务如下:

Out put of this command is below -> cat /sys/kernel/debug/spmi/spmi-0/data 该命令的输出如下-> cat /sys/kernel/debug/spmi/spmi-0/data

00800 00 03 03 00 01 01 00 C0 10 00 00 00 00 20 00 00
00810 00 03 03 03 00 03 03 00 00 00 00 00 00 00 00 00
00820 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00830 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00840 0F 07 01 00 0F 07 04 00 0F 07 07 80 0F 07 04 00
00850 0F 07 04 00 0F 03 08 00 00 00 01 80 00 00 00 00
00860 00 00 00 80 00 00 04 80 00 00 04 00 00 00 00 00
00870 0F 00 00 00 02 04 00 00 00 00 00 00 00 00 00 00
00880 FE 00 40 00 00 00 00 00 05 00 20 00 01 00 00 00

I need to check the value of first row and 14th column value and extract it. 我需要检查first row and 14th column value and extract it. That value either can be 00 or 20 . 该值可以是00 or 20 On the basis of that value i have to change the dir name which i think i can take care. 在该值的基础上,我必须更改目录名称,我认为我可以注意。

can any body help me out in this as i have googled from https://unix.stackexchange.com/questions/37313/how-do-i-grep-for-multiple-patterns but could not make it. 有没有人可以帮我解决这个问题,因为我从https://unix.stackexchange.com/questions/37313/how-do-i-grep-for-multiple-patterns进行了Google搜索,但无法成功。

It is very easy! 这很容易!

$ head /sys/kernel/debug/spmi/spmi-0/data -n 1| cut -d " " -f 14

Explanation: 说明:

  1. head /sys/kernel/debug/spmi/spmi-0/data -n 1 it will output first line from given file. head /sys/kernel/debug/spmi/spmi-0/data -n 1它将从给定文件输出第一行。

  2. cut -d " " -f 14 will select 14th field/column (where fields get delimited by " " -Space cut -d " " -f 14将选择第14个字段/列(其中的字段由" " -Space

Edit 编辑

Usage 用法

value=`head /sys/kernel/debug/spmi/spmi-0/data -n 1| cut -d " " -f 14`
echo $value

awk - solution awk-解决方案

Alternate solution provided by twalberg twalberg提供的替代解决方案

awk 'NR==1{print $14}' /sys/kernel/debug/spmi/spmi-0/data

Create a pattern which you need to search like in your case it's 00 or 20 as i understand. 创建一个模式,您需要搜索,就像我理解的那样是00或20。 pattern="00|20" pattern =“ 00 | 20”

cat /sys/kernel/debug/spmi/spmi-0/data | 猫/ sys /内核/调试/ spmi / spmi-0 /数据| grep -E “${pattern}” grep -E“ $ {pattern}”

Then you can use cut -d " " -f2 for getting specific colum This will give the output based on your pattern, hope i understood your problem correct. 然后,您可以使用cut -d“” -f2获取特定的列。这将根据您的模式提供输出,希望我能正确理解您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM