Search code examples
bashshellshcut

how to gzip a file using cut to exclude the path of the filename from a log file


I have a log file that prints if a batch file processed successfully or not. The 3rd column shows the status letter of "F" for Failed or "C" for complete. The 5th column shows the full path and batch file name.

Example of log file output:

392  02/04/2018:2:00  c  4444 /batchprocessing/abc.dat
444  02/04/2018:3:00  F  4442 /batchprocessing/mnop.dat  
3333 02/04/2018:4:00  c  2234 /batchprocessing/xyz.dat

I am trying to gzip all batch files that have a status of "F" to a backup directory while keeping the same file name pulled from the 5th column without the path, just the file name.

Code:

while read -r f1 f2 f3 f4 f5 f6 f7 ; do
    if [ "${f3}" = "F" ] ; then
        gzip "${f6}" > /backup/batch_backups/"${f6}" cut -c 17-30
    else
          echo "No Error!"
    fi
done < batch.log

the cut -c 17-30 should only pull the filename excluding the path to the file.

for example batch_backups should have mnop.dat.gz using the example output from the log file above.


Solution

  • Capture the output:

    gzip "${f6}" > /backup/batch_backups/"$(cut -c 17-30 <<<"${f6}")".gz
    

    But I think this is better than what you're trying to do (XY Problem), since you're trying to get the file name from full path:

    gzip "${f6}" > /backup/batch_backups/"$(basename "${f6}")".gz
    

    You can also use ${f6##*/} instead of basename.