135

Is there a way to get the integer that wc returns in bash?

Basically I want to write the line numbers and word counts to the screen after the file name.

output: filename linecount wordcount Here is what I have so far:

files=`ls`
for f in $files;
do
        if [ ! -d $f ] #only print out information about files !directories
        then
                # some way of getting the wc integers into shell variables and then printing them
                echo "$f $lines $ words"
        fi
done

4

19 に答える 19

99

これまでで最も簡単な答え:

wc < filename 
于 2012-01-29T13:31:33.337 に答える
63

cutコマンドを使用して、wcの出力の最初の単語 (行数または単語数)だけを取得できます。

lines=`wc -l $f | cut -f1 -d' '`
words=`wc -w $f | cut -f1 -d' '`
于 2010-09-19T18:40:31.827 に答える
53
wc $file | awk {'print "$4" "$2" "$1"'}

必要に応じてレイアウトを調整してください。

また、否定的な論理 (「ディレクトリではない」) よりも肯定的な論理 (「ファイルです」) を使用する方が適切です。

[ -f $file ] && wc $file | awk {'print "$4" "$2" "$1"'}
于 2010-09-19T18:40:06.910 に答える
46

wc は、異なるプラットフォームでは異なる形式で出力することがあります。例えば:

OS X の場合:

$ echo aa | wc -l

         1

Centos の場合:

$ echo aa | wc -l

1

したがって、カットのみを使用すると、番号が取得されない場合があります。代わりに、tr を試してスペース文字を削除してください:

$ echo aa | wc -l | tr -d ' '
于 2015-10-26T21:55:33.710 に答える
31

受け入れられた/人気のある回答は、OSX では機能しません。

以下のいずれも、bsd および linux で移植可能である必要があります。

wc -l < "$f" | tr -d ' '

また

wc -l "$f" | tr -s ' ' | cut -d ' ' -f 2

また

wc -l "$f" | awk '{print $1}'
于 2016-01-28T05:41:18.173 に答える
12

ファイル名をリダイレクトするwcと、出力時にファイル名が省略されます。

バッシュ:

read lines words characters <<< $(wc < filename)

また

read lines words characters <<EOF
$(wc < filename)
EOF

forの出力を反復処理するために使用する代わりに、次のlsようにします。

for f in *

これは、スペースを含むファイル名がある場合に機能します。

グロブを使用できない場合は、while readループにパイプする必要があります。

find ... | while read -r f

またはプロセス置換を使用する

while read -r f
do
    something
done < <(find ...)
于 2010-09-19T22:27:42.063 に答える
2

どうsedですか?

wc -l /path/to/file.ext | sed 's/ *\([0-9]* \).*/\1/'
于 2012-12-28T03:44:46.757 に答える
1
typeset -i a=$(wc -l fileName.dat  | xargs echo | cut -d' ' -f1)
于 2012-07-04T13:17:03.483 に答える
0

数値結果については、これを試してください:
nlines=$( wc -l <​​ $myfile )

于 2013-06-28T14:20:11.723 に答える
0

(1) 1wc回実行し、(2) 余分な変数を割り当てないようにするには、次を使用します。

read lines words <<< $(wc < $f | awk '{ print $1, $2 }')

完全なコード:

for f in *
do
    if [ ! -d $f ]
    then
        read lines words <<< $(wc < $f | awk '{ print $1, $2 }')
        echo "$f $lines $words"
    fi
done

出力例:

$ find . -maxdepth 1 -type f -exec wc {} \; # without formatting
 1  2 27 ./CNAME
  21  169 1065 ./LICENSE
 33 130 961 ./README.md
  86  215 2997 ./404.html
  71  168 2579 ./index.html
 21  21 478 ./sitemap.xml

$ # the above code
404.html 86 215
CNAME 1 2
index.html 71 168
LICENSE 21 169
README.md 33 130
sitemap.xml 21 21
于 2020-12-17T19:24:25.553 に答える
0

ここにstackoverflowの例を含む優れたソリューションがあります

ここで最も簡単なソリューションをコピーします。

FOO="bar"
echo -n "$FOO" | wc -l | bc                     # "3"

多分これらのページはマージされるべきですか?

于 2022-01-30T20:56:19.420 に答える
-1

これを試して:

wc `ls` | awk '{ LINE += $1; WC += $2 } END { print "lines: " LINE  " words: " WC }'

行数と単語数 (LINE と WC) を作成し、wc から抽出した値 (最初の列の値に $1、2 番目の列の値に $2 を使用) でそれらを増やし、最後に結果を出力します。

于 2010-09-19T18:43:32.960 に答える