Split sentences into separate lines

Awk is ideal for this:

awk -F '[?.!]' '{ for (i=1;i<=NF;i++) { print $i } }' file

Set the field delimiters to ? or . or ! and then loop through each field and print the entry.


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

$ sed 's/[!?.]/\n/g' file
you want to learn shell script
 First, you want to learn Linux command
 then
 you can learn shell script

You can call 3 tr commands to split for ? ! and .

cat test_string.txt | tr "!" "\n" | tr "?" "\n" | tr "." "\n"

Tags:

Bash

Awk

Tr