Reputation: 173
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
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
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"
FS=\" OFS=\"
define field separator "
BEGIN{split("8 9 0",a," ")}
define the 8,9,0 in array a
in BEGIN partNR%2==0
find the odd linessub(/.$/,a[++i],$2)
replace the last character in column 2 which get from array a
one after one1
same as printUpvotes: 4