LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 01-24-2008, 09:08 AM   #1
amitpardesi
LQ Newbie
 
Registered: Aug 2007
Posts: 13

Rep: Reputation: 0
pattern matching in file


I have a list of files with its complete path written in a file. The list consists of various types of files like .c, .h, .cgi, .pl[x], .cpp, etc.

eg:
...
...
./usr/src/kernel/linux-2.6.23/fs/ext3/inode.c
./home/abc/UI/myfile.pl
./home/abc/UI/cgi/edit.cgi
...
...

I want to create a diff file for each kind of file type.

Could someone please help!

Thanks
 
Old 01-24-2008, 10:49 AM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
It is hard to understand what you actually want. diff outputs the differences between a pair of files, but from your list of files, it is not obvious what would constitute a 'pair'. Please explain in some more detail what you are trying to accomplish.

--- rod.
 
Old 01-24-2008, 11:09 PM   #3
amitpardesi
LQ Newbie
 
Registered: Aug 2007
Posts: 13

Original Poster
Rep: Reputation: 0
I have a file, which I constructed using 'find . -type f > myfile.txt' command. This gave me the list of files in the current directory from where I execute this command.

There are various kinds (.c, .h, .pl, .cgi, .sh, etc) of files in the list. I want to separate out C/C++ file names in one file & Script files like .pl, .sh, etc. in another file.
 
Old 01-25-2008, 10:17 AM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
So, you want to sort the original list into sub-files by file type? This should do it.
Code:
#! /usr/bin/perl -w
#
#   LQamitpardesi.pl
#
#   Sort input list by filetype and write to sublist files.
#
#   Usage: LQamitpardesi.pl myfile.txt
#
#
use strict;
my %fileTypes;

    while(<>){
        chomp $_;
        if( $_ =~ m/.+\.([^\/]+)$/ ){
            push @{$fileTypes{$1}}, $_;
        }
    }
    
    foreach my $type ( sort keys %fileTypes ){
        my $typeFile = $type."_files.LQamitpardesi";
        open( FILE, ">$typeFile" ) || die "Cannot open '$typeFile' for output : $!\n";
        foreach my $fileName ( @{$fileTypes{ $type }} ){
            print FILE $fileName,"\n";
        }
        close FILE;
    }
--- rod.
 
Old 02-08-2008, 05:58 AM   #5
amitpardesi
LQ Newbie
 
Registered: Aug 2007
Posts: 13

Original Poster
Rep: Reputation: 0
Question Inserting new line on 2nd line

I have lots of perl / shell files. I want to insert a common line in all the files on the 2nd line of the file.

How do we insert a new line in the 2nd line of the file ?
 
Old 02-08-2008, 07:06 AM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
It's normally better to start a new thread when you have a new question (but don't do it now!!)

One option:
sed '2 i <text to add>' filename > newfilename

Really good SED tutorial here: http://www.grymoire.com/Unix/Sed.html
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pattern Matching Problem ratul_11 Programming 3 12-28-2007 12:27 AM
pattern matching nadeemr Linux - Newbie 8 06-13-2007 11:05 AM
Sed pattern matching digitalbrutus Programming 1 08-20-2006 01:37 PM
Linux/Unix script for file pattern matching varunnarang Programming 1 08-07-2006 01:14 PM
Perl pattern matching in VB rigel_kent Programming 1 05-30-2006 11:00 AM


All times are GMT -5. The time now is 09:02 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration