user3319356
user3319356

Reputation: 173

Adding lines to file

I need help regarding adding some lines to a file; for expamle, I grep from text this parameters and save it to File1:

cellIdentity="461"
cellIdentity="465"
cellIdentity="462"
cellIdentity="466"
cellIdentity="463"
cellIdentity="467"

And now I need to create another file, File2 that looks like:

cellIdentity="461"
cellIdentity="465"
 cellIdentity="468"
cellIdentity="462"
cellIdentity="466"
 cellIdentity="469"
cellIdentity="463"
cellIdentity="467"
 cellIdentity="460"

Three new lines I spaced out. So basically, I need to add these 3 lines, the last digit's 8,9,0 are always the same in these three lines, and the first two digit's are like in other lines. I don't know if it is possible to do it. I was trying with the sed command but no luck. I'm using /bin/csh in solaris. Any help/tips would be great.

Thanks

Upvotes: 1

Views: 97

Answers (2)

potong
potong

Reputation: 58351

This might work for you (GNU sed):

sed -r '1{x;s/^/890/;x};2~2{p;G;s/(.)("\n)(.)(.*)/\3\2\4\3/;P;s/.*\n//;h;d}' file

Upvotes: 2

BMW
BMW

Reputation: 45223

Using awk

awk 'BEGIN{split("8 9 0",a," ")}NR%2==0{t=$0;sub(/.$/,a[++i],$2);$0=t RS $0}1' FS=\" OFS=\" file

cellIdentity="461"
cellIdentity="465"
cellIdentity="468"
cellIdentity="462"
cellIdentity="466"
cellIdentity="469"
cellIdentity="463"
cellIdentity="467"
cellIdentity="460"

Explanation

  • FS=\" OFS=\" define field separator "
  • BEGIN{split("8 9 0",a," ")} define the 8,9,0 in array a in BEGIN part
  • NR%2==0 find the odd lines
  • sub(/.$/,a[++i],$2) replace the last character in column 2 which get from array a one after one
  • 1 same as print

Upvotes: 4

Related Questions