1

Original file FinalResults.txt contains the following:

loginName:name1
memoryInfo:jsHeapSizeLimit:2181038082
session:cabSessionID:
sessionStartTime:

loginName:name1
memoryInfo:jsHeapSizeLimit:2181038080
session:cabSessionID:
sessionStartTime:

loginName:name2
memoryInfo:jsHeapSizeLimit:2181038080
session:cabSessionID:
sessionStartTime:

loginName:name3
memoryInfo:jsHeapSizeLimit:2181038084
session:cabSessionID:
sessionStartTime:

loginName:name4
memoryInfo:jsHeapSizeLimit:2181038080
session:cabSessionID:
sessionStartTime:

memoryInfo:jsHeapSizeLimit:2181038080
session:cabSessionID:
sessionStartTime:

loginName:name5
memoryInfo:jsHeapSizeLimit:2181038080
session:cabSessionID:
sessionStartTime:

loginName:name1
memoryInfo:jsHeapSizeLimit:2181038082
session:cabSessionID:
sessionStartTime:

loginName:name6
memoryInfo:jsHeapSizeLimit:2181038083
session:cabSessionID:
sessionStartTime:

This is repeated multiple times throughout the original output. I would like to search through this file and create another output text file that should have 1 line per user and look like the following:

loginName:  memoryInfo:jsHeapSizeLimit:

The loginName and memoryInfo should be separated by a Tab space.

I want to exclude some names from this list.

This is what I have so far:

$ grep -e "^loginName\|^memoryInfo" FinalResults.txt | egrep -v 'name1|name2' | awk '$1!=p; {p=$1}' | paste -d"\t" - - > Test.txt

After removing the names, I am left with memoryInfo followed by memoryInfo.

How can I modify my script to have the output from this:

loginName:A memoryInfo:jsHeapSizeLimit: 1Gb
loginName:B memoryInfo:jsHeapSizeLimit: 2Gb
memoryInfo:jsHeapSizeLimit: 3Gb loginName:C
memoryInfo:jsHeapSizeLimit: 4Gb

enter image description here To this:

loginName:A memoryInfo:jsHeapSizeLimit: 1Gb
loginName:B memoryInfo:jsHeapSizeLimit: 2Gb
loginName:C memoryInfo:jsHeapSizeLimit: 4Gb

Basically, it should be Name, memoryInfo in that pattern. If it is a memoryInfo followed by memoryInfo, I want the second one to be removed.

  • How to you get your A, B, C and 1Gb, 2Gb, 3Gb from your input file? – roaima Feb 21 at 16:10
  • That is already pre-generated on the FinalResults.txt – Ibrahim A Feb 21 at 16:15
  • Please show a snippet of a real input file with a few loginName: and memoryInfo: lines that produces the wrong output with your script. – Bodo Feb 21 at 16:15
  • If I take your sample code and your sample input file I get loginName: memoryInfo:jsHeapSizeLimit. This doesn't match what you say it produces. – roaima Feb 21 at 16:17
  • I have added the picture. The main file FinalResults.txt has some entries without the loginName field which throws everything off. I posted a picture of the output on my original post so you see what I am talking about. – Ibrahim A Feb 21 at 16:24
1

You can use AWK for this task.

First solution using a similar egrep command to exclude users:

 egrep -v 'loginName:(name1|name2)' FinalResults.txt | awk '/^loginName:/ { login=$0; } # save line
     /^memoryInfo:jsHeapSizeLimit:/ { 
         if(login!="") { # only if we have a saved loginName line
             printf "%s\t%s\n", login, $0;
             login=""; # clear to avoid printing twice
         }
     }'

With the input from the question, the output is

loginName:name3 memoryInfo:jsHeapSizeLimit:2181038084
loginName:name4 memoryInfo:jsHeapSizeLimit:2181038080
loginName:name5 memoryInfo:jsHeapSizeLimit:2181038080
loginName:name6 memoryInfo:jsHeapSizeLimit:2181038083

Second solution with an extended AWK script and an exclude list in a separate file

Assuming you create a file exclude with all users to exclude, one per line

name1
name2

you can use an extended AWK script and supply the exclude file as the first file before the input data file(s)

awk 'NR==FNR {# condition is valid for first file only
        exclude[$0]=1; # add name to exclude map
        next; # stop processing, do not check other rules
     }
     /^loginName:/ { 
         name=substr($0,11); # extract name
         if (!( name in exclude )) login=$0; } # save line if not in exclude list
     /^memoryInfo:jsHeapSizeLimit:/ {
         if(login!="") { # only if we have a saved loginName line
             printf "%s\t%s\n", login, $0;
             login=""; # clear to avoid printing twice
         }
     }' exclude FinalResults.txt

This produces the same result as the first AWK script combined with egrep.

  • I added the FinalResults.txt information on my original post so you can copy and paste it now. You can run my code to see exactly the issue I run into. – Ibrahim A Feb 21 at 16:38
  • This script does clean up to a certain extent, however any lines with name1 and name2 should be excluded along with the associated memoryInfo. – Ibrahim A Feb 21 at 16:43
  • @IbrahimA Combined first AWK script with egrep command and added alternative solution – Bodo Feb 21 at 17:47
  • Much appreciated! Works like a charm. Wish I had your skills! – Ibrahim A Feb 21 at 18:27
  • 1
    @IbrahimA Working on UNIX or Linux and continuing to learn will surely improve your skills. I guess I started 20 years earlier than you. – Bodo Feb 21 at 18:34
1

i Have done by below method

awk '/^loginName:/{x=NR+1}(NR<=x){print}' filename| sed "N;s/\n/ /g"| awk '$0 !~ /name[12]/{print $0}'

output

loginName:name3 memoryInfo:jsHeapSizeLimit:2181038084
loginName:name4 memoryInfo:jsHeapSizeLimit:2181038080
loginName:name5 memoryInfo:jsHeapSizeLimit:2181038080
loginName:name6 memoryInfo:jsHeapSizeLimit:2181038083
  • This works as well, now if I wanted to remove names like Elmo and Mickey, how would I go about modifying your script to reflect that? – Ibrahim A Feb 21 at 18:37
  • I got it! Just replace /name[12]/ with /Elmo|Mickey/ – Ibrahim A Feb 21 at 18:41
  • This is actually missing the tab in between loginName and memoryInfo – Ibrahim A Feb 21 at 20:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.