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 Answers 2

2

Using sed:

sed -E 's/^(<p id="mt">.{20}).*(<\/p>$)/\1\2/' infile 
1
  • This will print the </p> twice in case the length is in the range 16...19. Commented Sep 27, 2020 at 20:56
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> 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.