Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split sentences into separate lines

Tags:

bash

awk

tr

I'm trying to split sentences in a file into separate lines using a shell script.

Now I would like to split the strings by !, ? or . . The output should be like this :

The file that I want to read from my_text.txt and contains

you want to learn shell script? First, you want to learn Linux command! then. you can learn shell script.

Now I would like to split the strings by " ! " or "? " or "." The output should be like this :

you want to learn shell script                 
First, you want to learn Linux command             
then           
you can learn shell script

I used this script :

while read p
do
   echo $p | tr "? ! ." "\n " 
done < my_text.txt

But the output is:

you want to learn shell script

First, you want to learn Linux command then you can learn shell script

Can somebody help?

like image 626
Abdallah_98 Avatar asked Dec 16 '20 09:12

Abdallah_98


People also ask

How do you split sentences?

During post-processing, if one sentence ends with a question or exclamation mark followed by a double quote, and the other sentence begins with a lower case letter, then these sentences are joined together.

How do you split a paragraph in a sentence?

How would you split it into individual sentences, each forming its own mini paragraph? Obviously, if we are talking about a single paragraph with a few sentences, the answer is no brainer: you do it manually by placing your cursor at the end of each sentence and pressing the ENTER key twice.

What is a split sentence in writing?

A split subject is a construction in which the subject of a sentence appears to consist of two parts that do not appear next to each other.


Video Answer


1 Answers

This could be done in a single awk using its global substitution option as follows, written and tested with shown samples only in GNU awk. Simply globally substituting ?,!,. with new line(by default ORS(output record separator) value as new line).

awk '{gsub(/\?|!|\./,ORS)} 1' Input_file
like image 65
RavinderSingh13 Avatar answered Nov 15 '22 23:11

RavinderSingh13