# Concatenating sequence length to another file

**URL:** <https://community.unix.com/t/concatenating-sequence-length-to-another-file/375047>\
**Category:** UNIX for Beginners Q & A\
**Created:** [March 5, 2019, 1:38pm UTC](https://community.unix.com/t/concatenating-sequence-length-to-another-file/375047 "2019-03-05T13:38:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ibk](https://community.unix.com/letter_avatar/ibk/32/5_5575768a8748004e209b776fc1b2916d.png) [@Ibk](https://community.unix.com/u/Ibk)\
**Post date:** [March 5, 2019, 1:38pm UTC](https://community.unix.com/t/concatenating-sequence-length-to-another-file/375047/1 "2019-03-05T13:38:02Z")

</div>

I want to add the sequence length of File\_1.fa and File \_2.fa to form the form the fifth column in File\_1\_pos.txt and File\_2\_poa.txt respectively using awk and bash. Can anyone help me? Thanks

Get sequence length of each file

```nohighlight
File_1.fa
File_2.fa 

```

Add the sequence length to be the third column of File 3  
File\_1\_pos.txt

```nohighlight
File_1_pos 253 164
File_1_pos 738 827

```

File\_2\_pos.txt

```nohighlight
File_2_pos 1494 1583
File_2_pos 1785 1874

```

Expected Output  
File\_1\_pos.txt

```nohighlight
1 File_1_pos 253 164 8126
2 File_1_pos 738 827 8126

```

File\_2\_poa.txt

```nohighlight
1 File_2_pos 1494 1583 9655
2 File_2_pos 1785 1874 9655

```

I tried this but I dint get my expected output

```nohighlight
for file in *.fa; do a=`awk '/^>/ {if (seqlen){print seqlen};next; } { seqlen += length($0)}END{print seqlen}' $file` | awk -F, '{$1=++i OFS"\t" $1;}1' ${file%.*}pos.txt | awk 'BEGIN{OFS="\t"}{print $1,$2,$3,$4,a}'; done

```

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/5_5575768a8748004e209b776fc1b2916d.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [March 5, 2019, 2:20pm UTC](https://community.unix.com/t/concatenating-sequence-length-to-another-file/375047/2 "2019-03-05T14:20:24Z")

</div>

Please post input file samples. How do you calculate the "sequence length"? Is there one or more of them per file?

---

<div class="post-metadata">

**Author:** ![Ibk](https://community.unix.com/letter_avatar/ibk/32/5_5575768a8748004e209b776fc1b2916d.png) [@Ibk](https://community.unix.com/u/Ibk)\
**Post date:** [March 5, 2019, 2:42pm UTC](https://community.unix.com/t/concatenating-sequence-length-to-another-file/375047/3 "2019-03-05T14:42:30Z")

</div>

These are examples of the input sequence. Just one long sequence per file. thanks  
\>File\_1.fa

```nohighlight
TTGAAAGGGGGCCCGGGGGATCTCCCCCGCGGTAACTGGTCACAGTTGCCGCGGACGGAGATCATCCCCC
GGTTACCCCCTTTCGACGCGGGTACTGCGATAGTGCCACCCCAGTCCTTCCTACTCCCGACTCCCGACCC
CAACCCAGGTTCCTTGGAACAGGAACACCAATTTATTCATCCCTTGGATGCTGACTAATCAGAGGAACGT
CAGCATTTTCCGGCCCAGGCTAAGAGAAGTAGATAAGTTAGAATCTAAATTATTTATCATCCCCTTGACG
AATTCGCGTTGGAAAAGCACCTCTCACTTGCCGCTCTTCACACCCATCATTCTAATTCGGCCCCTGTGTT

```

\>File\_2.fa

```nohighlight
GAGCCCCTTGTTGAAGTGTTTCCCTCCATCGCGACGTGGTTGGAGATCTAAGTTAACCGACTCCGACGAA
ACTACCATCATGCCTCCCCGATTATGTGATGCTTTCTGCCCTGCTGGGTGGAGCATCCTCGGGTTGAGAA
ATCTTTCTTCCTTTTACCTTGGACTCCGGTCCCCCGGTCTAAGCCGCTTGGAATAAGACAGGGTTATCTT
CACTCCTCTTCTTTTCTACTTCACAGTGTTCTATGCTGTGAAAGGGTATGTGTCGCCCCTTCCTTCTTCG

```

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/5_5575768a8748004e209b776fc1b2916d.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [March 5, 2019, 4:59pm UTC](https://community.unix.com/t/concatenating-sequence-length-to-another-file/375047/4 "2019-03-05T16:59:34Z")

</div>

Try

```nohighlight
$ wc -cl *.fa | awk '
FILENAME == "-" {sub (".fa", "", $3)
                 T[$3] = $2 - $1
                 next
                }
FNR == 1 {IX = FILENAME
                 sub (/_[^_]*\..*$/, "", IX)
                }

                {print FNR, $0, T[IX] > (FILENAME ".new")
                }
' - OFS="\t" fil*pos.txt

$ cf *.new

---------- file_1_pos.txt.new: ----------

1 File_1_pos 253 164 350
2 File_1_pos 738 827 350

---------- file_2_pos.txt.new: ----------

1 File_2_pos 1494 1583 280
2 File_2_pos 1785 1874 280
 

```

Copy exactly as given; then `mv` the ".new" files over the old ".txt" files

EDIT: Given there are any number of `.fa` files, and each has a corresponding `_pos.txt` file, you could try

```nohighlight
$ wc -cl *.fa |  
awk '
FILENAME == "-" {if ($3 == "total") next
                 sub (".fa", "", $3)
                 T[$3] = $2 - $1
                 ARGV[ARGC++] = $3 "_pos.txt"
                 next
                }
FNR == 1 {IX = FILENAME
                 sub (/_[^_]*\..*$/, "", IX)
                }
                {print FNR, $0, T[IX] > (FILENAME ".new")
                }
' - OFS="\t" 

```

---

<div class="post-metadata">

**Author:** ![Ibk](https://community.unix.com/letter_avatar/ibk/32/5_5575768a8748004e209b776fc1b2916d.png) [@Ibk](https://community.unix.com/u/Ibk)\
**Post date:** [March 7, 2019, 8:37am UTC](https://community.unix.com/t/concatenating-sequence-length-to-another-file/375047/5 "2019-03-07T08:37:31Z")

</div>

Thank you Rudic, I have tried the code but did not give me the required output. The input files are separate files as well as the expected output.

---

<div class="post-metadata">

**Author:** ![rdrtx1](https://community.unix.com/user_avatar/community.unix.com/rdrtx1/32/2721_2.png) [@rdrtx1](https://community.unix.com/u/rdrtx1)\
**Post date:** [March 7, 2019, 3:14pm UTC](https://community.unix.com/t/concatenating-sequence-length-to-another-file/375047/6 "2019-03-07T15:14:42Z")

</div>

```nohighlight
for file in *.fa;
do
  awk '
  NR==FNR {
     if ($0 ~ /^>/ && why_print_seqlen_for_this_line) {if (seqlen) print seqlen; next;}
     # is there a record separator /^>/ in the *.fa files not mentioned in samples?

     seqlen += length($0);
     next;
  }
  {print FNR, $0, seqlen}
  ' OFS="\t" $file ${file%.*}_pos.txt > t_$$

  mv -f t_$$ ${file%.*}_pos.txt
done

```

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/5_5575768a8748004e209b776fc1b2916d.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [March 7, 2019, 4:22pm UTC](https://community.unix.com/t/concatenating-sequence-length-to-another-file/375047/7 "2019-03-07T16:22:23Z")

</div>

> [@ibk](#):
>
> ... The input files are separate files as well as the expected output.

So - which files exist, and by what feature are the files connected / related?
