1

I have a file with strings with pre-defined format:

src/history_of_vim.txt  -      1803  -  Wed Jul 14 11:59:15 +07 2021  -  ef669ffc3a7fc1c502c7be34d478757c1efe1aa6fd5768174e33542cb26c62bd  -  sha256

src/history_of_vim.txt  -      1853  -  Wed Jul 14 12:03:51 +07 2021  -  ef669ffc3a7fc1c502c7be34d478757c1efe1aa6fd5768174e33542cb26c62bd  -  sha256 

I need to write a bash script that counts number of lines, number of unique files (e.g. I have only 1 unique - history_of_vim.txt) and number of unique checksums.

I've done the following code (input path is only an example):

count_lines=0
while IFS= read -r line
do
  count_lines=${countlines+1}
done < "$input" 
echo $count_lines

But it only helps with the first task. I was trying to use awk with delimiter '-' and then print the exact positions that I need and take only unique with sort -u. However, I can't bring it out together to make it work.

Toto
  • 86,179
  • 61
  • 85
  • 118
Arzental
  • 83
  • 6
  • 1
    What is your expected output – anubhava Jul 14 '21 at 09:51
  • In this case it has to be 2 1 1 (number of lines, number of unique files, number of unique checksums) – Arzental Jul 14 '21 at 09:55
  • something like this maybe? https://stackoverflow.com/questions/68333498/shell-script-to-get-hosts-and-the-total-number-of-requests-per-host-from-log-fil/ It sounds like you took the `hacker rank` test as well, did you not see the OP in that post? :-) – Jetchisel Jul 14 '21 at 10:00
  • 2
    Looking at [your question history](https://stackoverflow.com/users/15120282/arzental?tab=questions) I think you should read this: [What does it mean to accept an answer?](https://meta.stackexchange.com/a/5235/346807) – James Brown Jul 14 '21 at 10:05
  • @Jetchisel Actually, I hear about this site for the first time. Not sure if it is a solution for me, but I can look at some helpful constructions. – Arzental Jul 14 '21 at 10:26

1 Answers1

2

You may use this awk:

awk -F '[[:blank:]]*-[[:blank:]]*' 'NF {++nrec; files[$1]; chksum[$(NF-1)]} 
END {print nrec, length(files), length(chksum)}' file

2 1 1
anubhava
  • 713,503
  • 59
  • 514
  • 593
  • Yes, it gives the result I needed. Tho, I don't understand almost all syntax and sort it out for myself (I am very new to bash). Thanks. – Arzental Jul 14 '21 at 16:00
  • Please go through a [basic awk tutorial](http://www.grymoire.com/Unix/Awk.html). awk is different from bash. – anubhava Jul 14 '21 at 16:43