You canIf I understood what you want then you should use grep, xargs and sedawk commands:
grepxargs -En3 "(Name|Admin|Oper)"< your_file.txt | xargs -n3 | sedawk -Ee 's'{gsub(/(Name|Admin|Oper)>//g',""); -e 's/\s+/print $1",/g'"$2","$3}' | awk -F',' --file script.awk
Where script.awk contains this:
#! /usr/bin/awk
{
if($1 ~ ".*>.*") print ","$2","$3
else if($2 ~ ".*>.*") print $1",,"$3
else if($3 ~ ".*>.*") print $1","$2","
else print
}
Name>Ami
Admin>2
Oper>1
Total>10
Name>Sum
Admin>3
Quant>23
Oper>2Total>2
Name>Tar
Admin>1
Oper>2
With grep -E "(Name|Admin|Oper)" you will select only those lines which match with any of the three specified strings: Name,Admin or Oper
With xargs -n3 you will get the output of the file every 3 lines (as one line):
Name>Ami Admin>2 Oper>1
Name>Sum Admin>3 Oper>2Total>2
Name>Tar Admin>1 Oper>2
And withWith sedawk '{gsub(/(Name|Admin|Oper)>/,""); print $1","$2","$3}' it will replace the unused textvalues like Name> Admin> or Oper> will be replaced by an empty string and addwith print $1","$2","$3 the commasvalues (after >) will be printed with a comma.
If you use:
sedxargs -Een3 's< data3 | awk '{gsub(/(Name|Admin|Oper)>//g',""); -e 's/\s+/print $1",/g'"$2","$3}
you will get:
Ami,2,1
Sum,3,Total>2
Tar,1,2
Now, it will be removed unnecessary strings like Total>2. With the script.awk you can remove them but before we have to define the separator, in this case the comma (,).
The awk code like $1 ~ ".*>.*" will verify if the current string ($1, $2 or $3) does match with the pattern .*>.* if it does, then, the current string will not be printed.
Important: The script.awkworks by group. So, if there is a not valid column, this one must be placed instead of Name or Admin or Oper. For example if your_file.txt has:
Name>Ami
Total>2
Oper>1
Name>Sum
Admin>3
Total>2
Total>Tar
Admin>1
Oper>2
The output of the script will be:
Ami,,1
Sum,3,
,1,2
But if you have inside the your_file.txt:
Total>Ami
Total>2
Oper>1
Name>Sum
Admin>3
Total>2
Total>Tar
Admin>1
Total>2
the command will not work as you expect.
Note: If you want that your_file.txt be edited too, you will have to use tee your_file.txt at the end:
grepxargs -En3 "(Name|Admin|Oper)"< your_file.txt | xargs -n3 | sedawk -Ee 's'{gsub(/(Name|Admin|Oper)>//g',""); -e 's/\s+/print $1",/g'"$2","$3}' | awk -F',' --file script.awk | tee your_file.txt