I have a delimited file with the following format that I have assigned to a bash variable "foo":
echo $foo A 1 hello B 2 hello C 3 hello D 4 world E 5 world F 6 world
I am trying to find a preferably awk-based solution that can split this variable into new text files such that all of the rows with identical values in column #3 are in one file. In a perfect world, the files would be named after the string in column #3 In this case, the files would be "hello.txt and "world.txt" with the following contents:
cat hello.txt A 1 hello B 2 hello C 3 hello cat world.txt D 4 world E 5 world F 6 world
EDIT: I should have included some previous attempts for this as mentioned below. I know how one could do this if the patterns were known ahead of time using something like grep, but am not sure how to allow it to accept wildcards. This is what I normally do if I know I will only need to extract by a very specific pattern.
echo "$foo" | grep -w "hello" > hello.txt echo "$foo" | grep -w "world" > world.txt