简体   繁体   中英

Compare 2 files and delete the lines that match criteria using awk / sed /bash

I am new to awk and sed and have the below requirement.

Below is the file data

Old File :

SNO,ID,SHAPE,COST,CURRENTCOST
1,ABC,EFG,123,123
2,EEC,EFG,123,12
3,ARR,EFG,123,12

New File :

SNO,ID,SHAPE,COST,CURRENTCOST
1,ABC,EFG,123,0
2,EEC,EFG,123,12
3,ARR,EFG,123,12 

Expected output File :

SNO,ID,SHAPE,COST,CURRENTCOST
2,EEC,EFG,123,12
3,ARR,EFG,123,12

Firstly , I would like compare the ID's and if they match CHECK IF CURRENTCOST(5th Column ) is 0 , if it is 0 , then delete the line from New file

Below is the code flow

if(CURRENTCOST == 0)
THEN 
  IF(FIRST FILE ID == SECOND FILE ID)
     THEN DELETE THE LINE FROM SECOND FILE 
  ELSE
     DO NOTHING

Also , I can skip copying that line if the condition is true and copy other lines to a new file

Hope this helps.

Thanks Tejas

Using awk you can do this:

awk -F, 'NR == FNR {a[$1]=$5; next} !($1 in a && $5 == 0)' oldFile newFile

SNO,ID,SHAPE,COST,CURRENTCOST
2,EEC,EFG,123,12
3,ARR,EFG,123,12
grep -v '0$' newFile  | join -t , -o 2.{1..5} oldFile -
  1. remove the unwanted line from the newFile
  2. join oldfile with modified content from stdin
  3. select columns from newfile only

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM