Manish
Manish

Reputation: 3521

How to replace text using perl/shell script?

I have one pattern

cat sample.text

\newline</Text>

I'm using the following command:

sed ':a; N; $!ba; s|\newline</Text>|</Text>|g' sample.text

but following command is not working.

Upvotes: 0

Views: 195

Answers (3)

Ed Morton
Ed Morton

Reputation: 203358

Are you just trying to get rid of the newlines before </Text>? That's just a simple substitution:

gawk -vRS='\0' -vORS= '{gsub(/\n<\/Text>/,"</Text>")}1' file

Upvotes: 2

potong
potong

Reputation: 58391

This might work for you (GNU sed):

sed '$!N;s/\n\(<\/Text>\)/\1/;P;D' file

or

sed -r '$!N;s|\n(</Text>)|\1|;P;D' file

However if \newline is a literal:

sed 's/\\newline\(<\/Text>\)/\1/g' file

or

sed -r 's|\\newline(</Text>)|\1|g' file

Would work just as well.

Upvotes: 2

Manish
Manish

Reputation: 3521

I just found solution myself so thought to share with everyone.

 sed ':a; N; $!ba; s|\\newline<\/Text>|</Text>|g' sample.text

Upvotes: 0

Related Questions