开发者

Simple linux script help

开发者 https://www.devze.com 2023-02-25 03:00 出处:网络
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.

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.

So the script should output only the lines (WHOLE LINES!) that match content(arg2) found in column x(arg1).

I tried with egre开发者_运维技巧p and sed, but I'm not experienced enough to finish it. I would appreciate some guidance...


Given your added information of needing to output the entire line, awk is easiest:

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. Pre-POSIX versions of awk won't understand the -v option, but will recognize the variable assignment without it. The -F option sets the field separator. In the body, we are using a pattern with the default action (which is 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).


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


There's also a solution with 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


grep -B1 -i "string from previous line" |grep -iv 'check string from previous line' |awk -F" " '{print $1}'

This will print your line.

0

精彩评论

暂无评论...
验证码 换一张
取 消