How to write multiple files with sed from a matched pattern
I have a file like this
Orange 23 34 56
Apple 23 44 56
Pear 23 44 56
I want to use sed so that it moves numbers after fruits to filenames Orange.txt
and Apple.txt
across different files
something like
s -re 's/(^\w+).*//' > \1.txt
I know I can do in awk, but I want the sed solution not to execute another unix command
+3
source to share
2 answers
This is only possible sed
if you are willing to jot down a giant list of every possible fruit in advance. Then it looks like this:
#! /usr/bin/sed -nf
/^Apple /w Apple.txt
/^Durian /w Durian.txt
/^Kiwi /w Kiwi.txt
/^Orange /w Orange.txt
/^Pear /w Pear.txt
/^Pineapple /w Pineapple.txt
/^Tomato /w Tomato.txt
# ... perhaps dozens more lines ...
If you cannot list every possible fruit in advance, this is simply not possible. Even if you can, however, do you really want to set yourself up for the daily WTF?
awk '{ print > $1 ".txt" }'
0
source to share