Bash/AWK Newbie taking on more than he can chew.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash/AWK Newbie taking on more than he can chew.
# 1  
Old 12-17-2007
Error 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. Smilie

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? Smilie

(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. Smilie

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

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  
Old 12-17-2007
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  
Old 12-19-2007
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  
Old 12-19-2007
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Newbie needs help with some bash scripting

Please bear with me, I'm a beginner but have had some experience and decent knowledge to understand things as I read them and I'm in the process of trying to learn more. I recently inherited a UNIX server which has a bash script which is part of a crontab schedule that needs to be modified (or... (3 Replies)
Discussion started by: Danylko
3 Replies

2. Shell Programming and Scripting

BASH loop -- newbie

Hi forums, Wondering if someone could assist me!!... I basically want to do a while read line on a file and input it into a set of parameters for a JSON geospatial demo.. With a loop where every line creates a new "card" to an effect... card example below... Data CSV one line: ... (4 Replies)
Discussion started by: skoot
4 Replies

3. Shell Programming and Scripting

Bash scripting - Newbie

Hi all, I have drill to do and I'll very appreciate your help: Please create a simple CSV file as follow (3 columns and 4 rows): A,B,C A,”B,C”,D “A,B”,C,D o A,B,”C,D” - Please refer to the comma between the quotation marks as a parameter and not as a separator. - Please provide... (3 Replies)
Discussion started by: elior
3 Replies

4. Shell Programming and Scripting

Taking inputs for awk

Hi, i want to print 2nd column value with the below script. I need to take input of the string i need to search in that file and file name. How can i take these two as inputs? using read command? Getting error for below script. echo "enter SID" read SID echo "enter filename" read filename... (8 Replies)
Discussion started by: sam_bd
8 Replies

5. Shell Programming and Scripting

Cannot get the correct ans. Using awk in taking average

Hi all, I think so I’m getting the result is wrong, while using following awk commend, colval=$(awk 'FNR>1 && NR==FNR{a=$4;next;} FNR>1 {a+=$4; print $2"\t"a/3}' filename_f.tsv filename_f2.tsv filename_f3.tsv) echo $colval >> Result.tsv it’s doing the condition 2 times, first result... (5 Replies)
Discussion started by: Shenbaga.d
5 Replies

6. UNIX for Dummies Questions & Answers

awk solution for taking bins

Hi all, I'm looking for an awk solution for taking bins of data set. For example, if I have two columns of data that I wish to use for a scatter plot, and it contains 5 million lines, how can I take averages of every 100 points, 1000, 10000 etc... The idea is to take bins of the 5,000,000 points... (7 Replies)
Discussion started by: torchij
7 Replies

7. Shell Programming and Scripting

Looking for help with parsing file contents in bash [newbie]

Hi I'm just messing around with bash and trying to learn it because I have a course next semester dealing with OS design where we need to know how to use SSH client and either bash or ksh. I've never done shell scripting before. I just started today and I was wondering how parsing files... (1 Reply)
Discussion started by: mehungry
1 Replies

8. Shell Programming and Scripting

Newbie bash scripting help requested

Hi, I'm very new to bash scripting and Linux in general. I'm running Ubuntu Server 10.04 and trying to write a bash script to launch a program. In doing so, I've come across a couple of things that I obviously don't understand. Here is a short script that exemplifies those things: ... (9 Replies)
Discussion started by: Carson Dyle
9 Replies

9. Shell Programming and Scripting

quick newbie bash question

I'm in .profile. I want to export a directory ( export MY_TOOL_HOME=/tools/my tool". Unfortunately for me, the directory I want to export to has a space in its name. So when I try to cd $MY_TOOL_HOME, i get a No such file or directory at the command line... thanks for the help (4 Replies)
Discussion started by: redsand9009
4 Replies

10. Shell Programming and Scripting

Newbie - Need help in bash scripting

Hi there, I am a student and currently working on a project. I have a file that contains about 50 filenames. (1.txt, 2.txt, 3.txt ...). I would like to know how can I store these filenames into a variable using a loop? I would appreciate if anyone can help me. Thank You. Regards, Bib (4 Replies)
Discussion started by: coolbib
4 Replies
Login or Register to Ask a Question