2

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 '.'
Mureinik
  • 297,002
  • 52
  • 306
  • 350

4 Answers4

2

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$//'
anubhava
  • 761,203
  • 64
  • 569
  • 643
1

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
Mureinik
  • 297,002
  • 52
  • 306
  • 350
1

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
James Brown
  • 36,089
  • 7
  • 43
  • 59
0

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

Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223