Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Check for Pattern if exists write to file

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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 Unix and Linux 03-16-2013
anbu23's Unix or Linux Image
anbu23 anbu23 is offline Forum Advisor  
Advisor
 
Join Date: Mar 2006
Last Activity: 18 July 2016, 7:20 AM EDT
Location: Bangalore,India
Posts: 2,152
Thanks: 21
Thanked 159 Times in 150 Posts
What have you tried?

Do you want to create empty files based on your pattern?
Sponsored Links
    #3  
Old Unix and Linux 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 Unix and Linux 03-17-2013
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 26 August 2016, 6:38 PM EDT
Location: Galactic Empire
Posts: 3,520
Thanks: 248
Thanked 1,275 Times in 1,197 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 Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 11:18 AM.