Bash Shellscript Column Check Error Handling

时间:2018-09-18 20:28:47

标签: bash shell awk text-processing

I am writing a Bash Shellscript. I need to check a file for if $value1 contains $value2. $value1 is the column number (1, 4, 5 as an example) and $value2 ($value2 can be '03', '04' , '09' etc) is the String I am looking for. If the column contains the $value2 then perform a move of the file to an error directory. I was wondering what is the best approach to this. I was thinking awk or is there another way?

$value1 and $value2 are stored in a config file. I have control over what format I can use. Here's an example. The file separator is Octal \036. I just depicted with | below.

Example

$value1=5
$value2=04

Input example1.txt

 example|42|udajha|llama|04
 example|22|udajha|llama|02 

Input example2.txt

example|22|udajha|llama|02 

Result move example1.txt to /home/user/error_directory and example2.txt stays in current directory (nothing happens)

2 个答案:

答案 0 :(得分:2)

awk can report out which files meet this condition:

awk -F"|" -v columnToSearch=$value1 -v valueToFind=$value2 '$columnToSearch==valueToFind{print FILENAME}' example1.txt example2.txt

Then you can do your mv based on that.

Example using a pipe to xargs (with smaller variable names since you get the idea by now):

awk -F"|" -v c=$value1 -v v=$value2 '$c==v{print FILENAME}' example1.txt example2.txt | xargs -I{} mv -i {} /home/user/error_directory

答案 1 :(得分:0)

如果要编写bash shell脚本,则可以使用cut按列将其分解。 确实有太多选择,这取决于您要完成什么。

根据我的数据经验,我将使用冒号而不是竖线,因为它使我可以避免使用“ cut”命令进行转义。

将数据文件更改为:

[root@]# cat mysript.sh
    #!/bin/sh -x
one=`cat example1.txt | cut -d: -f5`
two=`cat example2.txt | cut -d: -f5`

for i in $one
do
if [ $i -eq $two ]
     then
      movethis=`grep $two example1.txt`
      echo $movethis >> /home/me/error.txt
fi
done

cat /home/me/error.txt
[root@]# ./mysript.sh
++ cat example1.txt
++ cut -d: -f5
+ one='04
02 '
++ cat example2.txt
++ cut -d: -f5
+ two=02
+ for i in '$one'
+ '[' 04 -eq 02 ']'
+ for i in '$one'
+ '[' 02 -eq 02 ']'
++ grep 02 example1.txt
+ movethis='example:22:udajha:llama:02 '
+ echo example:22:udajha:llama:02
+ cat /home/me/error.txt
example:22:udajha:llama:02

我会这样写:(添加-x以便您可以看到处理过程,但是在您的代码中您不需要这样做。)

select usrs.id, usrs.name, opts.ts
from schema.table as usrs
inner join rvv.fbm as opts 
        on opts.id = usrs.id and opts.name = 'qr_optin'
where (name like '%t%' and name like '%2018%') or
      (name like '%t%' and name like '%2017%')

您可以使用任何实时命令来移动内容。触摸,cp,mv,在那里您要使用什么。