Home > Net >  Replace value on all lines except the third
Replace value on all lines except the third

Time:10-02

Dataset:

Meredith Norris Thomas;Regular Air;HomeOffice;Office Supplies;Envelopes;Small Box;Park Ridge⢠Embossed Executive Business Envelopes 
Paige Mason;Regular Air;Corporate;Office Supplies;Labels;Small Box;*Staples* Packaging Labels 
Kara Pace;Regular Air;HomeOffice;Office Supplies;Scissors, Rulers and Trimmers;Small Pack;Fiskars® Softgrip Scissors 
Kristina Collier;Regular Air;Consumer;Office Supplies;Labels;Small Box;Avery 498 
Ryan Foster;Regular Air;HomeOffice;Office Supplies;Scissors, Rulers and Trimmers;Wrap Bag;Letter Slitter

I have tried to replace "Regular Air" in column 2 on all lines except the 3rd one but I'm not getting expected output.

awk 'BEGIN{FS=";";OFS=";"} NR!=3{$2="Irregular Air";print $0}' sales.csv

Output:

Meredith Norris Thomas;Irregular Air;HomeOffice;Office Supplies;Envelopes;Small Box;Park Ridge⢠Embossed Executive Business Envelopes 
Paige Mason;Irregular Air;Corporate;Office Supplies;Labels;Small Box;*Staples* Packaging Labels 
Kristina Collier;Irregular Air;Consumer;Office Supplies;Labels;Small Box;Avery 498 
Ryan Foster;Irregular Air;HomeOffice;Office Supplies;Scissors, Rulers and Trimmers;Wrap Bag;Letter Slitter

Expected:

Meredith Norris Thomas;Irregular Air;HomeOffice;Office Supplies;Envelopes;Small Box;Park Ridge⢠Embossed Executive Business Envelopes 
Paige Mason;Irregular Air;Corporate;Office Supplies;Labels;Small Box;*Staples* Packaging Labels 
Kara Pace;Regular Air;HomeOffice;Office Supplies;Scissors, Rulers and Trimmers;Small Pack;Fiskars® Softgrip Scissors 
Kristina Collier;Irregular Air;Consumer;Office Supplies;Labels;Small Box;Avery 498 
Ryan Foster;Irregular Air;HomeOffice;Office Supplies;Scissors, Rulers and Trimmers;Wrap Bag;Letter Slitter

CodePudding user response:

When NR!=3 is before the curly braces it serves as a line filter: only lines where NR!=3 are printed.

To print all the lines but only conditionally change $2, move it to an if statement inside the curly brace block.

awk 'BEGIN{FS=";";OFS=";"} {if(NR!=3){$2="Irregular Air";} print $0}' sales.csv

Output:

Meredith Norris Thomas;Irregular Air;HomeOffice;Office Supplies;Envelopes;Small Box;Park Ridge⢠Embossed Executive Business Envelopes
Paige Mason;Irregular Air;Corporate;Office Supplies;Labels;Small Box;*Staples* Packaging Labels
Kara Pace;Regular Air;HomeOffice;Office Supplies;Scissors, Rulers and Trimmers;Small Pack;Fiskars® Softgrip Scissors
Kristina Collier;Irregular Air;Consumer;Office Supplies;Labels;Small Box;Avery 498
Ryan Foster;Irregular Air;HomeOffice;Office Supplies;Scissors, Rulers and Trimmers;Wrap Bag;Letter Slitter

CodePudding user response:

You are printing all lines but 3rd, just make print separate action and you will get desired result, let sales.csv content be

Meredith Norris Thomas;Regular Air;HomeOffice;Office Supplies;Envelopes;Small Box;Park Ridge⢠Embossed Executive Business Envelopes 
Paige Mason;Regular Air;Corporate;Office Supplies;Labels;Small Box;*Staples* Packaging Labels 
Kara Pace;Regular Air;HomeOffice;Office Supplies;Scissors, Rulers and Trimmers;Small Pack;Fiskars® Softgrip Scissors 
Kristina Collier;Regular Air;Consumer;Office Supplies;Labels;Small Box;Avery 498 
Ryan Foster;Regular Air;HomeOffice;Office Supplies;Scissors, Rulers and Trimmers;Wrap Bag;Letter Slitter

then

awk 'BEGIN{FS=";";OFS=";"} NR!=3{$2="Irregular Air"}{print $0}' sales.csv

gives output

Meredith Norris Thomas;Irregular Air;HomeOffice;Office Supplies;Envelopes;Small Box;Park Ridge⢠Embossed Executive Business Envelopes 
Paige Mason;Irregular Air;Corporate;Office Supplies;Labels;Small Box;*Staples* Packaging Labels 
Kara Pace;Regular Air;HomeOffice;Office Supplies;Scissors, Rulers and Trimmers;Small Pack;Fiskars® Softgrip Scissors 
Kristina Collier;Irregular Air;Consumer;Office Supplies;Labels;Small Box;Avery 498 
Ryan Foster;Irregular Air;HomeOffice;Office Supplies;Scissors, Rulers and Trimmers;Wrap Bag;Letter Slitter

Explanation: value changing action $2="Irregular Air" has condition so it is applied to all but 3rd line, printing action has not condition, so it is applied indiscriminately.

(tested in GNU Awk 5.0.1)

  • Related