The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
UNIX newbie NEWBIE question! Hanamachi UNIX for Dummies Questions & Answers 4 03-28-2009 04:10 PM
passing variable from bash to perl from bash script arsidh Shell Programming and Scripting 10 06-04-2008 01:25 PM
Taking Aim iBot Complex Event Processing RSS News 0 11-14-2007 08:10 AM
quick newbie bash question redsand9009 Shell Programming and Scripting 4 01-25-2007 03:44 AM
Newbie - Need help in bash scripting coolbib Shell Programming and Scripting 4 08-11-2002 09:28 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-17-2007
Asylus Asylus is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 3
Exclamation Bash/AWK Newbie taking on more than he can chew.

A few questions: I'm trying to use Bash (although I'm not against using AWK) to try to accomplish a few things, but I'm stumped on a few points.

I'm learning most of the basics quickly: but there are a few things I can't figure out.

1. I'm trying to count the number of .txt files in a directory and save it in a variable.
I can use
find $DIRECTORY -name *.txt | wc -l
but can't save that in a variable.

2&3. Is it possible to actually open these files (this will be a separate task entirely) and read a specific line? All of them are formatted the same, and I need to be able to read that line and save THAT to a list/array of variables- there are over 10,000 files- and I need the fourth line in each .txt. Many of these .txt files should have this line the same, so the array will never approach 10,000 - but I do need to be able to count how many times each appears- how would I do this?

(I know how to use loops, and create variables, but not open files (and read specific lines) nor make arrays/lists/whatever you call them in bash. <-Used to Java/Python; but can't use either here.

Any help with this is greatly appreciated- and I thank you for any help you can provide.

Last bit:
the files are all named similarly... 309456_20.txt
503256_01.txt
etc.

and all the files are in subdirectories named the same as the first three digits of the file.
So 309456_20.txt would be in subdirectory 309.

I'm not looking for a fast program or even a relatively efficient one; as long as it gets the job done.

Last edited by Asylus; 12-17-2007 at 01:44 PM.. Reason: Additional Information
  #2 (permalink)  
Old 12-17-2007
Asylus Asylus is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 3
Is there no-one that can help? -Or am I trying to do something that just isn't possible?

Last edited by Asylus; 12-17-2007 at 01:41 PM.. Reason: Plea for help :(
  #3 (permalink)  
Old 12-19-2007
mph999 mph999 is offline
Registered User
  
 

Join Date: Dec 2007
Posts: 1
For part 1 ...

TESTVAR=$(find . -name log_20070911* |wc -l)
echo $TESTVAR
1

If you enclose a command in $( ) then the output of that command is used, in this case, assigned to the variable TESTVAR.

You could use ` ` also.

Martin

(Parts 2/3 I need to think about)
  #4 (permalink)  
Old 12-19-2007
Tytalus's Avatar
Tytalus Tytalus is offline Forum Advisor  
echo {1..9}^2\;|bc
  
 

Join Date: Jun 2003
Location: Scotland
Posts: 431
for x in `find . -name *.txt`
do
sed -n '4p' $x
done

should get you the 4th line from each file

and then

| sort | uniq -c will get you your counts for each line

Last edited by Tytalus; 12-19-2007 at 12:47 PM.. Reason: noticed count requirement
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:21 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0