Home > database >  Replacing variable number of initial characters from lines using sed
Replacing variable number of initial characters from lines using sed

Time:11-06

Here is what I need to do. Each record in the following file consists of four lines like this:

identifier
ATGCGGGGGTATGAGATTCCCCTT
separator
9weaufijfhp0a9hpiaf$$#####@$3

There are hundreds of thousands of records in the file. The goal is to delete 'n' number of characters from only the fourth line given value in an index. The index is a variable with integers.

Example script:

while read integer
do
  sed '0~4 s/^$integer//g' input.txt
done < index

I don't think this works. Normally sed expects something like the following which will delete the first 5 characters. I need a way to generate a number of periods matching value in the index.

sed '0~4 s/^.....//g' input.txt

CodePudding user response:

Use the {n} regex quantifier

sed -E '0~4 s/^.{'"$integer"'}//' input.txt

Do some validation that the contents of the variable is actually an integer.

The pattern is anchored and can only match once: no need for the g option.

  • Related