0

How can I loop through and achieve below with passing shell variable in awk using for/while loop?

I have a text file like below.

mark     10 20 30
lawrence 40 22 60
mark     11 12 13
mike     15 16 17
lawrence 21 22 23
mike     31 32 33
mike     41 42 47

I want the output to be like below (2nd column denotes the count of occurrence of each name)

I had one more requirement sorry asking again output would be like

if mark has value 20(in third column), its occurence should be printed in next colmn

if mike has value 32(in third column), its occurence should be printed in next column

if lawrence has value 22(in third column), its occurence should be printed in next column

mark 2 1

mike 3 1

lawrence 2 2

This is what it looks like. I want the text file to be passed as below. Can you please help?

Command i am using now

n=$(date +"%Y%m%d");

LogDataN=`tail -10 "$n".txt`   -- my text file which contains the above data

A=`echo "$LogDataN" | awk '{ c["$1"]++ } END { for (name in c) print name, c[name] }' `

echo "$A"
3
  • just sort | uniq -c really
    – n.caillou
    May 9, 2020 at 7:08
  • @n.caillou That would take the full line into account and would therefore not work.
    – Kusalananda
    May 9, 2020 at 7:34
  • You must be joking about adding six specific data values (3 names, 3 scores) to the original requirement at this stage. What do you do when sarah shows up? Put n/a in the extra column, maybe? May 9, 2020 at 10:14

1 Answer 1

1

I don't quite see why you would be wanting to pass any shell variables to awk here.

tail "$(date +'%Y%m%d.txt')" |
awk '{ c[$1]++ } END { for (name in c) print name, c[name] }'

tail extracts the last 10 lines of text from the given file by default, so -n 10 (or the deprecated -10) is not needed. The date command is used to create the filename to read from.

The awk code uses the first column as a key in the associative array c, which holds the number of times each name has been seen. The count for a name is incremented for each line read from the file. At the end, the names and the associated counts are outputted.

Note that there is no use for shell variables here.

9
  • I get below while running { c[$4]++ } END { for (name in c) print name, c[name] }: command not found. please help. Should I pass sumthning in "name" ? Sorry to bother , new in this area
    – user411502
    May 9, 2020 at 7:11
  • command i am using A=echo "$LogDataN" | $awk '{ c[$1]++ } END { for (name in c) print name, c[name] }' where "$LogDataN" contains the data from text file.
    – user411502
    May 9, 2020 at 7:15
  • @user411502 You say you have a text file. Run my command exactly as I've shown (awk, not $awk, and don't use echo) with the name of your text file as the file argument at the end. If you need to clarify your question, please edit it.
    – Kusalananda
    May 9, 2020 at 7:33
  • This is what it looks like. I want the text file to be passed as below. Can you please help? n=$(date +"%Y%m%d"); LogDataN=tail -10 "$n".txt -- my text file which contains the data A=echo "$LogDataN" | awk '{ c["$1"]++ } END { for (name in c) print "name", c[name] }' echo "$A"
    – user411502
    May 9, 2020 at 7:56
  • @user411502 If you want to clarify you question, please edit it. I can't give an answer to something that was not actually specified in the question, that would disconnect my answer from the question.
    – Kusalananda
    May 9, 2020 at 8:26

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.