Home > database >  bash print only last 7 fields
bash print only last 7 fields

Time:10-23

I have hundreds of thousands of files with several hundreds of thousands of lines in each of them.

2022-09-19/SALES_1.csv:CUST1,US,2022-09-19,43.31,17.56,47.1,154.48,154. 114
2022-09-20/SALES_2.csv:CUST2,NA,2022-09-20,12.4,16.08,48.08,18.9,15.9,3517

The lines may have different number of fields. NO matter how many fields are present, I'm wanting to extract just the last 7 fields.

I'm trying with cut & awk but, have been only able to prit a range of fields but not last 'n' fields.

Please could I request guidance.

CodePudding user response:

$  rev file | cut -d, -f1-7 | rev

will give the last 7 fields regardless of varying number of fields in each record.

CodePudding user response:

Using any POSIX awk:

$ awk -F',' 'NF>7{sub("([^,]*,){"NF-7"}","")} 1' file
US,2022-09-19,43.31,17.56,47.1,154.48,154. 114
2022-09-20,12.4,16.08,48.08,18.9,15.9,3517

CodePudding user response:

 1  {m,g}awk' BEGIN { _ =(_ =_^= FS = OFS = ",") _
 2                   ___= "^[^"(__= "\5") ("]*")__
 3
 4  } NF<=_ || ($(NF-_) = __$(NF-_))^(sub(___,"")*!_)'
US,
    2022-09-19,
    43.31,
    17.56,
    47.1,
    154.48,
    154. 114
2022-09-20,
    12.4,
    16.08,
    48.08,
    18.9,
    15.9,
    3517
  • Related