如何从文本块中提取特定行并将其存储到字符串变量中? [英] How do you extract a specific line from block of text and store them into string variables?

查看:59
本文介绍了如何从文本块中提取特定行并将其存储到字符串变量中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[Bash脚本] -我正在使用LG设备,并且试图获取级联的设备ID字符串.我已经弄清楚了如何连接字符串,但是在从整体输出中提取字符串时遇到了麻烦.从引导加载程序中,运行 fastboot oem device-id 并获得类似的输出."|"之后的所有内容是实际输出的,我只包含#|" 作为行引用.

[Bash Scripting] - I'm working with an LG device and I'm trying to obtain a concatenated Device ID string. I've figured out how to concatenate strings, but I'm having trouble extracting the strings from the overall output. From the bootloader, I run fastboot oem device-id and get an output like this. Everything after the "|" was actually outputed, I only included the "# | " as line references.

1 | ...
2 | (bootloader) ----------------------------------
3 | (bootloader) Device-ID
4 | (bootloader) ABCDEFGH12345678ABCDEFGH12345678
5 | (bootloader) 87654321HGFEDCBA87654321HGFEDCBA
6 | (bootloader) ----------------------------------
7 | OKAY [  0.070s]
8 | finished. total time: 0.070s

似乎有点受 fastboot oem device-id 的限制.在命令中使用grep只会显示相同的内容.( fastboot oem device-id | grep bootloader -m 2 )这应该仅显示前两行(示例).我还尝试将所有内容输出到一个外部文本文件(并从那里开始工作),但是它创建了一个文本空白文件.( fastboot OEM设备ID >> test.txt )

Seems like fastboot oem device-id is a little restricted. Using grep with the command only displayed the same content. (fastboot oem device-id | grep bootloader -m 2) This should only display the first 2 lines (example). I also tried to output all the content to an external text file (and work from there), but it created a text blank file. (fastboot oem device-id >> test.txt)

理想情况下,我想提取第4行和第5行的内容,并将它们分别存储到单独的字符串变量- stringA stringB 中.字符串A和B应该输出以下内容

Ideally, I would like to extract the contents of Lines 4 and 5, and store them into separate string variables - stringA and stringB, respectively. Strings A and B should output the following

(stringA) <-- "(bootloader) ABCDEFGH12345678ABCDEFGH12345678" (Line 4)
(stringB) <-- "(bootloader) 87654321HGFEDCBA87654321HGFEDCBA" (Line 5)

然后我要像这样将它们连接起来.

Then I would concatenate them like this.

echo "Device ID:  $(stringA:13:32)$(stringB:13:32)"

从第13列(以上)开始,将排除(引导加载程序)_ 部分,仅保留所需的字符串.最后,回声的输出(上面)应该像这样.

Starting at column 13 (above) would exclude the (bootloader)_ part and leave only the desired string. In the end, the output of the echo (above) should look like this.

Device ID:  ABCDEFGH12345678ABCDEFGH1234567887654321HGFEDCBA87654321HGFEDCBA

奇怪的是,我能够使用不同的fastboot命令( fastboot设备)来完成上述所有操作.还有什么其他方法可以从第4行和第5行提取字符串并将它们存储到两个单独的字符串变量中?

Strangely, I was able to do all of the above using a different fastboot command (fastboot devices). What other ways are there to extract the strings from Lines 4 and 5 (above) and store them into two separate string variables?

推荐答案

使用awk

$ fastboot oem device-id | awk 'NR==4{a=$2} NR==5{b=$2; printf "Device ID:  %s%s\n",a,b; exit}'
Device ID:  ABCDEFGH12345678ABCDEFGH1234567887654321HGFEDCBA87654321HGFEDCBA

工作原理:

  • NR == 4 {a = $ 2}

当我们到达第四行时,将第二个字段保存在变量 a 中.

When we get to the fourth line, save the second field in the variable a.

NR == 5 {b = $ 2;printf设备ID:%s%s \ n",a,b;退出}

当我们到达第五行时,将第二个字段保存在变量 b 中并写入输出行.

When we get to the fifth line, save the second field in variable b and write the output line.

$ fastboot oem device-id | sed -n 's/.* //; 4h; 5{H; x; s/\n//; s/^/Device ID:  /p}'
Device ID:  ABCDEFGH12345678ABCDEFGH1234567887654321HGFEDCBA87654321HGFEDCBA

工作原理:

  • -n

告诉sed除非我们明确要求不要打印任何内容.

Tell sed not to print anything unless we explicitly ask it to.

s/.*//

找到直到行尾的所有字符,包括最后一行,然后将其删除.

Find all characters up to and including the last space on the line and delete them.

`4h

如果我们在第四行,请保留保留空间中的剩余内容.

If we are on the fourth line, save what's left to the hold space.

5 {H;X;s/\ n//;s/^/设备ID:/p}

如果我们在第五行,请将行的剩余内容附加到保留空间( H ).将保留空间与模式空间( x )交换.删除换行符的任何换行符( s/\ n//).将字符串 Device ID:添加到模式空间的开头并打印( s/^/Device ID:/p ).

If we are on the fifth line, append what's left of the line to the hold space (H). Swap the hold space with the pattern space (x). Remove the newline any newline character (s/\n//). Add the string Device ID: to the beginning of the pattern space and print it (s/^/Device ID: /p).

$ fastboot oem device-id | grep -oE '[[:alnum:]]{32}$' | { read a; read b; echo "Device ID:  $a$b"; }
Device ID:  ABCDEFGH12345678ABCDEFGH1234567887654321HGFEDCBA87654321HGFEDCBA

工作原理:

  • grep -oE'[[:alnum:]] {32} $'

如果fastboot中的行以32个字母数字字符结尾,请打印这些字符.

If the line from fastboot ends with 32 alphanumeric characters, print those characters.

更详细地, -o 选项告诉 grep 仅打印行中匹配的部分. -E 告诉grep使用扩展的正则表达式. [[[:alnum:]] {32} 匹配32个字母数字字符. $ 仅在行尾匹配.

In more detail, the -o option tells grep to only print the matching part of a line. -E tells grep to use extended regex. [[:alnum:]]{32} matches 32 alphanumeric characters. $ matches only at the end of the line.

阅读a;读b;回声设备ID:$ a $ b";

来自 grep 的第一条输出行被读取到变量 a 中.第二行输出被读取到变量 b 中.然后,将所需的行回显到标准输出.

The first output line from grep is read into variable a. The second output line is read into variable b. Then, the desired line is echoed to standard output.

这篇关于如何从文本块中提取特定行并将其存储到字符串变量中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆