# Parsing out data with multiple field separators

**URL:** <https://community.unix.com/t/parsing-out-data-with-multiple-field-separators/368852>\
**Category:** Shell Programming and Scripting\
**Created:** [October 17, 2017, 11:49am UTC](https://community.unix.com/t/parsing-out-data-with-multiple-field-separators/368852 "2017-10-17T11:49:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ncwxpanther](https://community.unix.com/letter_avatar/ncwxpanther/32/6_449c533f586939b6e5f246632a46421f.png) [@ncwxpanther](https://community.unix.com/u/ncwxpanther)\
**Post date:** [October 17, 2017, 11:49am UTC](https://community.unix.com/t/parsing-out-data-with-multiple-field-separators/368852/1 "2017-10-17T11:49:44Z")

</div>

I have a large file that I need to print certain sections out of.

file.txt

```nohighlight
/alpha/beta/delta/gamma/425/590/USC00015420.blah.lt.0.01.str:USC00015420Y2017M10BLALT.01 12 13 14 -9 1 -9 -9 -9 -9 -9 1 2 3 4 5 -9 -9

```

I need to print the "USC00015420" and field 16 (in this case "5").

The code I was hoping would work was

```nohighlight
 awk -F" " '{ printf substr($1,53,57)};{print (" "$16)}' file.txt

```

But it prints all the content within the periods (".") .

I need the final output to be

```nohighlight
USC00015420 5

```

---

<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:** [October 17, 2017, 11:53am UTC](https://community.unix.com/t/parsing-out-data-with-multiple-field-separators/368852/2 "2017-10-17T11:53:38Z")

</div>

This seems to work:

```nohighlight
awk '{ print substr($1,62,11) " " $16}' file.txt

```

The third parameter in substr is the number of characters that you want to print, not the position.

Alternatives based on field separators:

```nohighlight
awk '{ split($1,F,".*/|\\."); print F[2] " " $16}' file.txt

```

or

```nohighlight
awk '{ split($1,F,/.*\/|\./); print F[2] " " $16}' file.txt

```

or

```nohighlight
awk '{ s=$1; gsub(".*/|\\..*",x,s); print s " " $16}' file.txt

```

---

<div class="post-metadata">

**Author:** ![RavinderSingh13](https://community.unix.com/letter_avatar/ravindersingh13/32/6_449c533f586939b6e5f246632a46421f.png) [@RavinderSingh13](https://community.unix.com/u/RavinderSingh13)\
**Post date:** [October 17, 2017, 1:07pm UTC](https://community.unix.com/t/parsing-out-data-with-multiple-field-separators/368852/3 "2017-10-17T13:07:39Z")

</div>

Hello ncwxpanther,

Could you please try following too and let me know if this helps you.  
Solution 1st:

```nohighlight
awk '{split($1,a,"/");sub(/\..*/,"",a[8]);print a[8]"\t"$16}' Input_file

```

Solution 2nd:

```nohighlight
awk -F'[/.]' '{print $8"\t"$65}' Input_file

```

Thanks,  
R. Singh

---

<div class="post-metadata">

**Author:** ![bakunin](https://community.unix.com/letter_avatar/bakunin/32/6_449c533f586939b6e5f246632a46421f.png) [@bakunin](https://community.unix.com/u/bakunin)\
**Post date:** [October 17, 2017, 1:39pm UTC](https://community.unix.com/t/parsing-out-data-with-multiple-field-separators/368852/4 "2017-10-17T13:39:23Z")

</div>

> [@ncwxpanther](#):
>
> I have a large file that I need to print certain sections out of.

The basic problem is that you need to define what a "section" is: the line you presented uses different separators to delimit what i suppose is a "section" in your wording: spaces (or maybe tabs), dots, slashes and colons.

Will these four (five) characters always be delimiters?

Will the lines always have the same structure? (like "First 7 parts separated by "/", the seventh part consists of 6 parts separated by ".", then a colon, then ....")

If you could answer these questions we could perhaps provide better solutions which might work better. Without this information we will never be sure to have really solved the problem.

I hope this helps.

bakunin

---

<div class="post-metadata">

**Author:** ![ncwxpanther](https://community.unix.com/letter_avatar/ncwxpanther/32/6_449c533f586939b6e5f246632a46421f.png) [@ncwxpanther](https://community.unix.com/u/ncwxpanther)\
**Post date:** [October 17, 2017, 1:56pm UTC](https://community.unix.com/t/parsing-out-data-with-multiple-field-separators/368852/5 "2017-10-17T13:56:32Z")

</div>

> [@scrutinizer](#):
>
> This seems to work:
> 
> ```plaintext
> awk '{ print substr($1,62,11) " " $16}' file.txt
> 
> ```
> 
> The third parameter in substr is the number of characters that you want to print, not the position.

Thanks!  
I was able to get this to perform as expected

```plaintext
awk '{ print substr($1,53,11) " " $16}' file.txt

```

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [October 17, 2017, 2:09pm UTC](https://community.unix.com/t/parsing-out-data-with-multiple-field-separators/368852/6 "2017-10-17T14:09:56Z")

</div>

Certainly NOT with the sample you gave in post#1

> [@ncwxpanther](#):
>
> .  
> .  
> .
> 
> ```plaintext
> /alpha/beta/delta/gamma/425/590/USC00015420.blah.lt.0.01.str:USC00015420Y2017M10BLALT.01 12 13 14 -9 1 -9 -9 -9 -9 -9 1 2 3 4 5 -9 -9
> 
> ```
> 
> .  
> .  
> .
