Why uniq -c output with space instead of \t?

2020-06-17 06:57发布

I use uniq -c some text file. Its output like this:

123(space)first word(tab)other things
  2(space)second word(tab)other things

....

So I need extract total number(like 123 and 2 above), but I can't figure out how to, because if I split this line by space, it will like this ['123', 'first', 'word(tab)other', 'things']. I want to know why doesn't it output with tab?

And how to extract total number in shell? ( I finally extract it with python, WTF)

Update: Sorry, I didn't describe my question correctly. I didn't want to sum the total number, I just want to replace (space) with (tab), but it doesn't effect the space in words, because I still need the data after. Just like this:

123(tab)first word(tab)other things
  2(tab)second word(tab)other things

标签: shell awk uniq
7条回答
冷血范
2楼-- · 2020-06-17 07:22

Try this:

uniq -c | sed -r 's/^( *[^ ]+) +/\1\t/'
查看更多
Fickle 薄情
3楼-- · 2020-06-17 07:31

One possible solution to getting tabs after counts is to write a uniq -c-like script that formats exactly how you want. Here's a quick attempt (that seems to pass my minute or so of testing):

awk '
(NR == 1) || ($0 != lastLine) {
    if (NR != 1) {
        printf("%d\t%s\n", count, lastLine);
    }
    lastLine = $0;
    count = 1;
    next;
}
{
    count++;
}
END {
    printf("%d\t%s\n", count, lastLine);
}
' yourFile.txt
查看更多
仙女界的扛把子
4楼-- · 2020-06-17 07:32

You can sum all the numbers using awk:

awk '{s+=$1}END{print s}'
查看更多
迷人小祖宗
5楼-- · 2020-06-17 07:32

Based on William Pursell answer , if you like Perl compatible regular expressions (PCRE) maybe a more elegant and modern way would be

perl -pe 's/ *(\d+) /$1\t/'

Options are to execute (-e) and print (-p).

查看更多
爷、活的狠高调
6楼-- · 2020-06-17 07:40

Another solution. This is equivalent to the earlier sed solution, but it does use awk as requested / tagged!

cat yourFile.txt \
    | uniq -c \
    | awk '{
        match($0, /^ *[^ ]* /);
        printf("%s\t%s\n", $1, substr($0, RLENGTH + 1));
      }'
查看更多
Juvenile、少年°
7楼-- · 2020-06-17 07:41
$ cat <file> | uniq -c | awk -F" " '{sum += $1} END {print sum}'
查看更多
登录 后发表回答