在 shell 变量中提取文件的最后一个和倒数第二个字符串
虽然它看起来与我之前的帖子相似,但目的不同。
udit@udit-Dabba ~/ah $ cat decrypt.txt
60 00 00 00 00 17 3a 20 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 02 *00 00 e0 f9 6a 61 61 6e
65 6b 61 68 61 6e 67 61 79 65 77 6f 64 69 6e* 00
00 00 03 29
我想在 shell 变量中提取文件的最后一个字符串(这里是 29
)
我尝试了这个...
size=`wc -w encrypt.txt`
awk -v size=$size 'BEGIN {RS=" ";ORS=" ";}' {if (NR>size-1 &&
NR < size+1)print $0}' decrypt.txt
输出: 29
但是当我稍微更改文件时..
udit@udit-Dabba ~/ah $ cat decrypt.txt
60 00 00 00 00 17 3a 20 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 02 *00 00 e0 f9 6a 61 61 6e
65 6b 61 68 61 6e 67 61 79 65 77 6f 64 69 6e* 00
65 6b 61 68 61 6e 67 61 00 00 03 29
输出: 03
为什么结果存在差异?
我是 awk 和 shell 功能的新手,所以我不确定这是否是正确的方法???
我认为应该有一些 grep,sed,awk
或任何其他 Linux 命令的变体可以解决我的问题,但我不知道。
请为此指导我。
提前致谢。
用途:
在 shell 脚本中创建两个变量,用于存储输入文件的最后一个和倒数第二个字符串。
限制:
每个输入文件的末尾都包含一个空行。
(就像上面提到的 file 一样,在文件内容之后还会有一个空行,就像按 ENTER
键一样,并且无法更改,因为它是在运行时通过 C 程序生成的。)
Although it is looking similar to my previous post but here purpose is different.
udit@udit-Dabba ~/ah $ cat decrypt.txt
60 00 00 00 00 17 3a 20 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 02 *00 00 e0 f9 6a 61 61 6e
65 6b 61 68 61 6e 67 61 79 65 77 6f 64 69 6e* 00
00 00 03 29
I want to extract last string of the file (here it is 29
) in a shell varaible
I tried this ...
size=`wc -w encrypt.txt`
awk -v size=$size 'BEGIN {RS=" ";ORS=" ";}' {if (NR>size-1 &&
NR < size+1)print $0}' decrypt.txt
Output :
29
But when I changed the file slightly ..
udit@udit-Dabba ~/ah $ cat decrypt.txt
60 00 00 00 00 17 3a 20 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 01 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 02 *00 00 e0 f9 6a 61 61 6e
65 6b 61 68 61 6e 67 61 79 65 77 6f 64 69 6e* 00
65 6b 61 68 61 6e 67 61 00 00 03 29
Output :
03
Why there is discrepency between the results ??
I am new to awk and shell features so I am not sure whether it is a right way to do so or not ???
I think there should be some variation of grep,sed,awk
or any other linux command which may solve my problem but I am not aware of it.
Please guide me for this.
Thanx in advance.
Purpose :
Make two variables in a shell script which should store last and second last strings of an input file.
Limitation :
Every input file contains a blank line at the end of file.
(Like in above mentioned file , after the file contents there would be one more blank line just like hitting ENTER
key and that can not be changed because it is being generated through a C program at run time.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
也许 grep 部分并不完美,也许应该改变。
编辑
是更好的解决方案 - 请参阅戈登·戴维森的评论。
Maybe the
grep
-part isn't perfect and maybe should change.Edit
is better solution - see Gordon Davisson's comment.
获取最后一个字段:
对于最后一行只有一个字段的情况,倒数第二个字段会比较棘手(因此您需要前一行的最后一个字段)。
如果文件只包含一个值,这会生成一个空白和最后一个值。如果根本没有值,它只会产生一个空白。
To get the last field:
The second last field is trickier for the case where there is just one field on the last line (so you need the last field from the line before).
This produces a blank and the last value if the file contains but one value. It produces just a blank if there are no values at all.
如果你认为记录分隔符是空格或换行符,那么你只需要保留最后2条记录。
If you consider the record separator to be space or newline, then you just need to keep the last 2 records.
当然,您可以通过多种方式切断最后一个字段:
naturally, you can cut off the last field in a variety of ways:
这是一个 tr/sed 解决方案:
仅限 Sed:
Here's a tr/sed solution:
Sed only:
如果您安装了“rev”实用程序,下面的一个将会很方便。假设空格是分隔符。
If you got 'rev' utility installed below one would be handy. Presuming that space is the delimiter.