[英]Simple linux script help
I have a text file with the following structure: 我有一个具有以下结构的文本文件:
text1;text2;text3;text4
...
I need to write a script that gets 2 arguments: the column we want to search in and the content we want to find. 我需要编写一个包含2个参数的脚本:我们要搜索的列和我们要查找的内容。
So the script should output only the lines ( WHOLE LINES! ) that match content(arg2) found in column x(arg1). 因此,脚本应仅输出与在x(arg1)列中找到的content(arg2)匹配的行 ( WHOLE LINES! )。
I tried with egrep and sed, but I'm not experienced enough to finish it. 我尝试使用egrep和sed,但是我没有足够的经验来完成它。 I would appreciate some guidance... 我希望得到一些指导...
Given your added information of needing to output the entire line, awk
is easiest: 给定您需要输出整行的附加信息, awk
最简单:
awk -F';' -v col=$col -v pat="$val" '$col ~ pat' $input
Explaining the above, the -v
options set awk
variables without needing to worry about quoting issues in the body of the awk
script. 解释上述内容后, -v
选项设置了awk
变量,而不必担心在awk
脚本主体中引用问题。 Pre-POSIX versions of awk
won't understand the -v
option, but will recognize the variable assignment without it. 预POSIX版本的awk
不会明白的-v
选项,但会认识到没有它的变量赋值。 The -F
option sets the field separator. -F
选项设置字段分隔符。 In the body, we are using a pattern with the default action (which is print
); 在主体中,我们使用具有默认操作的模式(即print
); the pattern uses the variables we set with -v
for both the column ( $
there is awk
's "field index" operator, not a shell variable) and the pattern (and pat
can indeed hold an awk
-style regex). 模式使用我们用-v
为列设置的变量( $
有awk
的“字段索引”运算符,而不是shell变量)和模式( pat
确实可以容纳awk
风格的正则表达式)。
cat text_file.txt| cut -d';' column_num | grep pattern
It prints only the column that is matched and not the entire line. 它仅打印匹配的列,而不打印整个行。 let me think if there is a simple solution for that. 让我考虑一下是否有一个简单的解决方案。
Python 蟒蛇
#!/usr/bin/env python
import sys
column = 1 # the column to search
value = "the data you're looking for"
with open("your file","r") as source:
for line in source:
fields = line.strip().split(';')
if fields[column] == value:
print line
grep -B1 -i "string from previous line" |grep -iv 'check string from previous line' |awk -F" " '{print $1}'
这将打印您的行。
There's also a solution with egrep
. egrep
也有一个解决方案。 It's not a very beautiful one but it works: 它不是一个非常漂亮的,但是它可以工作:
egrep "^([^;]+;){`expr $col - 1`}$value;([^;]+;){`expr 3 - $col`}([^;]+){`expr 4 - $col`}$" filename
or even shorter: 甚至更短:
egrep "^([^;]+;){`expr $col - 1`}$value(;|$)" filename
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.