I have a file that looks like:
>ref_frame=1
TPGIRYQYNVLPQGWKGSPAIFQSSMTKILEPFRKQNPDIVIYQYMDDLYVGSD
>ref_frame=2
HQGLDISTMCFHRDGKDHQQYSKVA*QKS*SLLENKIQT*LSINTWMICM*DLT
>ref_frame=3
TRD*ISVQCASTGMERITSNIPK*HDKNLRAF*KTKSRHSYLSIHG*FVCRI*
>test_3_2960_3_frame=1
TPGIRYQYNVLPQGWKGSPAIFQSSMTKILEPSRKQNPDIVIYQYMDDLYVGSD
I want to assign a bash variable so that echo $variable
gives test_3_2960
The line/row that I want to assign the variable to will always be line 7. How can I accomplish this using bash?
so far I have:
variable=`cat file.txt | awk 'NR==7'`
echo $variable
= >test_3_2960_3_frame=1
CodePudding user response:
Using sed
$ variable=$(sed -En '7s/>(([^_]*_){2}[0-9] ).*/\1/p' input_file)
$ echo "$variable"
test_3_2960
CodePudding user response:
If you wish to continue with awk
$ variable=$(awk 'NR==7' file.txt | awk -F "[>_]" '{print $2"_"$3"_"$4}')
$ echo $variable
test_3_2960
CodePudding user response:
You can also use awk to split the line based on the delimiter '_':
$ variable=`cat file.txt | awk -F_ 'NR==7 {print $1"_"$2"_"$3}'`
$ echo $variable
test_3_2960