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
need loop clarification for the below code mail2sant Shell Programming and Scripting 2 10-30-2008 03:33 PM
Pick up the return code for every iteration and display the result only once in loop. manas6 Shell Programming and Scripting 1 10-21-2008 08:12 AM
loop through the directory for files and sort by date and process the first file dsdev_123 AIX 1 01-30-2008 05:31 PM
the given code goes in infinite loop and does not increment variable i mrityunjay22 Shell Programming and Scripting 6 12-26-2007 02:20 AM
Loop through files in a directory rladda Shell Programming and Scripting 4 06-24-2005 06:30 AM

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 01-19-2009
cgkmal cgkmal is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 47
Loop certain code to all files within directory

Hi all,

Can somebody help me with this problem pls.

I need to extract one specific line from each files in a folder and put
the all lines extracted in a unique output file in the following format.

line extracted, respective name of file, date of file.

I´m, trying the part to extract the desired line with



Code:
awk '/Category/ {getline; print}' infile.txt

But I dont know how to do a loop over all files and send all lines
to one outfile.txt with the format desired.

Example:
If the line extracted from one specific file with



Code:
awk '/Category/ {getline; print }' infile.txt

is "District school", the desire result would be:



Code:
District school,  file date,  file name

Thanks in advance for any help
  #2 (permalink)  
Old 01-19-2009
angheloko's Avatar
angheloko angheloko is offline
Registered User
  
 

Join Date: Jul 2008
Location: Philippines
Posts: 125
Hi cg,

You can loop through the files inside a directory like this:


Code:
FLDR="."
cd $FLDR
find . ! -name . -prune -type f | while read x; do
   # this will print the filename
  #echo $x

  # To get the date
  #ls -lt $x | awk '{print $6" "$7" "$8}' 
  # If the file is older than 6 months (not sure though), this will include the year. If not, this will include the time

  # What you want maybe like this: 
  found=`awk '/Category/ {getline; print }' "$x"`
  [ "$found" != "" ] && echo "$found,$x,"`ls -lt $x | awk '{print $6" "$7" "$8}'`
done

  #3 (permalink)  
Old 01-19-2009
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,099

Code:
#!/usr/bin/perl
@files=glob("*.txt");
open FH,">out.txt";
foreach(@files){
	open F,"<$_";
	my $file=$_;
	while(<F>){
		print FH $file," line $.: ",$_ if /Toyota/;
	}
	close F;
}
close FH;

  #4 (permalink)  
Old 01-20-2009
cgkmal cgkmal is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 47
Hi anghel and summer_cherry,

Simply perfect!!!, exactly what I need.

Very appreciated your always kindly help.

Best regards
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 07:59 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