如果条件不匹配,awk删除行并复制到日志文件?

时间:2021-08-14 19:26:12

Lets assume we have a string like this:

让我们假设我们有一个像这样的字符串:

383;06;55.270989;144991494994851A5485AA54J7HH337H3H33HT570BBG7BBGBT07BT7R55U155U5IR75I79QQ9SQQ9Q597Q57S229122928S4284;N

But down the file we encounter something like this:

但是在文件中我们会遇到这样的事情:

383;06;55.270989;||<FD><F0>p|/x|<A9>|<E2>|,|<F7>|l|L@<F5>q|I|b%<EB><AB><C2>l|F|<D7>%|<C0><E4>wy||z<BE>|;|b<E5>&x"h<D1>e|j|E|c|<F4><E1>
<C2>4^|Q|<EF>H|<E0>2t<C2>6'<E4><C7>||Z|<E0>q|9d|;N

Is there a way to run this and say if the txt file do not have x number of fields (separator ;) or remove it from file and place it in a log file?

有没有办法运行它,并说如果txt文件没有x个字段(分隔符;)或从文件中删除它并将其放在日志文件中?

Edit: this method also include having a log for the data that is being removed for later analysis

编辑:此方法还包括为正在删除的数据提供日志以供以后分析

1 个解决方案

#1


2  

To output two files you can redirect print statements in the case of lines you want to remove. Write the lines you want to keep to a tmp file and copy back to your input:

要输出两个文件,您可以在要删除的行的情况下重定向打印语句。将要保留的行写入tmp文件并复制回输入:

$ cat input
383;06;55.270989;144991494994851A5485AA54J7HH337H3H33HT570BBG7BBGBT07BT7R55U155U5IR75I79QQ9SQQ9Q597Q57S229122928S4284;N
383;06;55.270989;||<FD><F0>p|/x|<A9>|<E2>|,|<F7>|l|L@<F5>q|I|b%<EB><AB><C2>l|F|<D7>%|<C0><E4>wy||z<BE>|;|b<E5>&x"h<D1>e|j|E|c|<F4><E1><C2>4^|Q|<EF>H|<E0>2t<C2>6'<E4><C7>||Z|<E0>q|9d|;N

$ awk -F\; 'NF != 5 { print > "logfile.log"; next }1' input > tmp; mv tmp input

$ cat logfile.log
383;06;55.270989;||<FD><F0>p|/x|<A9>|<E2>|,|<F7>|l|L@<F5>q|I|b%<EB><AB><C2>l|F|<D7>%|<C0><E4>wy||z<BE>|;|b<E5>&x"h<D1>e|j|E|c|<F4><E1><C2>4^|Q|<EF>H|<E0>2t<C2>6'<E4><C7>||Z|<E0>q|9d|;N

$ cat input
383;06;55.270989;144991494994851A5485AA54J7HH337H3H33HT570BBG7BBGBT07BT7R55U155U5IR75I79QQ9SQQ9Q597Q57S229122928S4284;N

#1


2  

To output two files you can redirect print statements in the case of lines you want to remove. Write the lines you want to keep to a tmp file and copy back to your input:

要输出两个文件,您可以在要删除的行的情况下重定向打印语句。将要保留的行写入tmp文件并复制回输入:

$ cat input
383;06;55.270989;144991494994851A5485AA54J7HH337H3H33HT570BBG7BBGBT07BT7R55U155U5IR75I79QQ9SQQ9Q597Q57S229122928S4284;N
383;06;55.270989;||<FD><F0>p|/x|<A9>|<E2>|,|<F7>|l|L@<F5>q|I|b%<EB><AB><C2>l|F|<D7>%|<C0><E4>wy||z<BE>|;|b<E5>&x"h<D1>e|j|E|c|<F4><E1><C2>4^|Q|<EF>H|<E0>2t<C2>6'<E4><C7>||Z|<E0>q|9d|;N

$ awk -F\; 'NF != 5 { print > "logfile.log"; next }1' input > tmp; mv tmp input

$ cat logfile.log
383;06;55.270989;||<FD><F0>p|/x|<A9>|<E2>|,|<F7>|l|L@<F5>q|I|b%<EB><AB><C2>l|F|<D7>%|<C0><E4>wy||z<BE>|;|b<E5>&x"h<D1>e|j|E|c|<F4><E1><C2>4^|Q|<EF>H|<E0>2t<C2>6'<E4><C7>||Z|<E0>q|9d|;N

$ cat input
383;06;55.270989;144991494994851A5485AA54J7HH337H3H33HT570BBG7BBGBT07BT7R55U155U5IR75I79QQ9SQQ9Q597Q57S229122928S4284;N