Jiji
Jiji

Reputation: 71

Remove column in awk command

I want to remove column as for now i have command that include splitting, header and trailer in one command. I wanted to do splitting, header, trailer and remove first 3 columns in one command.

My original output is

  Country   Gender    Plate       Name        Account Number        Car Name
    X        F          A          Fara        XXXXXXXXXX            Ixora
    X        F          b          Jiha        XXXXXXXXXX            Saga
    X        M          c          Jiji        XXXXXXXXXX            Proton

My splited files:

first file

06032017

  Country   Gender    Plate       Name    Account Number     Car Name        
    X        F          A     Fara        XXXXXXXXXX         Ixora

EOF 1

second file
06032017

  Country   Gender    Plate       Name        Account Number        Car Name
    X        F          b          Jiha        XXXXXXXXXX            Saga

EOF1

My desitre output:

06032017

    Name    Account Number     Car Name        
    Fara        XXXXXXXXXX         Ixora

EOF 1

06032017

  Name        Account Number        Car Name
  Jiha        XXXXXXXXXX            Saga

EOF1

This is my splitted command:

awk -v date="$(date +"%d%m%Y")" -F\| 'NR==1 {h=$0; next} 
{file="CAR_V1_"$1"_"$2"_"date".csv"; print (a[file]++?"": "DETAIL "date"" ORS h ORS) $0 > file}
END{for(file in a) print "EOF " a[file] > file}' HIRE_PURCHASE_testing.csv

Upvotes: 0

Views: 1481

Answers (1)

George Vasiliou
George Vasiliou

Reputation: 6345

To get an idea how to skip some fields:

$ echo "Field1 Field2 Field3 Field4 Field5 Field6"  |awk '{print $0}'
Field1 Field2 Field3 Field4 Field5 Field6  #No skipping here. Printed just for comparison

$ echo "Field1 Field2 Field3 Field4 Field5 Field6"  |awk '{print substr($0, index($0,$4))}'
Field4 Field5 Field6

$ echo "Field1 Field2 Field3 Field4 Field5 Field6"  |awk '{$1=$2=$3="";print}'
   Field4 Field5 Field6
#Mind the gaps in the beginning. You can use this if you need to "delete" particular columns.
#Actually you are not really delete columns but replace their contents with a blank value.

$ echo "Field1 Field2 Field3 Field4 Field5 Field6"  |awk '{gsub($1FS$2FS$3FS$4,$4);print}'
Field4 Field5 Field6   #Columns 1-2-3-4 have been replaced by column4. No gaps here.

$ echo "Field1 Field2 Field3 Field4 Field5 Field6"  |awk '{print $4,$5,$6}' 
Field4 Field5 Field6
# If you have a fixed number of fields (i.e 6 fields) you can just do not print the first three fields and print the last three fields

Adapted to your existed code, the line

print (a[file]++?"": "DETAIL "date"" ORS h ORS) $0 > file

If changed to

print (a[file]++?"": "DETAIL "date"" ORS h ORS) substr($0, index($0,$4)) > file

Or to

print (a[file]++?"": "DETAIL "date"" ORS h ORS) $4,$5,$6 > file #if you have a fixed number of columns in the file)

should be enough.

I would sugggest next time you need help, simplify the problem to get help more easily. For the rest users is kind of hard to fully follow you.

Also have a look here to see how all those functions work in details: https://www.gnu.org/software/gawk/manual/html_node/String-Functions.html

Upvotes: 1

Related Questions