|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
||||
|
||||
|
What have you tried?
Do you want to create empty files based on your pattern? |
| Sponsored Links | ||
|
|
#3
|
|||
|
|||
|
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
|
||||
|
||||
|
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 | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|