Thursday, October 27, 2022

[SOLVED] bash print only last 7 fields

Issue

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.


Solution

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

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



Answered By - karakfa
Answer Checked By - Pedro (WPSolving Volunteer)