0

I don't know if this can be done with simple Unix commands to implement it. Because it looks a bit complicated.

I have a text that looks like the following.

<p id="mt">Iusto, numquam dolore aut voluptates delectus</p>
<p id="mt">Lorem ipsum dolor sit amet</p>
<p id="mt">Facere vitae sapiente necessitatibus</p>
<p id="mt">Tempora modi rem reprehenderit quam eos. Provident, animi ab ducimus dolorem</p>

Each line has different character lengths, and some content is too long and requires omission of excess portions.

For example, limit the <p id="mt">...</p> content to 20 characters and delete them if you go beyond that. It looks like this.

<p id="mt">Iusto, numquam dolor</p>
<p id="mt">Lorem ipsum dolor si</p>
<p id="mt">Facere vitae sapient</p>
<p id="mt">Tempora modi rem rep</p>
2

Using sed:

sed -E 's/^(<p id="mt">.{20}).*(<\/p>$)/\1\2/' infile
2
  • This will print the </p> twice in case the length is in the range 16...19. Sep 27 '20 at 20:56
  • @RakeshSharma fixed, thanks Sep 28 '20 at 5:15
1

command

awk -F ">" '{print $2}' filename| awk -F "<" '{print "<p id=\"mt\">"substr($1,1,20)"</p>"}' 

output

<p id="mt">Iusto, numquam dolor</p>
<p id="mt">Lorem ipsum dolor si</p>
<p id="mt">Facere vitae sapient</p>
<p id="mt">Tempora modi rem rep</p>




Python

m=open('filename','r')
for g in m:
    e=g.split('>')[1].split('<')[0][0:20]
    print "<p id=\"mt\"> {0}</p>".format(e)

output

<p id="mt"> Iusto, numquam dolor</p>
<p id="mt"> Lorem ipsum dolor si</p>
<p id="mt"> Facere vitae sapient</p>
<p id="mt"> Tempora modi rem rep</p>

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.