# Parse string

**URL:** <https://community.unix.com/t/parse-string/248721>\
**Category:** Shell Programming and Scripting\
**Created:** [September 6, 2009, 2:19pm UTC](https://community.unix.com/t/parse-string/248721 "2009-09-06T14:19:07Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![ktm](https://community.unix.com/letter_avatar/ktm/32/6_449c533f586939b6e5f246632a46421f.png) [@ktm](https://community.unix.com/u/ktm)\
**Post date:** [September 6, 2009, 2:19pm UTC](https://community.unix.com/t/parse-string/248721/1 "2009-09-06T14:19:07Z")

</div>

Hi, I need to parse a string, check if there are periods and strip the string.  
For example i have the following domains and subdomains: [mydomain.com](http://mydomain.com), [dev.mydomain.com](http://dev.mydomain.com)

I need to strip all periods so i have a string without periods or domain extensions: mydomain, devmydomain.

I use this for domains but i don't know how to do it for subdomains also

```plaintext
dom=(`echo $i | tr '.' ' '`)

```

Thank you

---

<div class="post-metadata">

**Author:** ![ripat](https://community.unix.com/letter_avatar/ripat/32/6_449c533f586939b6e5f246632a46421f.png) [@ripat](https://community.unix.com/u/ripat)\
**Post date:** [September 6, 2009, 3:14pm UTC](https://community.unix.com/t/parse-string/248721/2 "2009-09-06T15:14:33Z")

</div>

With awk:

```nohighlight
echo "dev.mydomain.com" | awk -F"." '{print $(NF-1)}'

```

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [September 6, 2009, 3:23pm UTC](https://community.unix.com/t/parse-string/248721/3 "2009-09-06T15:23:51Z")

</div>

```nohighlight
dom=${i//.}

```

---

<div class="post-metadata">

**Author:** ![ktm](https://community.unix.com/letter_avatar/ktm/32/6_449c533f586939b6e5f246632a46421f.png) [@ktm](https://community.unix.com/u/ktm)\
**Post date:** [September 6, 2009, 3:25pm UTC](https://community.unix.com/t/parse-string/248721/4 "2009-09-06T15:25:04Z")

</div>

Unfortunately it doesn't work because I don't know if will be a domain or a subdomain. If the output is:

> **[Web Hosting, Domain Name Registration - MyDomain.com](https://www.mydomain.com/)**
>
> Small business web hosting offering additional business services such as: domain name registrations, email accounts, web services, and various small business solutions.

[dev.mydomain.com](http://dev.mydomain.com)

I need to parse each string and strip the domain and periods.It works with tr but the problem is how do i check if it's a subdomain.For the two strings above it should result:

mydomain  
devmydomain

---

<div class="post-metadata">

**Author:** ![ripat](https://community.unix.com/letter_avatar/ripat/32/6_449c533f586939b6e5f246632a46421f.png) [@ripat](https://community.unix.com/u/ripat)\
**Post date:** [September 6, 2009, 3:38pm UTC](https://community.unix.com/t/parse-string/248721/5 "2009-09-06T15:38:48Z")

</div>

> [@ktm](#):
>
> Unfortunately it doesn't work because I don't know if will be a domain or a subdomain.

Yes it does work. Did you try?

```plaintext
$ echo "dev.mydomain.com" | awk -F"." '{print $(NF-1)}'
mydomain
$ echo "mydomain.com" | awk -F"." '{print $(NF-1)}'
mydomain

```

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [September 6, 2009, 3:48pm UTC](https://community.unix.com/t/parse-string/248721/6 "2009-09-06T15:48:21Z")

</div>

Oops misread you requirements...  
I guess you would need two statements

```nohighlight
j=${i/.com}
dom=${j//.}

```

or

```nohighlight
dom=$(echo $i|sed -e's/\.com//;s/\.//g')

```

---

<div class="post-metadata">

**Author:** ![ktm](https://community.unix.com/letter_avatar/ktm/32/6_449c533f586939b6e5f246632a46421f.png) [@ktm](https://community.unix.com/u/ktm)\
**Post date:** [September 6, 2009, 4:16pm UTC](https://community.unix.com/t/parse-string/248721/7 "2009-09-06T16:16:31Z")

</div>

> [@scrutinizer](#):
>
> Oops misread you requirements...  
> I guess you would need two statements
> 
> ```plaintext
> j=${i/.com}
> dom=${j//.}
> 
> ```
> 
> or
> 
> ```plaintext
> dom=$(echo $i|sed -e's/\.com//;s/\.//g')
> 
> ```

Thank you, it works but for different domain extensions like .net, .org ..ow do I do it ? I don't know what domain extension would be, it could be .net or aomething else

---

<div class="post-metadata">

**Author:** ![tukuyomi](https://community.unix.com/letter_avatar/tukuyomi/32/6_449c533f586939b6e5f246632a46421f.png) [@tukuyomi](https://community.unix.com/u/tukuyomi)\
**Post date:** [September 6, 2009, 4:43pm UTC](https://community.unix.com/t/parse-string/248721/8 "2009-09-06T16:43:17Z")

</div>

```nohighlight
$ dom="mydomain.com"; echo ${dom%.*} | sed 's/\.//g'
mydomain

$ dom="dev.mydomain.com"; echo ${dom%.*} | sed 's/\.//g'
devmydomain

```

---

<div class="post-metadata">

**Author:** ![ktm](https://community.unix.com/letter_avatar/ktm/32/6_449c533f586939b6e5f246632a46421f.png) [@ktm](https://community.unix.com/u/ktm)\
**Post date:** [September 6, 2009, 4:48pm UTC](https://community.unix.com/t/parse-string/248721/9 "2009-09-06T16:48:20Z")

</div>

> [@tukuyomi](#):
>
> ```plaintext
> $ echo "dev.mydomain.com" | sed 's/\.//g'
> devmydomaincom
> $ echo "mydomain.com" | sed 's/\.//g'
> mydomaincom
> 
> ```

Thank you but I think you misunderstood, without the domain extension, without .com, .net, .org or other domain extension. it must be something generic because I don't know the domain extension.  
The examples above should be:  
devmydomain (without com)  
mydomain (without com)

---

<div class="post-metadata">

**Author:** ![Vi-Curious](https://community.unix.com/letter_avatar/vi-curious/32/6_449c533f586939b6e5f246632a46421f.png) [@Vi-Curious](https://community.unix.com/u/Vi-Curious)\
**Post date:** [September 6, 2009, 4:48pm UTC](https://community.unix.com/t/parse-string/248721/10 "2009-09-06T16:48:34Z")

</div>

```nohighlight
> cat infile
dev.mydomain.com
mydomain.com
usca.berkeley.edu

```

```nohighlight
perl -ne '@_ = split /\./;for ($i=0;$i<scalar(@_)-1;$i++) {print @_[$i]};print "\n"' infile

```

```nohighlight
> perl -ne '@_ = split /\./;for ($i=0;$i<scalar(@_)-1;$i++) {print @_[$i]};print "\n"' infile
devmydomain
mydomain
uscaberkeley

```

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [September 6, 2009, 4:58pm UTC](https://community.unix.com/t/parse-string/248721/11 "2009-09-06T16:58:37Z")

</div>

> [@ktm](#):
>
> Thank you, it works but for different domain extensions like .net, .org ..ow do I do it ? I don't know what domain extension would be, it could be .net or aomething else

Well,

```plaintext
j=${i%.*}
dom=${j//.}

```

or

```plaintext
dom=$(echo $i|sed -e's/\.[^.]*$//;s/\.//g')

```

or

```plaintext
dom=$(echo ${i%.*}|sed -e's/\.//g')

```

Would take care of that. But there are also various top level domains in two parts, e.g. .co.uk or .co.au. You would have to write exceptions for those cases if you want to cover everything.

---

<div class="post-metadata">

**Author:** ![tukuyomi](https://community.unix.com/letter_avatar/tukuyomi/32/6_449c533f586939b6e5f246632a46421f.png) [@tukuyomi](https://community.unix.com/u/tukuyomi)\
**Post date:** [September 6, 2009, 5:00pm UTC](https://community.unix.com/t/parse-string/248721/12 "2009-09-06T17:00:40Z")

</div>

> [@ktm](#):
>
> Thank you but I think you misunderstood, without the domain extension, without .com, .net, .org or other domain extension. it must be something generic because I don't know the domain extension.  
> The examples above should be:  
> devmydomain (without com)  
> mydomain (without com)

Yes, I edited my post right after posting it, sorry :X  
[http://www.unix.com/shell-programming-scripting/118647-parse-string-2.html\#post302350975](http://www.unix.com/shell-programming-scripting/118647-parse-string-2.html%5C#post302350975)

---

<div class="post-metadata">

**Author:** ![ktm](https://community.unix.com/letter_avatar/ktm/32/6_449c533f586939b6e5f246632a46421f.png) [@ktm](https://community.unix.com/u/ktm)\
**Post date:** [September 6, 2009, 5:10pm UTC](https://community.unix.com/t/parse-string/248721/13 "2009-09-06T17:10:34Z")

</div>

Thank you all, it works now 🙂 .Problem solved, great forum and users
