Skip to main content
added 1362 characters in body
Source Link
Edgar Magallon
  • 5.2k
  • 3
  • 15
  • 29

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

You can use grep, xargs and sed commands:

grep -E "(Name|Admin|Oper)" your_file.txt | xargs -n3 | sed -Ee 's/(Name|Admin|Oper)>//g' -e 's/\s+/,/g'
Name>Ami  
Admin>2  
Oper>1
Total>10
Name>Sum  
Admin>3 
Quant>23
Oper>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>2
Name>Tar Admin>1 Oper>2

And with sed it will replace the unused text and add the commas:

sed -Ee 's/(Name|Admin|Oper)>//g' -e 's/\s+/,/g'

Note: If you want that your_file.txt be edited too, you will have to use tee your_file.txt at the end:

grep -E "(Name|Admin|Oper)" your_file.txt | xargs -n3 | sed -Ee 's/(Name|Admin|Oper)>//g' -e 's/\s+/,/g' | tee your_file.txt

If I understood what you want then you should use xargs and awk commands:

xargs -n3 < your_file.txt  | awk  '{gsub(/(Name|Admin|Oper)>/,"");  print $1","$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  
Name>Sum  
Admin>3  
Total>2  
Name>Tar  
Admin>1  
Oper>2

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 Total>2
Name>Tar Admin>1 Oper>2

With awk '{gsub(/(Name|Admin|Oper)>/,""); print $1","$2","$3}' the values like Name> Admin> or Oper> will be replaced by an empty string and with print $1","$2","$3 the values (after >) will be printed with a comma.

If you use:

xargs -n3 < data3  | awk  '{gsub(/(Name|Admin|Oper)>/,"");  print $1","$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:

xargs -n3 < your_file.txt  | awk  '{gsub(/(Name|Admin|Oper)>/,"");  print $1","$2","$3}' | awk -F',' --file script.awk | tee your_file.txt
added 432 characters in body
Source Link
Edgar Magallon
  • 5.2k
  • 3
  • 15
  • 29

You can use grep, xargs and sed commands:

 xargsgrep -n3E <"(Name|Admin|Oper)" your_file.txt | xargs -n3 | sed -Ee 's/(Name|Admin|Oper)>//g' -e 's/\s+/,/g'

If in the your_file.txt you have:

Name>Ami  
Admin>2  
Oper>1  
Total>10
Name>Sum  
Admin>3  
Quant>23
Oper>2  
Name>Tar  
Admin>1  
Oper>2

ThenWith 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 < your_file.txt 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>2
Name>Tar Admin>1 Oper>2

And with sed it will replace the unused text and add the commas:

sed -Ee 's/(Name|Admin|Oper)>//g' -e 's/\s+/,/g'

Note: If you want that your_file.txt be edited too, you will have to use tee your_file.txt at the end:

grep -E "(Name|Admin|Oper)" your_file.txt | xargs -n3 | sed -Ee 's/(Name|Admin|Oper)>//g' -e 's/\s+/,/g' | tee your_file.txt

You can use xargs and sed commands:

 xargs -n3 < your_file.txt | sed -Ee 's/(Name|Admin|Oper)>//g' -e 's/\s+/,/g'

If in the your_file.txt you have:

Name>Ami  
Admin>2  
Oper>1  
Name>Sum  
Admin>3  
Oper>2  
Name>Tar  
Admin>1  
Oper>2

Then with xargs -n3 < your_file.txt 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>2
Name>Tar Admin>1 Oper>2

And with sed it will replace the unused text and add the commas:

sed -Ee 's/(Name|Admin|Oper)>//g' -e 's/\s+/,/g'

You can use grep, xargs and sed commands:

grep -E "(Name|Admin|Oper)" your_file.txt | xargs -n3 | sed -Ee 's/(Name|Admin|Oper)>//g' -e 's/\s+/,/g'

If in the your_file.txt you have:

Name>Ami  
Admin>2  
Oper>1
Total>10
Name>Sum  
Admin>3 
Quant>23
Oper>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>2
Name>Tar Admin>1 Oper>2

And with sed it will replace the unused text and add the commas:

sed -Ee 's/(Name|Admin|Oper)>//g' -e 's/\s+/,/g'

Note: If you want that your_file.txt be edited too, you will have to use tee your_file.txt at the end:

grep -E "(Name|Admin|Oper)" your_file.txt | xargs -n3 | sed -Ee 's/(Name|Admin|Oper)>//g' -e 's/\s+/,/g' | tee your_file.txt
Source Link
Edgar Magallon
  • 5.2k
  • 3
  • 15
  • 29

You can use xargs and sed commands:

 xargs -n3 < your_file.txt | sed -Ee 's/(Name|Admin|Oper)>//g' -e 's/\s+/,/g'

If in the your_file.txt you have:

Name>Ami  
Admin>2  
Oper>1  
Name>Sum  
Admin>3  
Oper>2  
Name>Tar  
Admin>1  
Oper>2

Then with xargs -n3 < your_file.txt 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>2
Name>Tar Admin>1 Oper>2

And with sed it will replace the unused text and add the commas:

sed -Ee 's/(Name|Admin|Oper)>//g' -e 's/\s+/,/g'