Extract specific data content from a long list of data

My input:

Data name: ABC001
Data length: 1000
Detail info
Data Direction Start_time End_time Length
1 forward 10 100 90       
1 forward 15 200 185
2 reverse 50 500 450

Data name: XFG110
Data length: 100
Detail info
Data Direction Start_time End_time Length
1 forward 50 100 50       
2 reverse 50 90 40

Data name: ABC010
Data length: 200
Detail info
Data Direction Start_time End_time Length
1 forward 150 190 40       
.
.
.

My output:

ABC001 10 100
ABC001 15 200
ABC001 50 500

XFG110 50 100
XFG110 50 90

ABC010 150 190
.
.
.

Based on individual Data name, I would like extract its Start_time and End_time detail and put it at the column 2 and 3, respectively.
Thanks for any suggestion or hit to archive this goal.

If i'n reading your requiremenst right....one way:

#  awk '/Data name/{print"";t=$3}/forward/||/reverse/{print t,$3,$4}' infile

ABC001 10 100
ABC001 15 200
ABC001 50 500

XFG110 50 100
XFG110 50 90

ABC010 150 190

HTH

Try:

awk '{ if($0 ~ /^Data name:/) str=$3; if($0 ~ /^[0-9]/) print str" "$3" "$4; }' file
while read -r line
do
    case "$line" in
        "Data name"* )
            dname=${line#*: }
            ;;
        *forward*|*reverse* )
            set -- $line
            echo $dname $3 $4
    esac
done <"file"

Thanks a lot, Tytalus.
Your awk code worked perfectly and very fast for my case :slight_smile:
I just check it along all my long list of data.
I found out something like this happen if my input data is shown like this:
My input

Data name: LCM50
Data length: 100
Detail info
Data Direction Start_time End_time Length
1 reverse 10 20 10       
2 reverse 50 90 40

Data name: BCD011
Data length: 1000
Detail info
Data Direction Start_time End_time Length

Data name: FGG010
Data length: 500
Detail info
Data Direction Start_time End_time Length
1 reverse 10 190 180       

My desired output:

LCM50 10 20
LCM50 50 90

FGG010 10 190

Problem facing by the awk code given this output:

LCM50 10 20
LCM50 50 90


FGG010 10 190

Do you know how to archive the desired output by ignoring those data that got Data name, but don't have Start_time and End_time?
Thanks, Tytalus.

---------- Post updated at 05:18 AM ---------- Previous update was at 05:15 AM ----------

Hi, dennis.jacob.
I just try your code. It is worked nice as well.
Just it can't add a newline between two different Data name :frowning:
Thanks for your any advice and suggestion.

---------- Post updated at 05:22 AM ---------- Previous update was at 05:18 AM ----------

Thanks a lot too, ghostdog74.
Your code can work as well.
Just it also unable to add a new line between two different Data name detail :frowning:
From your opinion, is it ok to archive my target by running your code first , then take the output file and let it add new lane in between two different data name after then?
Thanks for advice.

if you want to add a newline, just do printf with \n. i will leave it to you to do that.

Thanks a lot, ghostdog74.
I get it already :slight_smile:

$ awk 'BEGIN{RS="";FS="\n"}
/Data name/ { split($1,a," ")
     for (i=2;i<=NF;i++) 
         {if (($i~/forward/)||($i~/reverse/)) 
              { split($i,b," "); print a[3],b[3],b[4]}}
     {printf "\n"}}'  urfile

ABC001 10 100
ABC001 15 200
ABC001 50 500

XFG110 50 100
XFG110 50 90

ABC010 150 190

Hi rdcwayx, your code is worked fine too. Just it lack of put a ";" before the for.
I already edited it for you :slight_smile:
Thanks a lot.

$ awk 'BEGIN{RS="";FS="\n"}
/Data name/ { split($1,a," ") ;
     for (i=2;i<=NF;i++) 
         {if (($i~/forward/)||($i~/reverse/)) 
              { split($i,b," "); print a[3],b[3],b[4]}}
     {printf "\n"}}'  urfile

ABC001 10 100
ABC001 15 200
ABC001 50 500

XFG110 50 100
XFG110 50 90

ABC010 150 190

Interesting, I needn't the ; in my system.

Anyway, that's good to hear the problem is fixed.

my $flag=0;
my $name;
while(<DATA>){
	if(/^Data name:\s*(.*)/){
		$name = $1;
	}
	elsif(/Start_time End_time/){
		$flag=1;
	}
	elsif(/^\s*$/){
		print "\n";
		$flag=0;		
	}
	else{
		if($flag eq 1){
			my @tmp = split;
			print $name," ",$tmp[2]," ",$tmp[3],"\n";
		}
	}
}
__DATA__
Data name: ABC001
Data length: 1000
Detail info
Data Direction Start_time End_time Length
1 forward 10 100 90       
1 forward 15 200 185
2 reverse 50 500 450

Data name: XFG110
Data length: 100
Detail info
Data Direction Start_time End_time Length
1 forward 50 100 50       
2 reverse 50 90 40

Data name: ABC010
Data length: 200
Detail info
Data Direction Start_time End_time Length
1 forward 150 190 40     

thanks summer, I trying your suggestion now :slight_smile:
Thanks first ^^