Check for Pattern if exists write to file | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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

Check for Pattern if exists write to file

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-16-2013
nex_asp nex_asp is offline
Banned
 
Join Date: Dec 2012
Last Activity: 21 May 2013, 2:14 AM EDT
Location: India
Posts: 70
Thanks: 20
Thanked 0 Times in 0 Posts
Check for Pattern if exists write to file

Hi ! All

I just want to search and write to new file if pattern is found in text file

following are my text files by which I want to search Month and last column number

my text file1


Code:
15-Jan-2011 25 ARTS 1255 125 125 178 198 
15-Jan-2011 25 ARTS 1255 125 125 178 198 
15-Jan-2011 25 ARTS 1255 125 125 178 198 
15-Jan-2011 25 ARTS 1255 125 125 178 198

my text file2


Code:
15-Feb-2011 25 ARTS 1255 125 125 178 119 
15-Feb-2011 25 ARTS 1255 125 125 178 119 
15-Feb-2011 25 ARTS 1255 125 125 178 119 
15-Feb-2011 25 ARTS 1255 125 125 178 119

my text file3

Code:
15 1 2011 25 ARTS 1255 125 125 178 112
15 1 2011 25 ARTS 1255 125 125 178 112
15 1 2011 25 ARTS 1255 125 125 178 112
15 1 2011 25 ARTS 1255 125 125 178 112

my text file4

Code:
15 2 2011 25 ARTS 1255 125 125 178 158
15 2 2011 25 ARTS 1255 125 125 178 158
15 2 2011 25 ARTS 1255 125 125 178 158
15 2 2011 25 ARTS 1255 125 125 178 158

ultimately I want to create new files like this


Code:
Jan_198_1.txt
Jan_112_2.txt
Feb_119_1.txt
Feb_158_2.txt

Any help in this regard is appreciated.
Sponsored Links
    #2  
Old 03-16-2013
anbu23's Avatar
anbu23 anbu23 is offline Forum Advisor  
Advisor
 
Join Date: Mar 2006
Last Activity: 31 July 2014, 8:46 AM EDT
Location: Bangalore,India
Posts: 2,089
Thanks: 12
Thanked 131 Times in 126 Posts
What have you tried?

Do you want to create empty files based on your pattern?
Sponsored Links
    #3  
Old 03-17-2013
nex_asp nex_asp is offline
Banned
 
Join Date: Dec 2012
Last Activity: 21 May 2013, 2:14 AM EDT
Location: India
Posts: 70
Thanks: 20
Thanked 0 Times in 0 Posts
No I want to copy contents of $file to new file to be created.

---------- Post updated at 09:13 AM ---------- Previous update was at 05:26 AM ----------

please tel me.. whether its possible or impossible in bash ?
    #4  
Old 03-17-2013
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 2 September 2014, 9:11 PM EDT
Location: Galactic Empire
Posts: 3,362
Thanks: 230
Thanked 1,197 Times in 1,125 Posts
How about an awk program?

Code:
awk -F'[- ]' ' BEGIN {
        mon = "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"
        split ( mon, M );
} {
        sub (/[ \t]+$/, X)
        for ( i = 1; i <= 12; i++ )
        {
                if ( $2 == M[i] || $2 == i )
                {
                        n = M[i] "_" $NF
                        if ( n in FA )
                        {
                                F = M[i] "_" $NF "_" C[i] ".txt"
                        }
                        else
                        {
                                C[i] += 1
                                F = M[i] "_" $NF "_" C[i] ".txt"
                                n = M[i] "_" $NF
                                FA[n] = n
                        }
                        print $0 >> F
                }
        }
        close (F)
} ' file*

Use nawk instead in SunOS or Solaris
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
File exists, but cannot be opened.How to check- whether it could be opened to read when it exists rxg Shell Programming and Scripting 2 05-10-2012 02:15 PM
how to check to see if a file exists? astropi Shell Programming and Scripting 2 11-20-2011 05:01 PM
Check to see if a file exists? buechler66 Shell Programming and Scripting 5 06-28-2011 10:38 PM
check if file exists with pattern matching sreenu.shell Shell Programming and Scripting 6 07-17-2009 02:52 AM
Need to write a script in UNIX to find a file if another file exists mmdawg Shell Programming and Scripting 1 05-04-2008 10:40 PM



All times are GMT -4. The time now is 10:06 PM.