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 '.'

Upvotes: 2

Views: 562

Answers (4)

James Brown
James Brown

Reputation: 37424

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

Upvotes: 1

anubhava
anubhava

Reputation: 785521

Assuming you don't have newlines in your filename you can use sed to strip out ending .txt:

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

Upvotes: 2

Mureinik
Mureinik

Reputation: 311843

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

Upvotes: 1

Gilles Quénot
Gilles Quénot

Reputation: 185530

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

Upvotes: 0

Related Questions