I have input file that look's like :

cellIdentity="42901"
cellIdentity="42902"
cellIdentity="42903"

number's inside the string are diferent from file to file. What I want now is to create ouput file that look's like

cellIdentity="42901"
cellIdentity="42905"
cellIdentity="42902"
cellIdentity="42906"
cellIdentity="42903"
cellIdentity="42907"

So basicly I need to insert 3 new lines which will have the same values as from input file, only the last digit will be all the time 5, 6 , 7.

I tried with awk, and I think I'm on good way, but still missing something, so please help. Thanks

awk 'BEGIN{split("5 6 7",a," ")}NR%2==0{t=$0;sub(/.$/,a[++i],$2);$0=t RS $0}1' FS=\" OFS=\" /tmp/MC/Cell_F1 > /tmp/MC/Cell_F2
有帮助吗?

解决方案

Yes, you are on a good way. The problem is that you perform the substitution on even lines in the input:

NR%2==0

Removing the check should fix the issue:

awk 'BEGIN{split("5 6 7",a," ")}{t=$0;sub(/.$/,a[++i],$2);$0=t RS $0}1' FS=\" OFS=\" /tmp/MC/Cell_F1 > /tmp/MC/Cell_F2

其他提示

Based on your awk

awk 'BEGIN {split("5 6 7",a," ")} {print $0"\n" substr($0,1,18) a[++i] "\""}' file
cellIdentity="42901"
cellIdentity="42905"
cellIdentity="42902"
cellIdentity="42906"
cellIdentity="42903"
cellIdentity="42907"

I take some part of the original string and add numbers to it.
If you have other number, larger number, you may need to change it some.

Since you already have some awk answers. How about something in perl?

perl -pe 'BEGIN{$n=4}
          $n++>7?$n=5:$n;
          $a=$_;print $a;
          s/(\d).$/$n."\""/ge' your_file
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top