The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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
Pattern matching in file and then display 10 lines above every time namishtiwari Shell Programming and Scripting 30 04-13-2009 11:40 AM
pattern matching over more lines trek Shell Programming and Scripting 3 04-22-2008 06:37 AM
Script to find file name for non matching pattern sujoy101 Shell Programming and Scripting 5 03-31-2008 09:10 AM
Search file for pattern and grab some lines before pattern frustrated1 Shell Programming and Scripting 2 12-22-2005 03:41 PM
getting file words as pattern matching arunkumar_mca High Level Programming 5 05-31-2005 03:28 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-24-2007
torenji torenji is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 1
Reading lines in a file matching a pattern

Hi,

I need to redirect the lines in a file to a different file if the character starting from 2 to 6 in the line are numerical [0-9].

Please let me know if anyone have any script to do this.

Thanks,
Ranjit
  #2 (permalink)  
Old 10-24-2007
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,715
Can you give us an example set of the lines of the file.
  #3 (permalink)  
Old 10-24-2007
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,078
awk

Hi,

First of all, i would like to give you a suggestion that while you are here. try to give a example about your thread. Then it will be easy for others to understand your requirements and give you the suitable solution.

I assume your requirements as follow:

input:
Code:
a23456 line is the first line
abcd line is the second line
12345644 line is the third line
sadfjkl line is the forth line
output ( content in another file):
Code:
a23456 line is the first line
12345644 line is the third line
code:
awk '{
if (substr($0,2,5)+0==substr($0,2,5))
print
}' firstfile > secondfile
  #4 (permalink)  
Old 10-25-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,507
using same example
Code:
# awk 'substr($0,2,5) ~ /[0-9]+/' "file"
a23456 line is the first line
12345644 line is the third line
  #5 (permalink)  
Old 10-25-2007
Yogesh Sawant's Avatar
Yogesh Sawant Yogesh Sawant is offline Forum Staff  
Part Time Moderator and Full Time Dad
  
 

Join Date: Sep 2006
Location: Rossem, Tazenda
Posts: 1,086
using Perl:
Code:
#!/usr/bin/perl
# extract_num_lines.pl
while (<>) {
    print if (m/^.\d{5}/);
}
Run this script as:
Code:
perl extract_num_lines.pl num_file > new_file
^ represents start of line
. represents a single character
\d represents a digit
\d{5} represent exactly five digits
Sponsored Links
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 03:49 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