Reputation: 3146
I have some text like this
blue chile green morning dawn
red turkey pink morning dawn
white south africa red evening midst & more
I would like to split it like this (notice the pipe delimiter)
blue child | green morning dawn
red turkey | pink morning dawn
white south africa | red evening midst & more
There aren't fixed width so I am having trouble partitioning the text
Upvotes: 0
Views: 36
Reputation: 67507
if you want to normalize spaces in addition to the pipe delimiter
$ sed -r 's/(.{25})/\1 | /' file | tr -s ' '
blue chile | green morning dawn
red turkey | pink morning dawn
white south africa | red evening midst & more
this matches your output but note that the extra space in red turkey
is removed as well. If you want to preserve it, it will require extra steps.
Upvotes: 1
Reputation: 204064
This MIGHT be what you're looking for:
$ sed -E 's/ {3,}/ | /; s/ */ /g' file
blue chile | green morning dawn
red turkey | pink morning dawn
white south africa | red evening midst & more
The above assumes your 2 "fields" are separated by 3 or more blank chars as in your posted sample input. It will work in GNU or OSX sed for -E
to enable EREs.
Otherwise, if the fields really ARE fixed width as it appears in your posted input then for improved robustness consider using GNU awk for FIELDWIDTHS:
$ awk -v FIELDWIDTHS='25 8 999' -v OFS=' | ' '{print $1, $2, $3}' file
blue chile | green | morning dawn
red turkey | pink | morning dawn
white south africa | red | evening midst & more
$ awk -v FIELDWIDTHS='25 8 999' -v OFS=' | ' '{print $1, $2 $3}' file
blue chile | green morning dawn
red turkey | pink morning dawn
white south africa | red evening midst & more
$ awk -v FIELDWIDTHS='25 8 999' -v OFS=' | ' '{$0 = $1 OFS $2 $3; gsub(/ +/," ")} 1' file
blue chile | green morning dawn
red turkey | pink morning dawn
white south africa | red evening midst & more
Upvotes: 3