Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9244791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:07:12+00:00 2026-06-18T09:07:12+00:00

I would like to extract the file paths from different directories listed inside a

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T09:07:13+00:00Added an answer on June 18, 2026 at 9:07 am

    this should work for you

    your grep....|awk -F/ '{$4=$4=="REL"?"AUNT":$4;$4=$4=="SIB"?"BROTHER":$4;print $4}'
    

    actually you could combine your grep into awk like:

    awk -F/ '/^FILE_PATHS/{$4=$4=="REL"?"AUNT":$4;$4=$4=="SIB"?"BROTHER":$4;print $4}' file.txt
    

    output from your example:

    MOTHER
    BROTHER
    AUNT
    FATHER
    

    show how it work:

    kent$  cat o
    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
    
    kent$  awk -F/ '/^FILE_PATHS/{$4=$4=="REL"?"AUNT":$4;$4=$4=="SIB"?"BROTHER":$4;print $4}'  o
    MOTHER
    BROTHER
    AUNT
    FATHER
    

    EDIT Again

    If the value on the 3rd field is SIB or REL I would like to print
    whatever is in the fifth field

    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:

      awk -F/ '/^FILE_PATHS/{$4=$4~"^(REL|SIB)$"?$6:$4;print $4}' file.txt
    

    test!!:

    kent$  cat o
    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
    
    kent$  awk -F/ '/^FILE_PATHS/{$4=$4~"^(REL|SIB)$"?$6:$4;print $4}' o
    MOTHER
    BROTHER
    AUNT
    FATHER
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to extract a channel audio from the an LPCM raw file
I would like to extract UID from /etc/passwd file which looks like this- www-data:x:33:33:www-data:/var/www:/bin/sh
I would like to extract lines of a file where specific columns have a
I would like to extract only tar.gz from the following strings. /root/abc/xzy/file_tar_src-5.2.8.23.tar.gz or /root/abc/xyz/file_tar_src-5.2.tar.gz
I would like to extract and construct some strings after identifying a substring that
I would like to extract items from this sample html, more specificly, i would
I would like to extract the GPS EXIF tag from pictures using php. I'm
I would like to extract a value or its inverse from database. I would
Hello! I would like to extract all citations from a text. Additionally, the name
I have an HTML file and would like to extract the text between <li>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.