替换除第三行以外的所有行的值

  • 本文关键字:替换 三行 shell csv awk
  • 更新时间 :
  • 英文 :


数据集:

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

我试图取代";常规空气";在第2列中,除了第3行之外的所有行,但我没有得到预期的输出。

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

输出:

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

预期:

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

您是print,但第三行除外,只需将print单独操作,您就会得到所需的结果,让sales.csv内容为

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

然后

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

给出输出

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

说明:值更改动作$2="Irregular Air"有条件,所以它应用于除第三行以外的所有行,打印动作没有条件,所以不加区分地应用。

(在GNU Awk 5.0.1中测试(

NR!=3在大括号之前时,它充当行过滤器:仅打印NR!=3的行。

要打印所有行但仅有条件地更改$2,请将其移动到大括号块内的if语句中。

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

输出:

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

相关内容

  • 没有找到相关文章

最新更新