Linux bash - split files into 2 words

I put together this one-line font that prints all the words in a file on different lines: sed -e 's / [^ a-zA-Z] / \ n / g' test_input | grep -v "^ $"

If test_input contains “My bike is fast and clean,” a single-layer output would be:
My
bike
is
fast
and
clean

Now I need another version that prints all words from 2 words in the text, like this (even with Bash):
My bike
bike
fast | quick and fast and clean

Do you know how to do this?

+3
source share
5 answers

Connect the word file to this standard script.

#! bash
last_word=""
while read word
do
  if [ $last_word != "" ] ; then
      echo $last_word $word
  fi
  last_word=$word
done
+1

:

paste  <(head -n -1 test.dat) <(tail +2 test.dat)
+1

awk ,

$ echo "My bike is fast and clean" | awk '{for(i=1;i<NF;i++){printf "%s %s\n",$i,$(i+1) } }'
My bike
bike is
is fast
fast and
and clean
+1

, , GNU sed , , :

sed 's/[[:blank:]]*\<\(\w\+\)\>/\1 \1\n/g; s/[^ ]* \([^\n]*\)\n\([^ ]*\)/\1 \2\n/g; s/ \n//; s/\n[^ ]\+$//' inputfile
0
source

add to the command:

| awk '(PREV!="") {printf "%s %s\n", PREV, $1} {PREV=$1}'
0
source

Source: https://habr.com/ru/post/1751046/


All Articles