Home > Enterprise >  how to concatenate the content of a file with a increment of last number of the column
how to concatenate the content of a file with a increment of last number of the column

Time:10-27

i have a file with some content and need to repeat nth time and addition to increment of last column number.

Input content of a file:
TCTA    7   ccta    TCTA    1
TCTA    8   ccta    TCTA    1
TCTA    9   ccta    TCTA    1
TCTA    10  ccta    TCTA    1
TCTA    11  ccta    TCTA    1
TCTA    12  ccta    TCTA    1
TCTA    13  ccta    TCTA    1
TCTA    14  ccta    TCTA    1
TCTA    15  ccta    TCTA    1
TCTA    16  ccta    TCTA    1
TCTA    17  ccta    TCTA    1

Expected output:
TCTA    7   ccta    TCTA    1
TCTA    8   ccta    TCTA    1
TCTA    9   ccta    TCTA    1
TCTA    10  ccta    TCTA    1
TCTA    11  ccta    TCTA    1
TCTA    12  ccta    TCTA    1
TCTA    13  ccta    TCTA    1
TCTA    14  ccta    TCTA    1
TCTA    15  ccta    TCTA    1
TCTA    16  ccta    TCTA    1
TCTA    17  ccta    TCTA    1
TCTA    7   ccta    TCTA    2
TCTA    8   ccta    TCTA    2
TCTA    9   ccta    TCTA    2
TCTA    10  ccta    TCTA    2
TCTA    11  ccta    TCTA    2
TCTA    12  ccta    TCTA    2
TCTA    13  ccta    TCTA    2
TCTA    14  ccta    TCTA    2
TCTA    15  ccta    TCTA    2
TCTA    16  ccta    TCTA    2
TCTA    17  ccta    TCTA    2
TCTA    7   ccta    TCTA    3
TCTA    8   ccta    TCTA    3
TCTA    9   ccta    TCTA    3
TCTA    10  ccta    TCTA    3
TCTA    11  ccta    TCTA    3
TCTA    12  ccta    TCTA    3
TCTA    13  ccta    TCTA    3
TCTA    14  ccta    TCTA    3
TCTA    15  ccta    TCTA    3
TCTA    16  ccta    TCTA    3
TCTA    17  ccta    TCTA    3
TCTA    7   ccta    TCTA    n
TCTA    8   ccta    TCTA    n
TCTA    9   ccta    TCTA    n
TCTA    10  ccta    TCTA    n
TCTA    11  ccta    TCTA    n
TCTA    12  ccta    TCTA    n
TCTA    13  ccta    TCTA    n
TCTA    14  ccta    TCTA    n
TCTA    15  ccta    TCTA    n
TCTA    16  ccta    TCTA    n
TCTA    17  ccta    TCTA    n

I tried following but unable to make last column increment by nth time.

for i in {1..5};do 
   cat file.txt >> out.txt
done

I need the last column to increased by nth time.

CodePudding user response:

Using a gnu-awk solution:

awk -v n=3 '
{
   rec = rec $0 RS
}
1
END {
   for (i=2; i<=n;   i)
      printf "%s", gensub(/[0-9] (\n|$)/, i "\\1", "g", rec)
}' file

TCTA    7   ccta    TCTA    1
TCTA    8   ccta    TCTA    1
TCTA    9   ccta    TCTA    1
TCTA    10  ccta    TCTA    1
TCTA    11  ccta    TCTA    1
TCTA    12  ccta    TCTA    1
TCTA    13  ccta    TCTA    1
TCTA    14  ccta    TCTA    1
TCTA    15  ccta    TCTA    1
TCTA    16  ccta    TCTA    1
TCTA    17  ccta    TCTA    1
TCTA    7   ccta    TCTA    2
TCTA    8   ccta    TCTA    2
TCTA    9   ccta    TCTA    2
TCTA    10  ccta    TCTA    2
TCTA    11  ccta    TCTA    2
TCTA    12  ccta    TCTA    2
TCTA    13  ccta    TCTA    2
TCTA    14  ccta    TCTA    2
TCTA    15  ccta    TCTA    2
TCTA    16  ccta    TCTA    2
TCTA    17  ccta    TCTA    2
TCTA    7   ccta    TCTA    3
TCTA    8   ccta    TCTA    3
TCTA    9   ccta    TCTA    3
TCTA    10  ccta    TCTA    3
TCTA    11  ccta    TCTA    3
TCTA    12  ccta    TCTA    3
TCTA    13  ccta    TCTA    3
TCTA    14  ccta    TCTA    3
TCTA    15  ccta    TCTA    3
TCTA    16  ccta    TCTA    3
TCTA    17  ccta    TCTA    3

CodePudding user response:

This might work for you (GNU parallel and sed):

parallel -q sed -E 's/\S $/$((& {}))/;s/.*/echo "&"/e' file ::: {0..2}

If you want to replace the last field by an increment, use:

parallel -q sed -E 's/\S $/{}/' file ::: {1..3}
  • Related