2

I have two text files with space-delimited fields (03 fields) in Linux environment:

File1 : ram-service1.txt

RAM used    Program

23500   kb  firewalld
14780   kb  tuned
10140   kb  polkitd
5370    kb  NetworkManager
4470    kb  rsyslogd
4270    kb  lvmetad
4240    kb  systemd
4010    kb  sshd(2)
3830    kb  systemd-journal
3720    kb  systemd-udevd
3120    kb  dhclient
1630    kb  qmgr
1590    kb  pickup
1180    kb  master
1070    kb  dbus-daemon
1000    kb  chronyd
1021.00 kb  sftp-server
1004.00 kb  login
967.00  kb  bash
867.00  kb  systemd-logind
741.00  kb  crond
734.00  kb  auditd
450.00  kb  irqbalance
397.00  kb  ramusage.sh
186.00  kb  pv

File2 : ram-service2.txt

RAM used    Program

23540   kb  firewalld
14830   kb  tuned
10140   kb  polkitd
5450    kb  NetworkManager
4370    kb  rsyslogd
4270    kb  lvmetad
4250    kb  systemd
3720    kb  systemd-udevd
3720    kb  systemd-journal
3180    kb  dhclient
1870    kb  sshd
1680    kb  qmgr
1640    kb  pickup
1200    kb  login
1190    kb  master
1070    kb  dbus-daemon
1010    kb  chronyd
941.00  kb  bash
868.00  kb  systemd-logind
747.00  kb  crond
736.00  kb  auditd
450.00  kb  irqbalance
395.00  kb  ramusage.sh
183.00  kb  pv

I just need to compare ram-service1.txt file's RAM Used column values with ram-service2.txt file's RAM Used column respective to their programs. As a example if the service "firewalld's" RAM Used value in ramservice1.txt file is greater than or less than the service "firewalld's" RAM Used value in ramservice2.txt file, I need to redirect it (difference + or -) into another file with the program name.

If anyone can solve this I would really thankful and its a great help for me. .

5
  • Have a look at <stackoverflow.com/help/minimal-reproducible-example> (specially the minimal point). It's also nice to provide the expected output, because descriptions can be tiresome to read and may lead to varying interpretations (for example, what is difference + or -? I have a guess but maybe I'm wrong.
    – Quasímodo
    Apr 15, 2020 at 18:50
  • Yes I can simplify the units. Do I need to repost with the changes? Apr 15, 2020 at 18:59
  • so basically - means 120 mb - 123 mb = - 3 mb. same as + means 123 mb - 120 mb = + 3 mb. Apr 15, 2020 at 19:05
  • Done. Is it OK or do I need to change more? Apr 15, 2020 at 19:19
  • It's fine now, thanks for the modifications.
    – Quasímodo
    Apr 15, 2020 at 19:22

2 Answers 2

1

Try this awk:

awk '
    NR==FNR{s1[$3]=$1;next}
    {s2[$3]=$1}
    END{
        for (value in s1){
            if (s1[value]!=s2[value]) print s1[value]-s2[value],$2,value
        }
    }
' ram-service1.txt ram-service2.txt | column -t > outfile

The column just puts the output in a nice "chart" format and saves it to outfile.

6
  • Yeah its working. Thank you for the quick response and its a big help for me. Thank you again. Apr 15, 2020 at 19:28
  • it seems like if condition doesn't work properly. I just change the if condition and add, if (s1[value]!=s2[value] && s1[value]-s2[value] -gt s1[value]*1.5). But it returns same output. according to my changes on input file (ram-service1.txt) output should limited to one row. care to comment? Apr 15, 2020 at 20:14
  • @LasithAnjulaGalanga It works properly. You wrote the extension incorrectly, it should not be -gt but >. And notice that the extra condition you imposed is never true.
    – Quasímodo
    Apr 15, 2020 at 21:07
  • The condition that I have imposed is set to be true by myself in ram-service1.txt document. That means I have multiplied specific one value into 1.5, then above mentioned condition will automatically produce the true output. Apr 16, 2020 at 3:22
  • 1
    Ya got it. BTW thank you again for your contribution. I really appreciate it!!!!!!!!!!!!!"""""""" Apr 16, 2020 at 11:47
1
join -1 3 -2 3 <(sort -k3 file1) <(sort -k3 file2) |
awk '$2 != $4 { printf("%20s:\t%+d\n", $1, $4 - $2) }'

This would join the files based on the program name in the third column. Each file is sorted on this field and then join reads that data and outputs the lines for which the program name matches in both files. The output of that step would look like

Program RAM used RAM used
NetworkManager 5370 kb 5450 kb
auditd 734.00 kb 736.00 kb
bash 967.00 kb 941.00 kb
chronyd 1000 kb 1010 kb
crond 741.00 kb 747.00 kb

(etc.)

... where the first column now is the program name, the second is the number from the first file and the fourth column is the number from the second file.

The awk program that reads this tests the values in column 2 and 4, and if they are different, it writes a line with the difference.

The result, given the data in the question, will be

      NetworkManager:   +80
              auditd:   +2
                bash:   -26
             chronyd:   +10
               crond:   +6
            dhclient:   +60
           firewalld:   +40
               login:   +196
              master:   +10
              pickup:   +50
                  pv:   -3
                qmgr:   +50
         ramusage.sh:   -2
            rsyslogd:   -100
             systemd:   +10
     systemd-journal:   -110
      systemd-logind:   +1
               tuned:   +50

A negative value means it's smaller in the second file.

3

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.