I would like to extract the file paths from different directories listed inside a file after “FILE_PATHS”,then extract a specific part of these file names based on a condition. For example:
$ grep ^FILE_PATHS file.txt
FILE_PATHS /james/families/MOTHER/analyses/trait
FILE_PATHS /james/families/SIB/analyses/BROTHER/trait
FILE_PATHS /james/families/REL/analyses/AUNT/trait
FILE_PATHS /james/families/FATHER/analyses/trait
From this list of file paths, I would like to extract a specific part of the file name “MOTHER”, “SIB”, “REL”, “FATHER”, and then if this part is equal to “SIB” print “BROTHER”, if it is equal to “REL” print “AUNT”, otherwise (for “MOTHER” and “FATHER”) print “MOTHER” and “FATHER”.
The only files that have a path different from the rest are the ones equal to “SIB” and “REL” in the 4th field, however the complication is that there are many options for what the value on the 6th field could be, so I am looking for a solution where I don’t need to specify the values of “BROTHER” and “AUNT” in the 6th field, but that would just print my 6th field.
So it would be something like this:
cat file.txt | while read line; do
if [ `echo "$line" | grep ^FILE_PATHS file.txt | cut -d' ' -f 2 | cut -d '/' -f4 -eq "BROTHER" | "REL" ` ]
then
grep ^FILE_PATHS file.txt | cut -d' ' -f 2 | cut -d '/' -f5
else
grep ^FILE_PATHS file.txt | cut -d' ' -f 2 | cut -d '/' -f4
fi; done
This is full of errors and incorrect, but maybe I have the wrong approach all together, and I am sure there is a smart way of doing this but I am totally new to BASH, is there a better approach I am not seeing?
this should work for you
actually you could combine your grep into awk like:
output from your example:
show how it work:
EDIT Again
I would say it should be 4th field is SIB/REL, then print 6th. since the first field is FILE_PATHS.
now this line works:
test!!: