![](/img/trans.png)
[英]how to use Linux command Sort to sort the text file according to 4th column, numeric order?
[英]How to check that the 4th character in a file is 'a' using linux grep command
为了找到第二个字符,它是grep -e'^。[aA]'。 那第四个角色会是什么呢? 我尝试了grep -e'^ ... [aA]'。 但它出了错。
grep
逐行处理输入。 如果a
或A
是任意行上的第二个字符,则^.[aA]
为true。
您可以将grep
与head
结合使用以仅检查第一行:
head -n1 filename | grep '^...[aA]'
但是它对于第一行少于四个字符的文件仍然不起作用:
x
ya
要真正检查文件中的第四个字符, grep
并不是最好的工具。
#! /bin/bash
read -N4 chars < filename
if [[ "${chars:3:1}" == [aA] ]] ; then
echo Found
fi
但是,如果您尝试了足够的努力,仍然可以使用它。 例如,使用tr
用空格替换换行符,然后就可以运行grep了:
tr '\n' ' ' < filename | grep '^...[aA]'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.