How to get the second column from command output?

后端 未结 8 1169
有刺的猬
有刺的猬 2020-11-29 18:08

My command\'s output is something like:

1540 \"A B\"
   6 \"C\"
 119 \"D\"

The first column is always a number, followed by a space, then a

8条回答
  •  死守一世寂寞
    2020-11-29 18:19

    #!/usr/bin/python
    import sys 
    
    col = int(sys.argv[1]) - 1
    
    for line in sys.stdin:
        columns = line.split()
    
        try:
            print(columns[col])
        except IndexError:
            # ignore
            pass
    

    Then, supposing you name the script as co, say, do something like this to get the sizes of files (the example assumes you're using Linux, but the script itself is OS-independent) :-

    ls -lh | co 5

提交回复
热议问题