Conditional Looping In Files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Conditional Looping In Files
# 1  
Old 06-03-2013
Conditional Looping In Files

I have a req. where i need to read data from multiple files and take counts of row which satisfy the condition. e.g.:
FILE1:
Code:
Col1 Col2 Col3
12 ab cd
15 de fg
25 gh tm

FILE2:
Code:
Col1 Col2 Col3
21 ab1 cd1
13 de1 fg1
25 gh1 tm1
---
---

FILE-N...

i need to find the count of rows where COL1 is between 10-20..result shoud be like:
FILE1.txt 2
FILE2.txt 1
-----------
FILE-N.txt n

Last edited by Scott; 06-03-2013 at 06:45 AM.. Reason: Please use code tags for code and data
# 2  
Old 06-03-2013
Code:
awk '$1>9 && $1 <21{a[FILENAME]++} END {for (i in a) {print i,a[i]}}' file1 file2 fileN

This User Gave Thanks to pravin27 For This Post:
# 3  
Old 06-03-2013
Thanks for the response. It would be great if you could explain it little. I am new to shell scripting.
# 4  
Old 06-03-2013
awk will read all files one by one and do all commands on all files
$1>9 && $1 <21 If column #1 have a value more then 9 and less then 21
Then create an array a with index of the FILENAME
The ++ tells that array should have one extra number for every hit.

After all file is counted then this END {for (i in a) {print i,a[i]}} will list all array that is created i in a and the value of them

PS array take some time to understand, but reading at testing will help a lot.
# 5  
Old 06-03-2013
awk is the precise solution, and easily adaptable to other conditions.
A quick and dirty solution is
Code:
egrep -c '^(1[0-9]|20)[[:blank:]]' FILE*.txt

but you cannot modify the printout format, and for a changing condition you'll have to develop a new regular expression.

Last edited by MadeInGermany; 06-03-2013 at 01:50 PM.. Reason: better match
# 6  
Old 06-03-2013
@MadeInGermany: this would count 1 abc def as well, or 200 xyz sss ...
This User Gave Thanks to RudiC For This Post:
# 7  
Old 06-03-2013
Thanks, I have improved the match in my post.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Error in looping through files

Hi, I've got a folder with several files I'd like to manipulate. The file names are all ending in .txt and I'd like to loop through their names for manipulation. This is the script I've got so far: for i in 'ls *.gtc.txt|cut -d "." -f1'; do echo${i}; done It should be easy enough, but... (2 Replies)
Discussion started by: zajtat
2 Replies

2. Shell Programming and Scripting

Looping through Files

Hi all , I am new on this forum . I have to face a particoular implementation issue and I need some help . Requirement : I need to read a particoular file (an xml file) and after reading it I need to call an Oracle Stored Procedure passing the content of the file as paramenter , in order... (3 Replies)
Discussion started by: Kolas79
3 Replies

3. Shell Programming and Scripting

looping through files

I am writing a ksh which has to load 7 files(.dat files) from input directory into oracle tables using sql loader. The process has to take each file at a time and once if it is loaded succesfully using sql loader into oracle tables then the process has to pick next file and load it into oracle... (2 Replies)
Discussion started by: vpv0002
2 Replies

4. UNIX for Dummies Questions & Answers

conditional renaming of files

Hello, I want to rename all the files in my directory according to the following criterion: IF the original filename does not end in 0 THEN rename the file to "original filname" but with the last character preceded by the string: _copy IF the original filename ends in 0 THEN take the... (2 Replies)
Discussion started by: juliette salexa
2 Replies

5. Shell Programming and Scripting

Looping through 2 files simultaneously

Hi all, I'm having a problem with a script which should ultimately provide a filename by reading a value from file1 and file2 then join together. I'm planning to use a loop/ loops to get the values out of both files and create a single string unfortunately the code currently treats the second... (7 Replies)
Discussion started by: chris01010
7 Replies

6. Shell Programming and Scripting

Looping through files...

I posted this in the Solaris forum, but I don't think it's platform specific, so I'm posting it here. Here is the situation. We are a company that has been using a professional publishing system, the software is called "ProType". It runs on Solaris 2.4, however it is no longer supported and we... (6 Replies)
Discussion started by: Fred Goldman
6 Replies

7. Shell Programming and Scripting

Looping on a list of files...

This isn't working for multiple files. It works for one file though. exists1=$(ls | grep gspp*) for FILES in $exists1 do echo "Loading $exists1" ... (23 Replies)
Discussion started by: lazerfoursix
23 Replies

8. Shell Programming and Scripting

Looping and conditional branching on the command line

I am piping STDOUT from commands such as ifconfig and dmesg through grep, sed and awk to get the information I need. I need to now perform some looping and branching now and have been trying to figure out how to do this on the command line. You may ask "Why the command line? - Why not put it... (2 Replies)
Discussion started by: karlgo
2 Replies

9. Shell Programming and Scripting

Help looping through files, please...

Okay... I've solved one problem. Here's the next. I'm writing a script file that needs to go through a directory and list all files in that directory. I'm using TCL/TK. I figured out how to go through the directory and how to loop through it, but I ran into a little problem. ... (2 Replies)
Discussion started by: kapolani
2 Replies

10. Shell Programming and Scripting

looping files

Hi, I have a file a.lst which lists all files. as a.dat b.dat c.dat I want to process these files mentioned in the list file in a loop. Say I want to display only the first line of all the files a.dat , b.dat, c.dat. How can I go about it? Please help. (5 Replies)
Discussion started by: dharmesht
5 Replies
Login or Register to Ask a Question