Issue
I have text like below
Current state of abc : RUNNING Current state of def : RUNNING Current state of ghi : RUNNING
I want this single line to be split into multiple rows like below
Current state of abc : RUNNING
Current state of def : RUNNING
Current state of ghi : RUNNING
I tried with tr command like tr ' ' '\n'
, but it prints the row after every space.
Solution
Continuing with the tr
thought:
tr -s ' ' '\n' | paste -d ' ' - - - - - -
# or, handle tabs too
tr -s '[:space:]' '\n' | paste -d ' ' - - - - - -
Or sed, inserting a newline after every 6 words
sed -E 's/([^[:space:]]+[[:space:]]+){6}/&\n/g'
The same idea in perl
perl -pe 's/(\S+\s+){6}/$&\n/g'
or GNU grep for -o
and \S/\s
:
grep -Eo '(\S+\s+){5}\S+'
or GNU awk for multi-char RS
and RT
and \S/\s
:
awk -v RS='(\\S+\\s+){5}\\S+' 'RT{print RT}'
Answered By - glenn jackman Answer Checked By - Katrina (WPSolving Volunteer)