Reputation: 3449
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?
Edit: this method also include having a log for the data that is being removed for later analysis
Upvotes: 0
Views: 57
Reputation: 10865
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:
$ 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
Upvotes: 2