如何摆脱STDOUT开头的领先空间?

时间:2017-03-23 00:58:50

标签: bash shell

代码:

find . -type f -exec file -b -- {} \; | sort | uniq -c | \
  sort -r -n | awk '{$1=""; print $0;}'

输出:

 GIF image data, version 89a, 57 x 68
 GIF image data, version 89a, 8 x 8
 GIF image data, version 89a, 17 x 11
 PNG image data, 128 x 128, 8-bit/color RGBA, non-interlaced
 JPEG image data, JFIF standard 1.02, aspect ratio, density 100x100, segment length 16, baseline, precision 8, 100x457, frames 3
 JPEG image data, Exif standard: [TIFF image data, little-endian, direntries=0], baseline, precision 8, 510x300, frames 3
 HTML document, UTF-8 Unicode text, with CRLF line terminators
 GIF image data, version 89a, 960 x 4
 GIF image data, version 89a, 46 x 42
 GIF image data, version 89a, 100 x 100
 Composite Document File V2 Document, Cannot read section info
 ASCII text, with CRLF line terminators

期望的输出:

GIF image data, version 89a, 57 x 68
GIF image data, version 89a, 8 x 8
GIF image data, version 89a, 17 x 11
PNG image data, 128 x 128, 8-bit/color RGBA, non-interlaced
JPEG image data, JFIF standard 1.02, aspect ratio, density 100x100, segment length 16, baseline, precision 8, 100x457, frames 3
JPEG image data, Exif standard: [TIFF image data, little-endian, direntries=0], baseline, precision 8, 510x300, frames 3
HTML document, UTF-8 Unicode text, with CRLF line terminators
GIF image data, version 89a, 960 x 4
GIF image data, version 89a, 46 x 42
GIF image data, version 89a, 100 x 100
Composite Document File V2 Document, Cannot read section info
ASCII text, with CRLF line terminators

可能相当容易,但我无法绕过它 - 如何移除第一个领先空间。

4 个答案:

答案 0 :(得分:0)

使用sub()删除初始空格。

find . -type f -exec file -b -- {} \; | sort | uniq -c | sort -r -n | awk '{$1=""; sub("^ ", ""); print $0;}'

答案 1 :(得分:0)

这就是你要找的东西:

find . -type f -exec file -b -- {} \; | sort | uniq -c | sort -r -n | awk '{$1=""; print $0;}' | sed 's/ //'

答案 2 :(得分:0)

您可以在此上下文中将#include "PPM.h" #include <fstream> // WRITES THE DATA TO THE FILE void saveImage(char *filename, int width, int height, int **data) { std::ofstream file; file.open(filename); file << "P3" << "\n" << width << " " << height << "\n255\n"; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { file << data[y*width + x][0] << " " << data[y*width + x][1] << " " << data[y*width + x][2] << "\n"; } } file.close(); } // CREATES A BUFFER TO HOLD THE DATA int** allocateBuffer(int width, int height, int rgbAmnt) { int size = width * height; int **data = new int*[size]; for (int i = 0; i < size; i++) { data[i] = new int[rgbAmnt]; } return data; } // RELEASE THE MEMORY void deleteBuffer(int **buffer, int size) { for (int i = 0; i < size; i++) { delete[] buffer[i]; } delete[] buffer; } 替换为awk

sed

删除周围空格的前导数字(计数)。

答案 3 :(得分:0)

最后使用sed的方法(而不是awk):

find . -type f -exec file -b -- {} \; | sort | uniq -c | \
  sort -r -n | sed -E 's/^ *[0-9]+ //'

注意:任何代码都必须允许uniq -c的输出为right justified - uniq -c打印 0-6 前导空格,具体取决于独特的物品。例如:

for f in 1 10 1000 100000 1000000 10000000 ; do yes "$f" | head -$f ; done | uniq -c
      1 1
     10 10
   1000 1000
 100000 100000
1000000 1000000
10000000 10000000