LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 02-12-2014, 01:57 PM   #1
rpascua
LQ Newbie
 
Registered: Dec 2011
Location: USA
Distribution: RHEL5, RHEL6, CentOS
Posts: 8

Rep: Reputation: Disabled
"for loop" command to separate lines in desired format


I have several directories that contain dozens of files and I'm trying to do a word count on each file and display it on screen. Here's the command I'm using.

for i in *; do echo $i \ ; find $i -type f | wc -l; done

The result looks something like this:

dir1
23
dir2
105
dir3
50

I would like the result to look more like this:

dir1 23
dir2 105
dir3 50

Any suggestions?
 
Old 02-12-2014, 02:19 PM   #2
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Here's a possibility:
Code:
find -type f -print0 | xargs -0 wc -l
If you need the columns switched, you could use awk:
Code:
find -type f -print0 | xargs -0 wc -l | awk '{ print $2" "$1 }'
or you you can use a printf instead of print, it works like C printf.
 
1 members found this post helpful.
Old 02-12-2014, 03:01 PM   #3
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
With this InFile ...
Code:
dir1
23
dir2
105
dir3
50
... this sed ...
Code:
sed '$!N;s/\n/ /' $InFile >$OutFile
... produced this OutFile ...
Code:
dir1 23
dir2 105
dir3 50
Daniel B. Martin
 
Old 02-12-2014, 03:18 PM   #4
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
Looks like you are counting files in a directory ( and subdirecties )

Slight variation on your original

Code:
for i in */;do echo $i $(find "$i" -type f | wc -l);done
If you are after the word count of each file in the subdirs then metaschima's will get that


Note, the trailing / in */
this convinces bash to only condider dirs.

In the find. subshell $() note the "" around $i
this prevents word splitting, therefore will work as desired even if dir has spaces in it
 
1 members found this post helpful.
Old 02-12-2014, 04:03 PM   #5
gengisdave
Member
 
Registered: Dec 2013
Location: Turin, Italy
Distribution: slackware
Posts: 328

Rep: Reputation: 74
"for loop" command to separate lines in desired format

echo -n doesn't put a newline char
 
Old 02-12-2014, 04:10 PM   #6
rpascua
LQ Newbie
 
Registered: Dec 2011
Location: USA
Distribution: RHEL5, RHEL6, CentOS
Posts: 8

Original Poster
Rep: Reputation: Disabled
Thank you!

Quote:
Originally Posted by Firerat View Post
Looks like you are counting files in a directory ( and subdirecties )

Slight variation on your original

Code:
for i in */;do echo $i $(find "$i" -type f | wc -l);done
If you are after the word count of each file in the subdirs then metaschima's will get that


Note, the trailing / in */
this convinces bash to only condider dirs.

In the find. subshell $() note the "" around $i
this prevents word splitting, therefore will work as desired even if dir has spaces in it
This worked for me. Thank you!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Why "cat" command is not executed as a separate process ? techie_san778 Linux - Server 4 10-19-2013 12:55 AM
how can I "cat" or "grep" a file to ignore lines starting with "#" ??? callagga Linux - Newbie 7 08-16-2013 06:58 AM
Format command shows "Cannot get current directory - I/O error" devn Solaris / OpenSolaris 1 06-18-2008 01:26 AM
"Permission denied" and "recursive directory loop" when searching for string in files mack1e Linux - Newbie 5 06-12-2008 07:38 AM
file format why difference between "command line and GUI interface" hocheetiong Linux - Newbie 3 09-17-2007 07:19 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration