简体   繁体   中英

Output of wc -l without file-extension

I've got the following line:

wc -l ./*.txt | sort -rn 

i want to cut the file extension. So with this code i've got the output: number filename.txt

for all my .txt-files in the .-directory. But I want the output without the file-extension, like this: number filename

I tried a pipe with cut for different kinds of parameter, but all i got was to cut the whole filename with this command.

wc -l ./*.txt | sort -rn | cut -f 1 -d '.'

假设文件名中没有换行符,则可以使用sed结尾的.txt

wc -l ./*.txt | sort -rn | sed 's/\.txt$//'

unfortunately, cut doesn't have a syntax for extracting columns according to an index from the end. One (somewhat clunky) trick is to use rev to reverse the line, apply cut to it and then rev it back:

wc -l ./*.txt | sort -rn | rev | cut -d'.' -f2- | rev

Using sed in more generic way to cut off whatever extension the files have:

$ wc -l *.txt | sort -rn | sed 's/\.[^\.]*$//'
 14 total
  8 woc
  3 456_base
  3 123_base
  0 empty_base

A better approach using proper mime type (what is the extension of tar.gz or such multi extensions ? )

#!/bin/bash

for file; do
    case $(file -b $file) in
        *ASCII*) echo "this is ascii" ;;
        *PDF*)   echo "this is pdf" ;;
        *)       echo "other cases" ;; 
    esac
done

This is a POC , not tested, feel free to adapt/improve/modify

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM