# Transpose using awk

**URL:** <https://community.unix.com/t/transpose-using-awk/245102>\
**Category:** Shell Programming and Scripting\
**Created:** [July 21, 2009, 9:59am UTC](https://community.unix.com/t/transpose-using-awk/245102 "2009-07-21T09:59:38Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![new\_ds\_man](https://community.unix.com/letter_avatar/new_ds_man/32/6_449c533f586939b6e5f246632a46421f.png) [@new\_ds\_man](https://community.unix.com/u/new_ds_man)\
**Post date:** [July 21, 2009, 9:59am UTC](https://community.unix.com/t/transpose-using-awk/245102/1 "2009-07-21T09:59:38Z")

</div>

I have a requirement to transpose the below xml which is in a text file on unix:

```nohighlight
<?xml version="1.0" ?>
<REQUEST>
<ID>XXX</ID>
<TIMESTAMP>20090720062610</TIMESTAMP>
<FLAG>Y</FLAG>
<TO_FLAG>Y</TO_FLAG>
</REQUEST>

```

to

```nohighlight
<?xml version="1.0" ?><REQUEST><ID>XXX</ID><TIMESTAMP>20090720062610</TIMESTAMP><FLAG>Y</FLAG><TO_FLAG>Y</TO_FLAG></REQUEST>
 

```

the current awk command that i am using provides the output truncating all the spaces as below, the spaces in the tag

```nohighlight
<?xml version="1.0" ?>

```

are removed:

```nohighlight
<?xmlversion="1.0"?><REQUEST><ID>XXX</ID><TIMESTAMP>20090720062610</TIMESTAMP><FLAG>Y</FLAG><TO_FLAG>Y</TO_FLAG></REQUEST>

```

the awk command i am using:

```nohighlight
awk '$1=$1' RS='' OFS='' test_trans.txt > SEQS_TRANS_test.txt

```

Help appreciated , Thanks in Advance.

---

<div class="post-metadata">

**Author:** ![zaxxon](https://community.unix.com/user_avatar/community.unix.com/zaxxon/32/660_2.png) [@zaxxon](https://community.unix.com/u/zaxxon)\
**Post date:** [July 21, 2009, 10:04am UTC](https://community.unix.com/t/transpose-using-awk/245102/2 "2009-07-21T10:04:04Z")

</div>

Edit your post and use CODE tags when displaying code, data or logs to enhance readability and to preserve formatting like indention etc.,ty.

---

<div class="post-metadata">

**Author:** ![rakeshawasthi](https://community.unix.com/user_avatar/community.unix.com/rakeshawasthi/32/224_2.png) [@rakeshawasthi](https://community.unix.com/u/rakeshawasthi)\
**Post date:** [July 21, 2009, 10:04am UTC](https://community.unix.com/t/transpose-using-awk/245102/3 "2009-07-21T10:04:05Z")

</div>

Try:

```nohighlight
awk '{printf $0} END {printf "\n"}' file

```

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [July 21, 2009, 10:12am UTC](https://community.unix.com/t/transpose-using-awk/245102/4 "2009-07-21T10:12:37Z")

</div>

To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags

````auto
```text
 and 
```

````

by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums

---

<div class="post-metadata">

**Author:** ![new\_ds\_man](https://community.unix.com/letter_avatar/new_ds_man/32/6_449c533f586939b6e5f246632a46421f.png) [@new\_ds\_man](https://community.unix.com/u/new_ds_man)\
**Post date:** [July 21, 2009, 11:58am UTC](https://community.unix.com/t/transpose-using-awk/245102/5 "2009-07-21T11:58:13Z")

</div>

Moderators, I will keep in mind not to post those again.

Thanks for the response rakeshawasti......

But there is a space at every new line.

like below:

```nohighlight
[?xml version="1.0" ?][REQUEST] [ID>497[/ID] [TIMESTAMP]20090720062610[TIMESTAMP] [FLAG]Y[/FLAG] [TO_FLAG]Y[/TO_FLAG][/REQUEST]

```

can we eliminate those spaces as well.

---

<div class="post-metadata">

**Author:** ![Franklin52](https://community.unix.com/user_avatar/community.unix.com/franklin52/32/500_2.png) [@Franklin52](https://community.unix.com/u/Franklin52)\
**Post date:** [July 21, 2009, 12:19pm UTC](https://community.unix.com/t/transpose-using-awk/245102/6 "2009-07-21T12:19:37Z")

</div>

Try this:

```nohighlight
awk '1' ORS= file

```

If you want a newline at the end:

```nohighlight
awk '{print} END{print "\n"}' ORS= file

```

---

<div class="post-metadata">

**Author:** ![new\_ds\_man](https://community.unix.com/letter_avatar/new_ds_man/32/6_449c533f586939b6e5f246632a46421f.png) [@new\_ds\_man](https://community.unix.com/u/new_ds_man)\
**Post date:** [July 21, 2009, 6:12pm UTC](https://community.unix.com/t/transpose-using-awk/245102/7 "2009-07-21T18:12:17Z")

</div>

Still struggling with it, i am posting a complete picture of what i am facing trouble with......

The XML available for me is in the below format:

```nohighlight
<?xml version="1.0" ?>
<REQUEST>
 <ID>874</ID>
 <TIMESTAMP>20090720062610</TIMESTAMP>
 <FLAG>Y</FLAG>
 <TO_FLAG>Y</TO_FLAG>
</REQUEST>
 
<?xml version="1.0" ?>
<REQUEST>
 <ID>000</ID>
 <TIMESTAMP>20090720061218</TIMESTAMP>
 <FLAG>Y</FLAG>
 <TO_FLAG>Y</TO_FLAG>
</REQUEST>

```

I need that to be transposed into below:

```nohighlight
<?xml version="1.0" ?><REQUEST><ID>874</ID><TIMESTAMP>20090720062610</TIMESTAMP><FLAG>Y</FLAG><TO_FLAG>Y</TO_FLAG></REQUEST>
<?xml version="1.0" ?><REQUEST><ID>000</ID><TIMESTAMP>20090720061218</TIMESTAMP><FLAG>Y</FLAG><TO_FLAG>Y</TO_FLAG></REQUEST>

```

As mentioned in the above every new XML starts with the tag

```nohighlight
<?xml version="1.0" ?>

```

Also something to be noted is that the XML tag contains spaces which should stay.

```nohighlight
<?xml version="1.0" ?>

```

Thanks in Advance.

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [July 21, 2009, 6:22pm UTC](https://community.unix.com/t/transpose-using-awk/245102/8 "2009-07-21T18:22:58Z")

</div>

```nohighlight
nawk '
   BEGIN {
      RS=FS=OFS=""
   }
   {for(i=2;i<=NF;i++) gsub(" ", "", $i); print}' myFile

```

---

<div class="post-metadata">

**Author:** ![ryandegreat25](https://community.unix.com/letter_avatar/ryandegreat25/32/6_449c533f586939b6e5f246632a46421f.png) [@ryandegreat25](https://community.unix.com/u/ryandegreat25)\
**Post date:** [July 21, 2009, 9:22pm UTC](https://community.unix.com/t/transpose-using-awk/245102/9 "2009-07-21T21:22:36Z")

</div>

try..

```nohighlight
-bash-3.2$ cat test
<?xml version="1.0" ?>
<REQUEST>
 <ID>874</ID>
 <TIMESTAMP>20090720062610</TIMESTAMP>
 <FLAG>Y</FLAG>
 <TO_FLAG>Y</TO_FLAG>
</REQUEST>

<?xml version="1.0" ?>
<REQUEST>
 <ID>000</ID>
 <TIMESTAMP>20090720061218</TIMESTAMP>
 <FLAG>Y</FLAG>
 <TO_FLAG>Y</TO_FLAG>
</REQUEST>
-bash-3.2$ echo `cat test` | sed 's/> </></g'
<?xml version="1.0" ?><REQUEST><ID>874</ID><TIMESTAMP>20090720062610</TIMESTAMP><FLAG>Y</FLAG><TO_FLAG>Y</TO_FLAG></REQUEST><?xml version="1.0" ?><REQUEST><ID>000</ID><TIMESTAMP>20090720061218</TIMESTAMP><FLAG>Y</FLAG><TO_FLAG>Y</TO_FLAG></REQUEST>
-bash-3.2$

```

---

<div class="post-metadata">

**Author:** ![new\_ds\_man](https://community.unix.com/letter_avatar/new_ds_man/32/6_449c533f586939b6e5f246632a46421f.png) [@new\_ds\_man](https://community.unix.com/u/new_ds_man)\
**Post date:** [July 22, 2009, 9:58am UTC](https://community.unix.com/t/transpose-using-awk/245102/10 "2009-07-22T09:58:19Z")

</div>

ryandegreat....thanks for the code, but i need the every \<xml version...\> tag in new line.

---

<div class="post-metadata">

**Author:** ![rubin](https://community.unix.com/user_avatar/community.unix.com/rubin/32/743_2.png) [@rubin](https://community.unix.com/u/rubin)\
**Post date:** [July 22, 2009, 2:34pm UTC](https://community.unix.com/t/transpose-using-awk/245102/11 "2009-07-22T14:34:24Z")

</div>

> [@new\\\_ds\_man](#):
>
> Still struggling with it, i am posting a complete picture of what i am facing trouble with......
> 
> The XML available for me is in the below format:
> 
> ```plaintext
> <?xml version="1.0" ?>
> <REQUEST>
> <ID>874</ID>
> <TIMESTAMP>20090720062610</TIMESTAMP>
> <FLAG>Y</FLAG>
> <TO_FLAG>Y</TO_FLAG>
> </REQUEST>
>  
> <?xml version="1.0" ?>
> <REQUEST>
> <ID>000</ID>
> <TIMESTAMP>20090720061218</TIMESTAMP>
> <FLAG>Y</FLAG>
> <TO_FLAG>Y</TO_FLAG>
> </REQUEST>
> 
> ```
> 
> ...  
> As mentioned in the above every new XML starts with the tag
> 
> ```plaintext
> <?xml version="1.0" ?>
> 
> ```
> 
> Also something to be noted is that the XML tag contains spaces which should stay.
> 
> ```plaintext
> <?xml version="1.0" ?>
> 
> ```
> 
> ...

Assuming the xml tag format will remain ( as described ) constant: \<?xml + spaces + version="Nr-dot-Nr" + spaces + ?\> , this might be enough:

```plaintext
awk '/^<\?xml( |\t)+version="[0-9]+\.[0-9]+"( |\t)+\?>$/ && NR>1{ printf RS $0; next}
      NF { sub(/^[\t]+/,""); print } END{ print RS }' ORS= filename

```

Output:

```plaintext
<?xml version="1.0" ?><REQUEST><ID>874</ID><TIMESTAMP>20090720062610</TIMESTAMP><FLAG>Y</FLAG><TO_FLAG>Y</TO_FLAG></REQUEST>
<?xml version="1.0" ?><REQUEST><ID>000</ID><TIMESTAMP>20090720061218</TIMESTAMP><FLAG>Y</FLAG><TO_FLAG>Y</TO_FLAG></REQUEST>

```

---

<div class="post-metadata">

**Author:** ![Franklin52](https://community.unix.com/user_avatar/community.unix.com/franklin52/32/500_2.png) [@Franklin52](https://community.unix.com/u/Franklin52)\
**Post date:** [July 22, 2009, 2:39pm UTC](https://community.unix.com/t/transpose-using-awk/245102/12 "2009-07-22T14:39:18Z")

</div>

Try this:

```nohighlight
awk 'BEGIN{RS=ORS="\n\n"} $1=$1' file

```

Regards

---

<div class="post-metadata">

**Author:** ![ryandegreat25](https://community.unix.com/letter_avatar/ryandegreat25/32/6_449c533f586939b6e5f246632a46421f.png) [@ryandegreat25](https://community.unix.com/u/ryandegreat25)\
**Post date:** [July 22, 2009, 8:49pm UTC](https://community.unix.com/t/transpose-using-awk/245102/13 "2009-07-22T20:49:22Z")

</div>

try this from franklin

```nohighlight
awk 'BEGIN{RS=ORS="\n\n"} $1=$1' test | grep . | sed 's/> </></g'

```

or

```nohighlight
echo `cat test`| sed -e 's|</REQUEST> |&\n|g' -e 's|> <|><|g'

```

---

<div class="post-metadata">

**Author:** ![edidataguy](https://community.unix.com/letter_avatar/edidataguy/32/6_449c533f586939b6e5f246632a46421f.png) [@edidataguy](https://community.unix.com/u/edidataguy)\
**Post date:** [July 22, 2009, 9:55pm UTC](https://community.unix.com/t/transpose-using-awk/245102/14 "2009-07-22T21:55:03Z")

</div>

```nohighlight
 
sed 's/^ *//' file.xml | tr -d '\x0A'

```
