Contents and details extract problem asking...

Input:

length    align    type    5453    8666    +
length    align    portion    5453    8666    +
length    align    range  5453    5616    +
length    align    name    5453    6121    +
length    align    age     5617    6121    +
length    align    name    7214    7404    +
length    align    age     7214    7404    +
length    align    name    8378    8666    +
length    align    age     8378    8416    +
length    align    average         8417    8666    +

length    align    type    33357   37495   -
length    align    portion    33357   37495   -
length    align    range  37341   37495   -
length    align    name    37065   37495   -
length    align    age     37065   37340   -
.
.

Output:

length    5453    6121    
length    7214    7404    
length    8378    8666    

length    37495   37065
.
.

All the portion is separated by a new line or "type". Under the "type", I only need to focus on the data with "name" at the column3. For those that "name" at the column3 and column6 is marked as "+", the data of column4 and column5 can be maintained. While those that "name" at the column3 and column6 is marked as "-", the data of column4 and column5 should be reversed. It seems like awk able to archive these goal.
Thanks for any advice.

Try...

awk '$3=="name"{print $1, ($6=="+"?$4 OFS $5:$5 OFS $4)}' OFS=\\t file1

Hi Ygor,
Thanks a lot for your awk code suggestion.
It worked well.
but it seems like it can't separated all the portion by a new line based on "type" word.
my desired output:

length    5453    6121    
length    7214    7404    
length    8378    8666    

length    37495   37065
.
.

The output after I run your awk code:

length    5453    6121    
length    7214    7404    
length    8378    8666    
length    37495   37065
.
.

Both of the output are quite close to each other except lack of the new line based on the "type" word. Do you got any idea to archive my desired output result?
Thanks a lot and again for your help.

Try this:

awk '{ if(($3 == "name") && ($6 == "+")) {print $1,$4,$5 } else if(($3 == "name") && ($6 == "-"))  {print "\n" $1,$5,$4}}' file1

Hi Jairaj,
Your awk code seems like still got a bit different to generate my desired output result?
Do you got other suggestion or advice to improve it?
Thanks again first.
I'm trying to edit your awk code to get my desired output result as well...

With a little adaptation of the code of Ygor:

awk '$3=="name"{print $1, ($6=="+"?$4 OFS $5:$5 OFS $4)}!NF{print ""}' OFS=\\t file1

Thanks for your help again, Franklin52.
Your awk code work perfectly in my case.
Thanks again.

It's Ygor's code with a little tweak.:wink:

Regards

or more short code:-

nawk '/name/||!NF{$0=$1FS$4FS$5;print $0}' infile.txt

;);):wink:

Short, but not what the OP expects.

Compare your output with the desired one.

Regards

sorry guys misread the thread.
ok.using Yogor code with shorter modification than Franklin52's one.

awk '$3=="name"||!NF{print $1, ($6=="+"?$4 OFS $5:$5 OFS $4)}' OFS=\\t file 

;);):wink:

Hi all,

Thanks for all the suggestion and advice to solve my problem :slight_smile:
Thanks ya.