0

I have a text that looks like the following.

<DIV>SOFTWARE V1.0.1.0.RDZCUAJ DOWNLOAD</DIV>
<DIV>...</DIV>
<DIV>...</DIV>
<DIV>SOFTWARE V1.0.2.0.VWZMXQE DOWNLOAD</DIV>
<DIV>...</DIV>
<DIV>...</DIV>
<DIV>SOFTWARE V1.0.3.0.GSVZQKE DOWNLOAD</DIV>
<DIV>...</DIV>
<DIV>...</DIV>
<DIV>SOFTWARE V1.0.4.0.UIUVAZD DOWNLOAD</DIV>
<DIV>...</DIV>
<DIV>...</DIV>
<DIV>SOFTWARE V1.0.5.0.ELBXBGB DOWNLOAD</DIV>
<DIV>...</DIV>
<DIV>...</DIV>

I need to remove the 8 characters in front of DOWNLOAD</DIV>, it eventually looked like this.

<DIV>SOFTWARE V1.0.1.0 DOWNLOAD</DIV>
<DIV>...</DIV>
<DIV>...</DIV>
<DIV>SOFTWARE V1.0.2.0 DOWNLOAD</DIV>
<DIV>...</DIV>
<DIV>...</DIV>
<DIV>SOFTWARE V1.0.3.0 DOWNLOAD</DIV>
<DIV>...</DIV>
<DIV>...</DIV>
<DIV>SOFTWARE V1.0.4.0 DOWNLOAD</DIV>
<DIV>...</DIV>
<DIV>...</DIV>
<DIV>SOFTWARE V1.0.5.0 DOWNLOAD</DIV>
<DIV>...</DIV>
<DIV>...</DIV>

Is it possible to do this with the sed or awk command?

Any help is appreciated in advance!

4

A very simple approach is the following:

$ sed 's,.........DOWNLOAD</DIV>, DOWNLOAD</DIV>,g' input.txt
<DIV>SOFTWARE V1.0.1.0 DOWNLOAD</DIV>
<DIV>...</DIV>
<DIV>...</DIV>
<DIV>SOFTWARE V1.0.2.0 DOWNLOAD</DIV>
<DIV>...</DIV>
<DIV>...</DIV>
<DIV>SOFTWARE V1.0.3.0 DOWNLOAD</DIV>
<DIV>...</DIV>
<DIV>...</DIV>
<DIV>SOFTWARE V1.0.4.0 DOWNLOAD</DIV>
<DIV>...</DIV>
<DIV>...</DIV>
<DIV>SOFTWARE V1.0.5.0 DOWNLOAD</DIV>
<DIV>...</DIV>
<DIV>...</DIV>

That replaces 9 characters in front of DOWNLOAD</DIV> and replaces it with DOWNLOAD</DIV>

| improve this answer | |
  • +1 I was seconds too late to post the same answer :) – schrodigerscatcuriosity Sep 25 at 1:16
  • Thank you. It's working! – Matthew Sep 25 at 1:50
2

With a sed that has -E to enable EREs (e.g. GNU sed and BSD/OSX sed):

sed -E 's:.{8}( DOWNLOAD</DIV>):\1:' file

otherwise with any POSIX sed:

sed 's:.\{8\}\( DOWNLOAD</DIV>\):\1:' file

e.g.

$ sed -E 's:.{8}( DOWNLOAD</DIV>):\1:' file
<DIV>SOFTWARE V1.0.1.0 DOWNLOAD</DIV>
<DIV>...</DIV>
<DIV>...</DIV>
<DIV>SOFTWARE V1.0.2.0 DOWNLOAD</DIV>
<DIV>...</DIV>
<DIV>...</DIV>
<DIV>SOFTWARE V1.0.3.0 DOWNLOAD</DIV>
<DIV>...</DIV>
<DIV>...</DIV>
<DIV>SOFTWARE V1.0.4.0 DOWNLOAD</DIV>
<DIV>...</DIV>
<DIV>...</DIV>
<DIV>SOFTWARE V1.0.5.0 DOWNLOAD</DIV>
<DIV>...</DIV>
<DIV>...</DIV>

$ sed 's:.\{8\}\( DOWNLOAD</DIV>\):\1:' file
<DIV>SOFTWARE V1.0.1.0 DOWNLOAD</DIV>
<DIV>...</DIV>
<DIV>...</DIV>
<DIV>SOFTWARE V1.0.2.0 DOWNLOAD</DIV>
<DIV>...</DIV>
<DIV>...</DIV>
<DIV>SOFTWARE V1.0.3.0 DOWNLOAD</DIV>
<DIV>...</DIV>
<DIV>...</DIV>
<DIV>SOFTWARE V1.0.4.0 DOWNLOAD</DIV>
<DIV>...</DIV>
<DIV>...</DIV>
<DIV>SOFTWARE V1.0.5.0 DOWNLOAD</DIV>
<DIV>...</DIV>
<DIV>...</DIV>
| improve this answer | |
1

You can try this:

sed 's#SOFTWARE \(.*\)\.[A-Z]\{7\} DOWNLOAD#SOFTWARE \1 DOWNLOAD#' file
| improve this answer | |
1

Using Raku (née Perl6)

~$ raku -pe 's/ <(. ** 8)> <?before " DOWNLOAD</DIV>" $$ >  //;' download.txt
<DIV>SOFTWARE V1.0.1.0 DOWNLOAD</DIV>
<DIV>...</DIV>
<DIV>...</DIV>
<DIV>SOFTWARE V1.0.2.0 DOWNLOAD</DIV>
<DIV>...</DIV>
<DIV>...</DIV>
<DIV>SOFTWARE V1.0.3.0 DOWNLOAD</DIV>
<DIV>...</DIV>
<DIV>...</DIV>
<DIV>SOFTWARE V1.0.4.0 DOWNLOAD</DIV>
<DIV>...</DIV>
<DIV>...</DIV>
<DIV>SOFTWARE V1.0.5.0 DOWNLOAD</DIV>
<DIV>...</DIV>
<DIV>...</DIV>

The code above uses the -pe autoprint flag in conjunction with the s/// substitution operator. Inside the left-half of the s/// operator a zero-width lookahead assertion is used to find the DOWNLOAD</DIV> closing tag, and the preceding 8 characters are exactly captured (and deleted) with the <(. ** 8)> code.

HTH.

https://raku.org
https://docs.raku.org/language/regexes

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.