Linux : Move files that have more than 100 commas in one line

153 Views Asked by At

I have 100 files in a specific directory that contains several records with fields delimited with commas.

I need to use a Linux command that check the lines in each file and if the line contains more than 100 comma move it to another directory.

Is it possible ?

1

There are 1 best solutions below

3
On BEST ANSWER

Updated Answer

Although my original answer below is functional, Glenn's (@glennjackman) suggestion in the comments is far more concise, idiomatic, eloquent and preferable - as follows:

#!/bin/bash
mkdir subdir
for f in file*; do
   awk -F, 'NF>100{exit 1}' "$f" || mv "$f" subdir
done

It basically relies on awk's exit status generally being 0, and then only setting it to 1 when encountering files that need moving.

Original Answer

This will tell you if a file has more than 100 commas on any line:

awk -F, 'NF>100{print 1;exit} END{print 0}' someFile

It will print 1 and exit without parsing the remainder of the file if the file has any line with more than 100, and print 0 at the end if it doesn't.

If you want to move them as well, use

#!/bin/bash
mkdir subdir
for f in file*; do
   if [[ $(awk -F, 'NF>100{print 1;exit}END{print 0}' "$f") != "0" ]]; then
      echo mv "$f" subdir
   fi
done

Try this and see if it selects the correct files, and, if you like it, remove the word echo and run it again so it actually moves them. Back up first!