简体   繁体   English

Unix:使用grep或awk-需要在特定字段上查找字母的出现,将行打印出来

[英]unix: Using grep or awk - need to find occurrence of a letter on specific field print the line

I have several fixed files and need to quickly scan them for occurrence of letter L on field 159 and print. 我有几个固定文件,需要快速扫描它们以查看在字段159上是否出现字母L并进行打印。 Example: 例:

123456ABCDEF23253657LA00000000MMMKSNS
123456ABCDEF23253657BA00000000MMMKSNS
123456ABCDEF23253657LA00000000MMMKSNS
123456ABCDEF23253657LA00000000MMMKSNS
12345        3253657LA00000000MMMKSNS

I tried grep -E '^.{21} L' but doesn't find for a 180 fixed length grep -E '^.{159} L' 我尝试了grep -E '^.{21} L'但是找不到180固定长度的grep -E '^.{159} L'

With awk you can print the substring. 使用awk,您可以打印子字符串。 See nim's answer for syntax. 有关语法,请参见nim的答案。

while read line; \
  do echo "$line" | awk '{print substr($0,21,1)}'| \
  grep -q "L"; [ $? -eq 0 ] && \
  echo "$line"; \
  done < "${file}"

Output: 输出:

123456ABCDEF23253657LA00000000MMMKSNS
123456ABCDEF23253657LA00000000MMMKSNS
123456ABCDEF23253657LA00000000MMMKSNS
12345        3253657LA00000000MMMKSNS

You were very close, 你很亲近

echo "123456ABCDEF23253657LA00000000MMMKSNS
123456ABCDEF23253657BA00000000MMMKSNS
123456ABCDEF23253657LA00000000MMMKSNS
123456ABCDEF23253657LA00000000MMMKSNS
12345        3253657LA00000000MMMKSNS" | grep -E '^.{20}L'

output 输出

123456ABCDEF23253657LA00000000MMMKSNS
123456ABCDEF23253657LA00000000MMMKSNS
123456ABCDEF23253657LA00000000MMMKSNS
12345        3253657LA00000000MMMKSNS

Note that having a space between {20} L throws off the position, AND that as your L is in Position 21, you skip 20 chars. 请注意,在{20} L之间留一个空格将抛出该位置,并且当L位于位置21时,将跳过20字符。

IHTH 高温超导

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

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