# Awk script

**URL:** <https://community.unix.com/t/awk-script/283137>\
**Category:** Shell Programming and Scripting\
**Created:** [February 20, 2011, 3:41am UTC](https://community.unix.com/t/awk-script/283137 "2011-02-20T03:41:52Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![shen](https://community.unix.com/letter_avatar/shen/32/6_22734026db12803ebb3e059622ac3534.png) [@shen](https://community.unix.com/u/shen)\
**Post date:** [February 20, 2011, 3:41am UTC](https://community.unix.com/t/awk-script/283137/1 "2011-02-20T03:41:52Z")

</div>

I have following text

```nohighlight
scaffold_1 phytozome6 gene 12632 13612 . + . ID=PT_0001s00200;Name=PT_0001s00200
scaffold_1 phytozome6 mRNA 12632 13612 . + . ID=PAC:18235173;Name=PT_0001s00200.1;PACid=18235173;Parent=PT_0001s00200
scaffold_1 phytozome6 5'-UTR 12632 12638 . + . Parent=PAC:18235173;PACid=18235173
scaffold_1 phytozome6 CDS 12639 12650 . + 0 Parent=PAC:18235173;PACid=18235173
scaffold_1 phytozome6 CDS 12768 12891 . + 0 Parent=PAC:18235173;PACid=18235173
scaffold_1 phytozome6 CDS 13117 13226 . + 2 Parent=PAC:18235173;PACid=18235173
scaffold_1 phytozome6 CDS 13310 13384 . + 0 Parent=PAC:18235173;PACid=18235173
scaffold_1 phytozome6 3'-UTR 13385 13612 . + . Parent=PAC:18235173;PACid=18235173

```

I want convert into

```nohighlight
scaffold_1 phytozome6 gene 12632 13612 . + . ID=PT_0001s00200;Name=PT_0001s00200
scaffold_1 phytozome6 mRNA 12632 13612 . + . ID=PT_0001s00200.1;Name=PT_0001s00200.1;PACid=18235173;Parent=PT_0001s00200
scaffold_1 phytozome6 5'-UTR 12632 12638 . + . Parent=PT_0001s00200.1;PACid=18235173
scaffold_1 phytozome6 CDS 12639 12650 . + 0 Parent=PT_0001s00200.1;PACid=18235173
scaffold_1 phytozome6 CDS 12768 12891 . + 0 Parent=PT_0001s00200.1;PACid=18235173
scaffold_1 phytozome6 CDS 13117 13226 . + 2 Parent=PT_0001s00200.1;PACid=18235173
scaffold_1 phytozome6 CDS 13310 13384 . + 0 Parent=PT_0001s00200.1;PACid=18235173
scaffold_1 phytozome6 3'-UTR 13385 13612 . + . Parent=PT_0001s00200.1;PACid=18235173

```

I tried with following script

```nohighlight
awk '{if(substr($9,17,10)=="Name=PT") print gensub("Parent=PAC:"substr($9,47,9),"Parent="substr($9,22,16)".1",
  substr($0,0)) substr($0,0) }' inputfile > outputfile

```

but still no luck.

Can you please help me,

---

<div class="post-metadata">

**Author:** ![DC\_Slick](https://community.unix.com/letter_avatar/dc_slick/32/6_22734026db12803ebb3e059622ac3534.png) [@DC\_Slick](https://community.unix.com/u/DC_Slick)\
**Post date:** [February 20, 2011, 4:10am UTC](https://community.unix.com/t/awk-script/283137/2 "2011-02-20T04:10:47Z")

</div>

why not use sed?

```nohighlight
sed 's/Parent=PAC:18235173;PACid=18235173/Parent=PT_0001s00200.1;PACid=18235173/g'

```

You can do an inline edit to the same file using the -i switch. Or redirect it to another file like your example shows

---

<div class="post-metadata">

**Author:** ![shen](https://community.unix.com/letter_avatar/shen/32/6_22734026db12803ebb3e059622ac3534.png) [@shen](https://community.unix.com/u/shen)\
**Post date:** [February 20, 2011, 4:50am UTC](https://community.unix.com/t/awk-script/283137/3 "2011-02-20T04:50:30Z")

</div>

I have used following script but still I cant replace the Parent=PAC

```nohighlight
awk '{if(substr($9,17,10)=="Name=PT") n=substr($9,22,16) gsub("Parent=PAC:"substr($9,47,9),"Parent="n".1");
  gsub("ID=PAC:"substr($9,8,19),"ID="n".1"); }' input>output

```

any help appreciated

---------- Post updated at 04:50 AM ---------- Previous update was at 04:41 AM ----------

Thank you for your reply,but this is only one record there are set records with unique PAC and PT ids ,In this case I have to use your solution 300000 times with changing PAC and PT ids,cant we use

```nohighlight
if(substr($9,17,10)=="Name=PT")

```

with your solution.

---

<div class="post-metadata">

**Author:** ![rdcwayx](https://community.unix.com/letter_avatar/rdcwayx/32/6_22734026db12803ebb3e059622ac3534.png) [@rdcwayx](https://community.unix.com/u/rdcwayx)\
**Post date:** [February 20, 2011, 5:44am UTC](https://community.unix.com/t/awk-script/283137/4 "2011-02-20T05:44:28Z")

</div>

```nohighlight
awk -F"[=;]" 'NR==1{s=$2 ".1";print;FS=OFS=";";next}{sub(/=.*/,"="s,$1)}1' infile

```

---

<div class="post-metadata">

**Author:** ![shen](https://community.unix.com/letter_avatar/shen/32/6_22734026db12803ebb3e059622ac3534.png) [@shen](https://community.unix.com/u/shen)\
**Post date:** [February 20, 2011, 6:24am UTC](https://community.unix.com/t/awk-script/283137/5 "2011-02-20T06:24:55Z")

</div>

Thank you for your reply but I get following

```nohighlight
scaffold_1 phytozome6 gene 2330052 2335284 . - . ID=.1;Name=PT_0001s02940
scaffold_1 phytozome6 mRNA 2330052 2335284 . - . ID=.1;Name=PT_0001s02940.1;PACid=18235154;Parent=PT_0001s02940
scaffold_1 phytozome6 CDS 2334981 2335230 . - 0 Parent=.1;PACid=18235154
scaffold_1 phytozome6 5'-UTR 2335231 2335284 . - . Parent=.1;PACid=18235154
scaffold_1 phytozome6 CDS 2334079 2334206 . - 2 Parent=.1;PACid=18235154
scaffold_1 phytozome6 CDS 2333907 2333978 . - 0 Parent=.1;PACid=18235154
scaffold_1 phytozome6 CDS 2333635 2333780 . - 0 Parent=.1;PACid=18235154
scaffold_1 phytozome6 CDS 2333448 2333562 . - 1 Parent=.1;PACid=18235154
scaffold_1 phytozome6 CDS 2333285 2333365 . - 0 Parent=.1;PACid=18235154
scaffold_1 phytozome6 CDS 2332541 2332678 . - 0 Parent=.1;PACid=18235154
scaffold_1 phytozome6 CDS 2331826 2331913 . - 0 Parent=.1;PACid=18235154
scaffold_1 phytozome6 CDS 2330651 2330764 . - 2 Parent=.1;PACid=18235154
scaffold_1 phytozome6 3'-UTR 2330052 2330460 . - . Parent=.1;PACid=18235154
scaffold_1 phytozome6 CDS 2330461 2330483 . - 2 Parent=.1;PACid=18235154

```

still no luck,  
I used following command it will give the correct output,Did I do correct?

```nohighlight
awk '{if(substr($9,1,5)=="ID=PT_") n=substr($9,4,16)".1"; gsub("ID=PAC:"substr($9,8,8),"ID="n);
  gsub("Parent=PAC:"substr($9,12,8),"Parent="n); print; }' infile > outfile

```

---

<div class="post-metadata">

**Author:** ![rdcwayx](https://community.unix.com/letter_avatar/rdcwayx/32/6_22734026db12803ebb3e059622ac3534.png) [@rdcwayx](https://community.unix.com/u/rdcwayx)\
**Post date:** [February 20, 2011, 6:42am UTC](https://community.unix.com/t/awk-script/283137/6 "2011-02-20T06:42:13Z")

</div>

is your document's first line different as the sample? I get the ID number from the first line.

The first line should be:

```nohighlight
scaffold_1 phytozome6 gene 12632 13612 . + . ID=PT_0001s00200;Name=PT_0001s00200

```

---

<div class="post-metadata">

**Author:** ![shen](https://community.unix.com/letter_avatar/shen/32/6_22734026db12803ebb3e059622ac3534.png) [@shen](https://community.unix.com/u/shen)\
**Post date:** [February 20, 2011, 6:48am UTC](https://community.unix.com/t/awk-script/283137/7 "2011-02-20T06:48:34Z")

</div>

yes It is different

```nohighlight
scaffold_1 phytozome6 gene 12632 13612 . + . ID=POPTR_0001s00200;Name=POPTR_0001s00200
scaffold_1 phytozome6 mRNA 12632 13612 . + . ID=PAC:18235173;Name=POPTR_0001s00200.1;PACid=18235173;Parent=POPTR_0001s00200
scaffold_1 phytozome6 5'-UTR 12632 12638 . + . Parent=PAC:18235173;PACid=18235173
scaffold_1 phytozome6 CDS 12639 12650 . + 0 Parent=PAC:18235173;PACid=18235173
scaffold_1 phytozome6 CDS 12768 12891 . + 0 Parent=PAC:18235173;PACid=18235173
scaffold_1 phytozome6 CDS 13117 13226 . + 2 Parent=PAC:18235173;PACid=18235173
scaffold_1 phytozome6 CDS 13310 13384 . + 0 Parent=PAC:18235173;PACid=18235173
scaffold_1 phytozome6 3'-UTR 13385 13612 . + . Parent=PAC:18235173;PACid=18235173

```

change it to

```nohighlight
caffold_1 phytozome6 gene 12632 13612 . + . ID=POPTR_0001s00200;Name=POPTR_0001s00200
scaffold_1 phytozome6 mRNA 12632 13612 . + . ID=POPTR_0001s00200.1;Name=POPTR_0001s00200.1;PACid=18235173;Parent=POPTR_0001s00200
scaffold_1 phytozome6 5'-UTR 12632 12638 . + . Parent=POPTR_0001s00200.1;PACid=18235173
scaffold_1 phytozome6 CDS 12639 12650 . + 0 Parent=POPTR_0001s00200.1;PACid=18235173
scaffold_1 phytozome6 CDS 12768 12891 . + 0 Parent=POPTR_0001s00200.1;PACid=18235173
scaffold_1 phytozome6 CDS 13117 13226 . + 2 Parent=POPTR_0001s00200.1;PACid=18235173
scaffold_1 phytozome6 CDS 13310 13384 . + 0 Parent=POPTR_0001s00200.1;PACid=18235173
scaffold_1 phytozome6 3'-UTR 13385 13612 . + . Parent=POPTR_0001s00200.1;PACid=18235173

```

Do you think your function will help.sorry for changing format

---

<div class="post-metadata">

**Author:** ![rdcwayx](https://community.unix.com/letter_avatar/rdcwayx/32/6_22734026db12803ebb3e059622ac3534.png) [@rdcwayx](https://community.unix.com/u/rdcwayx)\
**Post date:** [February 20, 2011, 7:00am UTC](https://community.unix.com/t/awk-script/283137/8 "2011-02-20T07:00:49Z")

</div>

No change, Still fine.

```nohighlight
$ cat infile
scaffold_1 phytozome6 gene 12632 13612 . + . ID=POPTR_0001s00200;Name=POPTR_0001s00200
scaffold_1 phytozome6 mRNA 12632 13612 . + . ID=PAC:18235173;Name=POPTR_0001s00200.1;PACid=18235173;Parent=POPTR_0001s00200
scaffold_1 phytozome6 5'-UTR 12632 12638 . + . Parent=PAC:18235173;PACid=18235173
scaffold_1 phytozome6 CDS 12639 12650 . + 0 Parent=PAC:18235173;PACid=18235173
scaffold_1 phytozome6 CDS 12768 12891 . + 0 Parent=PAC:18235173;PACid=18235173
scaffold_1 phytozome6 CDS 13117 13226 . + 2 Parent=PAC:18235173;PACid=18235173
scaffold_1 phytozome6 CDS 13310 13384 . + 0 Parent=PAC:18235173;PACid=18235173
scaffold_1 phytozome6 3'-UTR 13385 13612 . + . Parent=PAC:18235173;PACid=18235173

$ awk -F"[=;]" 'NR==1{s=$2 ".1";print;FS=";";OFS=";";next}{sub(/=.*/,"="s,$1)}1' infile
scaffold_1 phytozome6 gene 12632 13612 . + . ID=POPTR_0001s00200;Name=POPTR_0001s00200
scaffold_1 phytozome6 mRNA 12632 13612 . + . ID=POPTR_0001s00200.1;Name=POPTR_0001s00200.1;PACid=18235173;Parent=POPTR_0001s00200
scaffold_1 phytozome6 5'-UTR 12632 12638 . + . Parent=POPTR_0001s00200.1;PACid=18235173
scaffold_1 phytozome6 CDS 12639 12650 . + 0 Parent=POPTR_0001s00200.1;PACid=18235173
scaffold_1 phytozome6 CDS 12768 12891 . + 0 Parent=POPTR_0001s00200.1;PACid=18235173
scaffold_1 phytozome6 CDS 13117 13226 . + 2 Parent=POPTR_0001s00200.1;PACid=18235173
scaffold_1 phytozome6 CDS 13310 13384 . + 0 Parent=POPTR_0001s00200.1;PACid=18235173
scaffold_1 phytozome6 3'-UTR 13385 13612 . + . Parent=POPTR_0001s00200.1;PACid=18235173

```

Is your awk support `-F"[=;]"`

---

<div class="post-metadata">

**Author:** ![shen](https://community.unix.com/letter_avatar/shen/32/6_22734026db12803ebb3e059622ac3534.png) [@shen](https://community.unix.com/u/shen)\
**Post date:** [February 20, 2011, 7:13am UTC](https://community.unix.com/t/awk-script/283137/9 "2011-02-20T07:13:27Z")

</div>

Yes It supports FS,Thank you for your help but It I want unique sub sets depend on the POPTR\_ for example,those are gene names

```nohighlight
scaffold_997 phytozome6 gene 1687 2351 . - . ID=POPTR_0997s00200;Name=POPTR_0997s00200
scaffold_997 phytozome6 mRNA 1687 2351 . - . ID=PAC:18226942;Name=POPTR_0997s00200.1;PACid=18226942;Parent=POPTR_0997s00200
scaffold_997 phytozome6 CDS 2240 2317 . - 0 Parent=PAC:18226942;PACid=18226942
scaffold_997 phytozome6 5'-UTR 2318 2351 . - . Parent=PAC:18226942;PACid=18226942
scaffold_997 phytozome6 CDS 2078 2111 . - 0 Parent=PAC:18226942;PACid=18226942
scaffold_997 phytozome6 3'-UTR 1687 1866 . - . Parent=PAC:18226942;PACid=18226942
scaffold_997 phytozome6 CDS 1867 1997 . - 2 Parent=PAC:18226942;PACid=18226942

```

changed to

```nohighlight
scaffold_997 phytozome6 gene 1687 2351 . - . ID=POPTR_0997s00200;Name=POPTR_0997s00200
scaffold_997 phytozome6 mRNA 1687 2351 . - . ID=POPTR_0997s00200.1;Name=POPTR_0997s00200.1;PACid=18226942;Parent=POPTR_0997s00200
scaffold_997 phytozome6 CDS 2240 2317 . - 0 Parent=POPTR_0997s00200.1;PACid=18226942
scaffold_997 phytozome6 5'-UTR 2318 2351 . - . Parent=POPTR_0997s00200.1;PACid=18226942
scaffold_997 phytozome6 CDS 2078 2111 . - 0 Parent=POPTR_0997s00200.1;PACid=18226942
scaffold_997 phytozome6 3'-UTR 1687 1866 . - . Parent=POPTR_0997s00200.1;PACid=18226942
scaffold_997 phytozome6 CDS 1867 1997 . - 2 Parent=POPTR_0997s00200.1;PACid=18226942

```

Do you think you can adjust your function? I really like your function,instead of following one

```nohighlight
awk '{if(substr($9,1,9)=="ID=POPTR_") n=substr($9,4,16)".1"; gsub("ID=PAC:"substr($9,8,8),"ID="n);
  gsub("Parent=PAC:"substr($9,12,8),"Parent="n); print; }' input>output

```

---

<div class="post-metadata">

**Author:** ![rdcwayx](https://community.unix.com/letter_avatar/rdcwayx/32/6_22734026db12803ebb3e059622ac3534.png) [@rdcwayx](https://community.unix.com/u/rdcwayx)\
**Post date:** [February 20, 2011, 6:45pm UTC](https://community.unix.com/t/awk-script/283137/10 "2011-02-20T18:45:35Z")

</div>

I don't care what your code is doing, I write the code to export the output you want.

I try your latest input and output with my command, still perfect, I don't see any issue.

So you need point out the error what I need adjust.

---

<div class="post-metadata">

**Author:** ![shen](https://community.unix.com/letter_avatar/shen/32/6_22734026db12803ebb3e059622ac3534.png) [@shen](https://community.unix.com/u/shen)\
**Post date:** [February 21, 2011, 1:34am UTC](https://community.unix.com/t/awk-script/283137/11 "2011-02-21T01:34:39Z")

</div>

Here is the expected input and output,but your code not generate following output

```nohighlight
sscaffold_1	phytozome6	gene	12632	13612	.	+	.	ID=POPTR_0001s00200;Name=POPTR_0001s00200
scaffold_1	phytozome6	mRNA	12632	13612	.	+	.	ID=PAC:18235173;Name=POPTR_0001s00200.1;PACid=18235173;Parent=POPTR_0001s00200
scaffold_1	phytozome6	5'-UTR	12632	12638	.	+	.	Parent=PAC:18235173;PACid=18235173
scaffold_1	phytozome6	CDS	12639	12650	.	+	0	Parent=PAC:18235173;PACid=18235173
scaffold_1	phytozome6	CDS	12768	12891	.	+	0	Parent=PAC:18235173;PACid=18235173
scaffold_1	phytozome6	CDS	13117	13226	.	+	2	Parent=PAC:18235173;PACid=18235173
scaffold_1	phytozome6	CDS	13310	13384	.	+	0	Parent=PAC:18235173;PACid=18235173
scaffold_1	phytozome6	3'-UTR	13385	13612	.	+	.	Parent=PAC:18235173;PACid=18235173
scaffold_1	phytozome6	gene	19769	22804	.	+	.	ID=POPTR_0001s00210;Name=POPTR_0001s00210
scaffold_1	phytozome6	mRNA	19769	22804	.	+	.	ID=PAC:18238552;Name=POPTR_0001s00210.1;PACid=18238552;Parent=POPTR_0001s00210
scaffold_1	phytozome6	5'-UTR	19769	19827	.	+	.	Parent=PAC:18238552;PACid=18238552
scaffold_1	phytozome6	CDS	19828	20136	.	+	0	Parent=PAC:18238552;PACid=18238552
scaffold_1	phytozome6	CDS	22190	22516	.	+	0	Parent=PAC:18238552;PACid=18238552
scaffold_1	phytozome6	3'-UTR	22517	22804	.	+	.	Parent=PAC:18238552;PACid=18238552
scaffold_1	phytozome6	gene	74076	75893	.	+	.	ID=POPTR_0001s00220;Name=POPTR_0001s00220
scaffold_1	phytozome6	mRNA	74076	75893	.	+	.	ID=PAC:18237390;Name=POPTR_0001s00220.1;PACid=18237390;Parent=POPTR_0001s00220
scaffold_1	phytozome6	CDS	74076	74235	.	+	0	Parent=PAC:18237390;PACid=18237390
scaffold_1	phytozome6	CDS	74359	74634	.	+	2	Parent=PAC:18237390;PACid=18237390
scaffold_1	phytozome6	CDS	75259	75893	.	+	2	Parent=PAC:18237390;PACid=18237390
scaffold_1	phytozome6	gene	80191	81289	.	-	.	ID=POPTR_0001s00230;Name=POPTR_0001s00230
scaffold_1	phytozome6	mRNA	80191	81289	.	-	.	ID=PAC:18235601;Name=POPTR_0001s00230.1;PACid=18235601;Parent=POPTR_0001s00230
scaffold_1	phytozome6	CDS	81161	81289	.	-	0	Parent=PAC:18235601;PACid=18235601
scaffold_1	phytozome6	CDS	80191	80385	.	-	0	Parent=PAC:18235601;PACid=18235601

```

desired output

```nohighlight
scaffold_1	phytozome6	gene	12632	13612	.	+	.	ID=POPTR_0001s00200;Name=POPTR_0001s00200
scaffold_1	phytozome6	mRNA	12632	13612	.	+	.	ID=POPTR_0001s00200.1;Name=POPTR_0001s00200.1;PACid=18235173;Parent=POPTR_0001s00200
scaffold_1	phytozome6	5'-UTR	12632	12638	.	+	.	Parent=POPTR_0001s00200.1;PACid=18235173
scaffold_1	phytozome6	CDS	12639	12650	.	+	0	Parent=POPTR_0001s00200.1;PACid=18235173
scaffold_1	phytozome6	CDS	12768	12891	.	+	0	Parent=POPTR_0001s00200.1;PACid=18235173
scaffold_1	phytozome6	CDS	13117	13226	.	+	2	Parent=POPTR_0001s00200.1;PACid=18235173
scaffold_1	phytozome6	CDS	13310	13384	.	+	0	Parent=POPTR_0001s00200.1;PACid=18235173
scaffold_1	phytozome6	3'-UTR	13385	13612	.	+	.	Parent=POPTR_0001s00200.1;PACid=18235173
scaffold_1	phytozome6	gene	19769	22804	.	+	.	ID=POPTR_0001s00210;Name=POPTR_0001s00210
scaffold_1	phytozome6	mRNA	19769	22804	.	+	.	ID=POPTR_0001s00210.1;Name=POPTR_0001s00210.1;PACid=18238552;Parent=POPTR_0001s00210
scaffold_1	phytozome6	5'-UTR	19769	19827	.	+	.	Parent=POPTR_0001s00210.1;PACid=18238552
scaffold_1	phytozome6	CDS	19828	20136	.	+	0	Parent=POPTR_0001s00210.1;PACid=18238552
scaffold_1	phytozome6	CDS	22190	22516	.	+	0	Parent=POPTR_0001s00210.1;PACid=18238552
scaffold_1	phytozome6	3'-UTR	22517	22804	.	+	.	Parent=POPTR_0001s00210.1;PACid=18238552
scaffold_1	phytozome6	gene	74076	75893	.	+	.	ID=POPTR_0001s00220;Name=POPTR_0001s00220
scaffold_1	phytozome6	mRNA	74076	75893	.	+	.	ID=POPTR_0001s00220.1;Name=POPTR_0001s00220.1;PACid=18237390;Parent=POPTR_0001s00220
scaffold_1	phytozome6	CDS	74076	74235	.	+	0	Parent=POPTR_0001s00220.1;PACid=18237390
scaffold_1	phytozome6	CDS	74359	74634	.	+	2	Parent=POPTR_0001s00220.1;PACid=18237390
scaffold_1	phytozome6	CDS	75259	75893	.	+	2	Parent=POPTR_0001s00220.1;PACid=18237390
scaffold_1	phytozome6	gene	80191	81289	.	-	.	ID=POPTR_0001s00230;Name=POPTR_0001s00230
scaffold_1	phytozome6	mRNA	80191	81289	.	-	.	ID=POPTR_0001s00230.1;Name=POPTR_0001s00230.1;PACid=18235601;Parent=POPTR_0001s00230
scaffold_1	phytozome6	CDS	81161	81289	.	-	0	Parent=POPTR_0001s00230.1;PACid=18235601
scaffold_1	phytozome6	CDS	80191	80385	.	-	0	Parent=POPTR_0001s00230.1;PACid=18235601

```

I hope you can clearly see the issue now.Thank you

---

<div class="post-metadata">

**Author:** ![rdcwayx](https://community.unix.com/letter_avatar/rdcwayx/32/6_22734026db12803ebb3e059622ac3534.png) [@rdcwayx](https://community.unix.com/u/rdcwayx)\
**Post date:** [February 21, 2011, 3:12pm UTC](https://community.unix.com/t/awk-script/283137/12 "2011-02-21T15:12:38Z")

</div>

```nohighlight
awk -F \; '/gene/{split($(NF-1),a,"=");print;next}{sub(/=.*/,"="a[2]".1",$1)}1 ' infile

```

---

<div class="post-metadata">

**Author:** ![yinyuemi](https://community.unix.com/letter_avatar/yinyuemi/32/6_22734026db12803ebb3e059622ac3534.png) [@yinyuemi](https://community.unix.com/u/yinyuemi)\
**Post date:** [February 22, 2011, 2:33am UTC](https://community.unix.com/t/awk-script/283137/13 "2011-02-22T02:33:26Z")

</div>

```nohighlight
awk -F"=|;" '/Name=/{sub($2,$4);v=$2}{$2=v}1' file

```

---

<div class="post-metadata">

**Author:** ![shen](https://community.unix.com/letter_avatar/shen/32/6_22734026db12803ebb3e059622ac3534.png) [@shen](https://community.unix.com/u/shen)\
**Post date:** [February 22, 2011, 4:04am UTC](https://community.unix.com/t/awk-script/283137/14 "2011-02-22T04:04:38Z")

</div>

Thanks rdcwayx you are really talented,I just want to remove spaces between ; because your output gave following

```plaintext
Parent=POPTR_0001s00230.1; PACid=18235601

```

---------- Post updated at 04:04 AM ---------- Previous update was at 04:01 AM ----------

> [@yinyuemi](#):
>
> ```plaintext
> awk -F"=|;" '/Name=/{sub($2,$4);v=$2}{$2=v}1' file
> 
> ```

Thank you yinyuemi your out put missing ;

```plaintext
sscaffold_1 phytozome6 gene 12632 13612 . + . ID POPTR_0001s00200 Name POPTR_0001s00200
scaffold_1 phytozome6 mRNA 12632 13612 . + . ID POPTR_0001s00200.1 Name POPTR_0001s00200.1 PACid 18235173 Parent POPTR_0001s00200
scaffold_1 phytozome6 5'-UTR 12632 12638 . + . Parent POPTR_0001s00200.1 PACid 18235173
scaffold_1 phytozome6 CDS 12639 12650 . + 0 Parent POPTR_0001s00200.1 PACid 18235173
scaffold_1 phytozome6 CDS 12768 12891 . + 0 Parent POPTR_0001s00200.1 PACid 18235173
scaffold_1 phytozome6 CDS 13117 13226 . + 2 Parent POPTR_0001s00200.1 PACid 18235173
scaffold_1 phytozome6 CDS 13310 13384 . + 0 Parent POPTR_0001s00200.1 PACid 18235173
scaffold_1 phytozome6 3'-UTR 13385 13612 . + . Parent POPTR_0001s00200.1 PACid 18235173
scaffold_1 phytozome6 gene 19769 22804 . + . ID POPTR_0001s00210 Name POPTR_0001s00210
scaffold_1 phytozome6 mRNA 19769 22804 . + . ID POPTR_0001s00210.1 Name POPTR_0001s00210.1 PACid 18238552 Parent POPTR_0001s00210
scaffold_1 phytozome6 5'-UTR 19769 19827 . + . Parent POPTR_0001s00210.1 PACid 18238552
scaffold_1 phytozome6 CDS 19828 20136 . + 0 Parent POPTR_0001s00210.1 PACid 18238552
scaffold_1 phytozome6 CDS 22190 22516 . + 0 Parent POPTR_0001s00210.1 PACid 18238552
scaffold_1 phytozome6 3'-UTR 22517 22804 . + . Parent POPTR_0001s00210.1 PACid 18238552
scaffold_1 phytozome6 gene 74076 75893 . + . ID POPTR_0001s00220 Name POPTR_0001s00220
scaffold_1 phytozome6 mRNA 74076 75893 . + . ID POPTR_0001s00220.1 Name POPTR_0001s00220.1 PACid 18237390 Parent POPTR_0001s00220
scaffold_1 phytozome6 CDS 74076 74235 . + 0 Parent POPTR_0001s00220.1 PACid 18237390
scaffold_1 phytozome6 CDS 74359 74634 . + 2 Parent POPTR_0001s00220.1 PACid 18237390
scaffold_1 phytozome6 CDS 75259 75893 . + 2 Parent POPTR_0001s00220.1 PACid 18237390
scaffold_1 phytozome6 gene 80191 81289 . - . ID POPTR_0001s00230 Name POPTR_0001s00230
scaffold_1 phytozome6 mRNA 80191 81289 . - . ID POPTR_0001s00230.1 Name POPTR_0001s00230.1 PACid 18235601 Parent POPTR_0001s00230
scaffold_1 phytozome6 CDS 81161 81289 . - 0 Parent POPTR_0001s00230.1 PACid 18235601
scaffold_1 phytozome6 CDS 80191 80385 . - 0 Parent POPTR_0001s00230.1 PACid 18235601

```

---

<div class="post-metadata">

**Author:** ![rdcwayx](https://community.unix.com/letter_avatar/rdcwayx/32/6_22734026db12803ebb3e059622ac3534.png) [@rdcwayx](https://community.unix.com/u/rdcwayx)\
**Post date:** [February 22, 2011, 5:01am UTC](https://community.unix.com/t/awk-script/283137/15 "2011-02-22T05:01:57Z")

</div>

```nohighlight
awk '/gene/{split($(NF-1),a,"=");print;next}{sub(/=.*/,"="a[2]".1",$1)}1 ' FS=\; OFS=\; infile

```

---

<div class="post-metadata">

**Author:** ![ctsgnb](https://community.unix.com/letter_avatar/ctsgnb/32/6_22734026db12803ebb3e059622ac3534.png) [@ctsgnb](https://community.unix.com/u/ctsgnb)\
**Post date:** [February 22, 2011, 5:45am UTC](https://community.unix.com/t/awk-script/283137/16 "2011-02-22T05:45:32Z")

</div>

```nohighlight
while IFS='=;' read a b c d e
do [["$b" == "$d"]] && v=$b || b=$v
[[! -z $e]] && d="$d;$e"
echo "$a=$b;$c=$d"
done <infile

```

```nohighlight
# cat tst
sscaffold_1 phytozome6 gene 12632 13612 . + . ID=POPTR_0001s00200;Name=POPTR_0001s00200
scaffold_1 phytozome6 mRNA 12632 13612 . + . ID=PAC:18235173;Name=POPTR_0001s00200.1;PACid=18235173;Parent=POPTR_0001s00200
scaffold_1 phytozome6 5'-UTR 12632 12638 . + . Parent=PAC:18235173;PACid=18235173
scaffold_1 phytozome6 CDS 12639 12650 . + 0 Parent=PAC:18235173;PACid=18235173
scaffold_1 phytozome6 CDS 12768 12891 . + 0 Parent=PAC:18235173;PACid=18235173
scaffold_1 phytozome6 CDS 13117 13226 . + 2 Parent=PAC:18235173;PACid=18235173
scaffold_1 phytozome6 CDS 13310 13384 . + 0 Parent=PAC:18235173;PACid=18235173
scaffold_1 phytozome6 3'-UTR 13385 13612 . + . Parent=PAC:18235173;PACid=18235173
scaffold_1 phytozome6 gene 19769 22804 . + . ID=POPTR_0001s00210;Name=POPTR_0001s00210
scaffold_1 phytozome6 mRNA 19769 22804 . + . ID=PAC:18238552;Name=POPTR_0001s00210.1;PACid=18238552;Parent=POPTR_0001s00210
scaffold_1 phytozome6 5'-UTR 19769 19827 . + . Parent=PAC:18238552;PACid=18238552
scaffold_1 phytozome6 CDS 19828 20136 . + 0 Parent=PAC:18238552;PACid=18238552
scaffold_1 phytozome6 CDS 22190 22516 . + 0 Parent=PAC:18238552;PACid=18238552
scaffold_1 phytozome6 3'-UTR 22517 22804 . + . Parent=PAC:18238552;PACid=18238552
scaffold_1 phytozome6 gene 74076 75893 . + . ID=POPTR_0001s00220;Name=POPTR_0001s00220
scaffold_1 phytozome6 mRNA 74076 75893 . + . ID=PAC:18237390;Name=POPTR_0001s00220.1;PACid=18237390;Parent=POPTR_0001s00220
scaffold_1 phytozome6 CDS 74076 74235 . + 0 Parent=PAC:18237390;PACid=18237390
scaffold_1 phytozome6 CDS 74359 74634 . + 2 Parent=PAC:18237390;PACid=18237390
scaffold_1 phytozome6 CDS 75259 75893 . + 2 Parent=PAC:18237390;PACid=18237390
scaffold_1 phytozome6 gene 80191 81289 . - . ID=POPTR_0001s00230;Name=POPTR_0001s00230
scaffold_1 phytozome6 mRNA 80191 81289 . - . ID=PAC:18235601;Name=POPTR_0001s00230.1;PACid=18235601;Parent=POPTR_0001s00230
scaffold_1 phytozome6 CDS 81161 81289 . - 0 Parent=PAC:18235601;PACid=18235601
scaffold_1 phytozome6 CDS 80191 80385 . - 0 Parent=PAC:18235601;PACid=18235601

```

```nohighlight
# while IFS='=;' read a b c d e; do [["$b" == "$d"]] && v=$b || b=$v ; [[! -z $e]] && d="$d;$e" ; echo "$a=$b;$c=$d" ; done <tst
sscaffold_1 phytozome6 gene 12632 13612 . + . ID=POPTR_0001s00200;Name=POPTR_0001s00200
scaffold_1 phytozome6 mRNA 12632 13612 . + . ID=POPTR_0001s00200;Name=POPTR_0001s00200.1;PACid=18235173;Parent=POPTR_0001s00200
scaffold_1 phytozome6 5'-UTR 12632 12638 . + . Parent=POPTR_0001s00200;PACid=18235173
scaffold_1 phytozome6 CDS 12639 12650 . + 0 Parent=POPTR_0001s00200;PACid=18235173
scaffold_1 phytozome6 CDS 12768 12891 . + 0 Parent=POPTR_0001s00200;PACid=18235173
scaffold_1 phytozome6 CDS 13117 13226 . + 2 Parent=POPTR_0001s00200;PACid=18235173
scaffold_1 phytozome6 CDS 13310 13384 . + 0 Parent=POPTR_0001s00200;PACid=18235173
scaffold_1 phytozome6 3'-UTR 13385 13612 . + . Parent=POPTR_0001s00200;PACid=18235173
scaffold_1 phytozome6 gene 19769 22804 . + . ID=POPTR_0001s00210;Name=POPTR_0001s00210
scaffold_1 phytozome6 mRNA 19769 22804 . + . ID=POPTR_0001s00210;Name=POPTR_0001s00210.1;PACid=18238552;Parent=POPTR_0001s00210
scaffold_1 phytozome6 5'-UTR 19769 19827 . + . Parent=POPTR_0001s00210;PACid=18238552
scaffold_1 phytozome6 CDS 19828 20136 . + 0 Parent=POPTR_0001s00210;PACid=18238552
scaffold_1 phytozome6 CDS 22190 22516 . + 0 Parent=POPTR_0001s00210;PACid=18238552
scaffold_1 phytozome6 3'-UTR 22517 22804 . + . Parent=POPTR_0001s00210;PACid=18238552
scaffold_1 phytozome6 gene 74076 75893 . + . ID=POPTR_0001s00220;Name=POPTR_0001s00220
scaffold_1 phytozome6 mRNA 74076 75893 . + . ID=POPTR_0001s00220;Name=POPTR_0001s00220.1;PACid=18237390;Parent=POPTR_0001s00220
scaffold_1 phytozome6 CDS 74076 74235 . + 0 Parent=POPTR_0001s00220;PACid=18237390
scaffold_1 phytozome6 CDS 74359 74634 . + 2 Parent=POPTR_0001s00220;PACid=18237390
scaffold_1 phytozome6 CDS 75259 75893 . + 2 Parent=POPTR_0001s00220;PACid=18237390
scaffold_1 phytozome6 gene 80191 81289 . - . ID=POPTR_0001s00230;Name=POPTR_0001s00230
scaffold_1 phytozome6 mRNA 80191 81289 . - . ID=POPTR_0001s00230;Name=POPTR_0001s00230.1;PACid=18235601;Parent=POPTR_0001s00230
scaffold_1 phytozome6 CDS 81161 81289 . - 0 Parent=POPTR_0001s00230;PACid=18235601
scaffold_1 phytozome6 CDS 80191 80385 . - 0 Parent=POPTR_0001s00230;PACid=18235601
#

```

---

<div class="post-metadata">

**Author:** ![shen](https://community.unix.com/letter_avatar/shen/32/6_22734026db12803ebb3e059622ac3534.png) [@shen](https://community.unix.com/u/shen)\
**Post date:** [February 22, 2011, 8:26am UTC](https://community.unix.com/t/awk-script/283137/17 "2011-02-22T08:26:26Z")

</div>

your out put missing with .1 I need to replace POPTR\_0001s00200.1,which is name= value with PAC ids.

---------- Post updated at 08:26 AM ---------- Previous update was at 08:22 AM ----------

Hi rdcwayx unfortunately I got special case I want to

```plaintext
sscaffold_1 phytozome6 gene 12632 13612 . + . ID=POPTR_0001s00200;Name=POPTR_0001s00200
scaffold_1 phytozome6 mRNA 12632 13612 . + . ID=PAC:18235173;Name=POPTR_0001s00200.2;PACid=18235173;Parent=POPTR_0001s00200
scaffold_1 phytozome6 5'-UTR 12632 12638 . + . Parent=PAC:18235173;PACid=18235173
scaffold_1 phytozome6 CDS 12639 12650 . + 0 Parent=PAC:18235173;PACid=18235173
scaffold_1 phytozome6 CDS 12768 12891 . + 0 Parent=PAC:18235173;PACid=18235173
scaffold_1 phytozome6 CDS 13117 13226 . + 2 Parent=PAC:18235173;PACid=18235173
scaffold_1 phytozome6 CDS 13310 13384 . + 0 Parent=PAC:18235173;PACid=18235173

```

so I want to get out put

```plaintext
sscaffold_1 phytozome6 gene 12632 13612 . + . ID=POPTR_0001s00200;Name=POPTR_0001s00200
scaffold_1 phytozome6 mRNA 12632 13612 . + . ID=POPTR_0001s00200.2;Name=POPTR_0001s00200.2;PACid=18235173;Parent=POPTR_0001s00200
scaffold_1 phytozome6 5'-UTR 12632 12638 . + . Parent=POPTR_0001s00200.2;PACid=18235173
scaffold_1 phytozome6 CDS 12639 12650 . + 0 Parent=POPTR_0001s00200.2;PACid=18235173
scaffold_1 phytozome6 CDS 12768 12891 . + 0 Parent=POPTR_0001s00200.2;PACid=18235173
scaffold_1 phytozome6 CDS 13117 13226 . + 2 Parent=POPTR_0001s00200.2;PACid=18235173
scaffold_1 phytozome6 CDS 13310 13384 . + 0 Parent=POPTR_0001s00200.2;PACid=18235173
scaffold_1 phytozome6 3'-UTR 13385 13612 . + . Parent=POPTR_0001s00200.2;PACid=18235173

```

Can I get the

> [@](#):
>
> Name=POPTR\_0001s00200.2

value nad replace with

> [@](#):
>
> ID=PAC:18235173

and

> [@](#):
>
> Parent=PAC:18235173

this POPTR\_0001s00200.2 go up to POPTR\_0001s00200.12

---

<div class="post-metadata">

**Author:** ![ctsgnb](https://community.unix.com/letter_avatar/ctsgnb/32/6_22734026db12803ebb3e059622ac3534.png) [@ctsgnb](https://community.unix.com/u/ctsgnb)\
**Post date:** [February 22, 2011, 12:55pm UTC](https://community.unix.com/t/awk-script/283137/18 "2011-02-22T12:55:37Z")

</div>

```nohighlight
while IFS='=;' read a b c d e; do if [["$b" != "$d"]]; then [["$f" == 1]] && v=$d; f=0; b=$v; else f=1; fi; [[! -z $e]] && d="$d;$e"; echo "$a=$b;$c=$d"; done <tst

```

```nohighlight
# cat tst
sscaffold_1 phytozome6 gene 12632 13612 . + . ID=POPTR_0001s00200;Name=POPTR_0001s00200
scaffold_1 phytozome6 mRNA 12632 13612 . + . ID=PAC:18235173;Name=POPTR_0001s00200.1;PACid=18235173;Parent=POPTR_0001s00200
scaffold_1 phytozome6 5'-UTR 12632 12638 . + . Parent=PAC:18235173;PACid=18235173
scaffold_1 phytozome6 CDS 12639 12650 . + 0 Parent=PAC:18235173;PACid=18235173
scaffold_1 phytozome6 CDS 12768 12891 . + 0 Parent=PAC:18235173;PACid=18235173
scaffold_1 phytozome6 CDS 13117 13226 . + 2 Parent=PAC:18235173;PACid=18235173
scaffold_1 phytozome6 CDS 13310 13384 . + 0 Parent=PAC:18235173;PACid=18235173
scaffold_1 phytozome6 3'-UTR 13385 13612 . + . Parent=PAC:18235173;PACid=18235173
scaffold_1 phytozome6 gene 19769 22804 . + . ID=POPTR_0001s00210;Name=POPTR_0001s00210
scaffold_1 phytozome6 mRNA 19769 22804 . + . ID=PAC:18238552;Name=POPTR_0001s00210.1;PACid=18238552;Parent=POPTR_0001s00210
scaffold_1 phytozome6 5'-UTR 19769 19827 . + . Parent=PAC:18238552;PACid=18238552
scaffold_1 phytozome6 CDS 19828 20136 . + 0 Parent=PAC:18238552;PACid=18238552
scaffold_1 phytozome6 CDS 22190 22516 . + 0 Parent=PAC:18238552;PACid=18238552
scaffold_1 phytozome6 3'-UTR 22517 22804 . + . Parent=PAC:18238552;PACid=18238552
scaffold_1 phytozome6 gene 74076 75893 . + . ID=POPTR_0001s00220;Name=POPTR_0001s00220
scaffold_1 phytozome6 mRNA 74076 75893 . + . ID=PAC:18237390;Name=POPTR_0001s00220.1;PACid=18237390;Parent=POPTR_0001s00220
scaffold_1 phytozome6 CDS 74076 74235 . + 0 Parent=PAC:18237390;PACid=18237390
scaffold_1 phytozome6 CDS 74359 74634 . + 2 Parent=PAC:18237390;PACid=18237390
scaffold_1 phytozome6 CDS 75259 75893 . + 2 Parent=PAC:18237390;PACid=18237390
scaffold_1 phytozome6 gene 80191 81289 . - . ID=POPTR_0001s00230;Name=POPTR_0001s00230
scaffold_1 phytozome6 mRNA 80191 81289 . - . ID=PAC:18235601;Name=POPTR_0001s00230.1;PACid=18235601;Parent=POPTR_0001s00230
scaffold_1 phytozome6 CDS 81161 81289 . - 0 Parent=PAC:18235601;PACid=18235601
scaffold_1 phytozome6 CDS 80191 80385 . - 0 Parent=PAC:18235601;PACid=18235601
#

```

```nohighlight
# while IFS='=;' read a b c d e; do if [["$b" != "$d"]]; then [["$f" == 1]] && v=$d; f=0; b=$v; else f=1; fi; [[! -z $e]] && d="$d;$e"; echo "$a=$b;$c=$d"; done <tst
sscaffold_1 phytozome6 gene 12632 13612 . + . ID=POPTR_0001s00200;Name=POPTR_0001s00200
scaffold_1 phytozome6 mRNA 12632 13612 . + . ID=POPTR_0001s00200.1;Name=POPTR_0001s00200.1;PACid=18235173;Parent=POPTR_0001s00200
scaffold_1 phytozome6 5'-UTR 12632 12638 . + . Parent=POPTR_0001s00200.1;PACid=18235173
scaffold_1 phytozome6 CDS 12639 12650 . + 0 Parent=POPTR_0001s00200.1;PACid=18235173
scaffold_1 phytozome6 CDS 12768 12891 . + 0 Parent=POPTR_0001s00200.1;PACid=18235173
scaffold_1 phytozome6 CDS 13117 13226 . + 2 Parent=POPTR_0001s00200.1;PACid=18235173
scaffold_1 phytozome6 CDS 13310 13384 . + 0 Parent=POPTR_0001s00200.1;PACid=18235173
scaffold_1 phytozome6 3'-UTR 13385 13612 . + . Parent=POPTR_0001s00200.1;PACid=18235173
scaffold_1 phytozome6 gene 19769 22804 . + . ID=POPTR_0001s00210;Name=POPTR_0001s00210
scaffold_1 phytozome6 mRNA 19769 22804 . + . ID=POPTR_0001s00210.1;Name=POPTR_0001s00210.1;PACid=18238552;Parent=POPTR_0001s00210
scaffold_1 phytozome6 5'-UTR 19769 19827 . + . Parent=POPTR_0001s00210.1;PACid=18238552
scaffold_1 phytozome6 CDS 19828 20136 . + 0 Parent=POPTR_0001s00210.1;PACid=18238552
scaffold_1 phytozome6 CDS 22190 22516 . + 0 Parent=POPTR_0001s00210.1;PACid=18238552
scaffold_1 phytozome6 3'-UTR 22517 22804 . + . Parent=POPTR_0001s00210.1;PACid=18238552
scaffold_1 phytozome6 gene 74076 75893 . + . ID=POPTR_0001s00220;Name=POPTR_0001s00220
scaffold_1 phytozome6 mRNA 74076 75893 . + . ID=POPTR_0001s00220.1;Name=POPTR_0001s00220.1;PACid=18237390;Parent=POPTR_0001s00220
scaffold_1 phytozome6 CDS 74076 74235 . + 0 Parent=POPTR_0001s00220.1;PACid=18237390
scaffold_1 phytozome6 CDS 74359 74634 . + 2 Parent=POPTR_0001s00220.1;PACid=18237390
scaffold_1 phytozome6 CDS 75259 75893 . + 2 Parent=POPTR_0001s00220.1;PACid=18237390
scaffold_1 phytozome6 gene 80191 81289 . - . ID=POPTR_0001s00230;Name=POPTR_0001s00230
scaffold_1 phytozome6 mRNA 80191 81289 . - . ID=POPTR_0001s00230.1;Name=POPTR_0001s00230.1;PACid=18235601;Parent=POPTR_0001s00230
scaffold_1 phytozome6 CDS 81161 81289 . - 0 Parent=POPTR_0001s00230.1;PACid=18235601
scaffold_1 phytozome6 CDS 80191 80385 . - 0 Parent=POPTR_0001s00230.1;PACid=18235601
#

```

In a multi-line display (more readable)

```nohighlight
while IFS='=;' read a b c d e
do if [["$b" != "$d"]]
        then [["$f" == 1]] && v=$d; f=0; b=$v
        else f=1
fi
[[! -z $e]] && d="$d;$e"
echo "$a=$b;$c=$d"
done <tst

```

---

<div class="post-metadata">

**Author:** ![shen](https://community.unix.com/letter_avatar/shen/32/6_22734026db12803ebb3e059622ac3534.png) [@shen](https://community.unix.com/u/shen)\
**Post date:** [February 22, 2011, 2:34pm UTC](https://community.unix.com/t/awk-script/283137/19 "2011-02-22T14:34:31Z")

</div>

Thank you it works perfectly,your code is faster.I tried with following code earlier

```nohighlight
awk '/mRNA/{split($(NF-2),a,"=");print;next}{sub(/t=.*/,"t="a[2],$1)}1 ' FS=\; OFS=\; input > tmp1
awk '{if(substr($3,1,4)=="mRNA")split ($9, a,";");{sub(a[1],a[2])} print ;}' tmp1>tmp2
awk '{if(substr($9,1,4)=="Name"){sub(substr($9,1,4),"ID")} print;}' tmp2> output

```

---

<div class="post-metadata">

**Author:** ![rdcwayx](https://community.unix.com/letter_avatar/rdcwayx/32/6_22734026db12803ebb3e059622ac3534.png) [@rdcwayx](https://community.unix.com/u/rdcwayx)\
**Post date:** [February 22, 2011, 6:40pm UTC](https://community.unix.com/t/awk-script/283137/20 "2011-02-22T18:40:03Z")

</div>

All in one.

```nohighlight
awk '/mRNA/{split($2,a,"=");print;next}/gene/{print;next}{sub(/=.*/,"="a[2],$1)}1 ' FS=\; OFS=\; infile

```

[Next page](https://community.unix.com/t/awk-script/283137.md?page=2)
