Issue
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
Solution
You are print
ing 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)
Answered By - Daweo Answer Checked By - Mary Flores (WPSolving Volunteer)