The UNIX and Linux Forums  

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
grep for a particular pattern and remove few lines above top and bottom of the patter fed.linuxgossip Shell Programming and Scripting 17 07-25-2008 09:29 AM
grep required pattern and next 2 or 3 lines cvvsnm UNIX for Dummies Questions & Answers 3 02-01-2008 05:20 AM
grep to show lines only after pattern wannalearn Shell Programming and Scripting 5 10-09-2007 03:44 AM
How to select lines in unix matches a pattern at a particular position cs_banda UNIX for Dummies Questions & Answers 2 10-06-2006 04:28 PM
grep - to exclude lines beginning with pattern frustrated1 Shell Programming and Scripting 2 08-29-2005 08:18 AM

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

Join Date: Oct 2008
Posts: 4
How to awk/sed/grep lines which contains a pattern at a given position

Dear friends

I am new to linux and was trying to split some files userwise in our linux server.

I have a data file of 156 continuous columns named ecscr final.

I want the script to redirect all the lines containing a pattern of 7 digits to separate files. I was using grep to do that, as below. I have written all available patterns to a file named usercode.

while read alpha
do
cat ecscrfinal | grep -e $alpha >> mkoff.$alpha
echo "Generating markoff data for $alpha"
done < /home/ftp/ecs/usercode


But I need only those lines which have the pattern occuring at 3rd column or 81st column to go to that file. In the above case if the pattern is found any wher in the line, it is redirected to that file which gives me a wrong result.

How sed/awk/grep can be used to match a pattern occuring only at a specified field.....?

The pattern should be matched only if it is occuring in 3rd position or 81st postition.

Please help me.....i am running against a deadline.........
  #2 (permalink)  
Old 10-29-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
  
 

Join Date: Dec 2007
Location: Home of 17-time world champion Boston Celtics
Posts: 1,311
Wink The following may work for you


Code:
> cat file20
abcde1234567ffffffffggggggg
abcdezzzzzzzffffffffggggggg
abczzyyyyyyygggggggg1212121

> awk 'substr($0,6,7)<"AAAAAAA" || substr($0,21,7)<"AAAAAAA"' file20
abcde1234567ffffffffggggggg
abczzyyyyyyygggggggg1212121

It selects records that are numeric at either of two positions in the data record.
  #3 (permalink)  
Old 10-29-2008
anoopvraj anoopvraj is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 4
i tried like this

while read alpha
do
awk 'substr($0,3,7)<"$alpha"||substr($0,81,7)<"$alpha"'<ecscrfinal >> mkoff.$alpha
awk 'substr($0,3,7)<"$alpha"||substr($0,81,7)<"$alpha"' <ecsdrfinal >> mkoff.$alpha
done < /home/ftp/ecs/usercode

which yielded no results

then like this, $alph without quotes;

while read alpha
do
awk 'substr($0,3,7)<$alpha||substr($0,81,7)<$alpha'<ecscrfinal >> mkoff.$alpha
awk 'substr($0,3,7)<$alpha||substr($0,81,7)<$alpha' <ecsdrfinal >> mkoff.$alpha
done < /home/ftp/ecs/usercode

In this case the entire file is copied to all the userwise split files

What I am doing wrong..... Cant I do substitution inside awk...??
Closed Thread

Bookmarks

Tags
awk distinct column values, grep, sed

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 04:02 PM.


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