how to extract a certain part of a line

Hi friends,

I want to select and use the certain part of a line. For example I have the following line

home/1245/hgdf/acsdf/myhome/afolder/H2O/endfile

how can I extract the part " /myhome/afolder/H2O/endfile "

thanks

echo "home/1245/hgdf/acsdf/myhome/afolder/H2O/endfile" | sed 's/^.\+\(\/myhome\)/\1/i'
1 Like

it works well. thank you

$ echo "home/1245/hgdf/acsdf/myhome/afolder/H2O/endfile" | awk -F"/" '{for(i=5;i<=NF;i++) {printf("/%s",$i); if(i==NF)print "\n"}}'
/myhome/afolder/H2O/endfile
1 Like

Is it possible to define a function that extracts the certain part of subsequent line when I copy the whole line after it. For example, when I type

rpf home/1245/hgdf/acsdf/myhome/afolder/H2O/endfile

output will be /myhome/afolder/H2O/endfile

can I define a function rpf like this ?

Type in your .bashrc -or other, depending on your shell-

rpf() {
   #Get one of the two suggestions from above, replacing the string by "$1" (with double quotes)
}

and you're done :slight_smile:

1 Like

thank you... thumbs up :slight_smile: