UNIX for Beginners Questions & Answers

If you're not sure where to post a Unix or Linux question, post it here. All unix and Linux beginners welcome in this forum!

awk based on first 5 filed of column

awk, column, linux

👤 Login to reply
 
Thread Tools Search this Thread Display Modes
    #1  
Old 11-21-2017
shanul karim shanul karim is offline
Registered User
 
Join Date: Aug 2017
Last Activity: 21 November 2017, 11:06 PM EST
Posts: 20
Thanks: 5
Thanked 0 Times in 0 Posts
awk based on first 5 filed of column

Dear Team,


I need support to use awk program to grep data based on matching pattern first 5 digits "96656" .

Data I have as below

Code:
966565996656,420012906026651 NA NOTMATCHED
9661740045165,420011902196656 NA NOTMATCHED
9661740085225,420011900196656 NA NOTMATCHED
9665640160118,420011901966563 NA NOTMATCHED
9661740160267,420011901966564 NA NOTMATCHED
9665640160500,420011901966566 NA NOTMATCHED
9661740161648,420011901966568 NA NOTMATCHED
9665640171771,420011902096656 NA NOTMATCHED

Need awk program which filter data as below with first column having first 5 digits matching as "96656"

Code:
966565996656,420012906026651 NA NOTMATCHED
9665640160118,420011901966563 NA NOTMATCHED
9665640160500,420011901966566 NA NOTMATCHED
9665640171771,420011902096656 NA NOTMATCHED

I am trying below code

Code:
 awk -F'=|,|' '$1 == "96656" {print $0}'STG3TOUDCMATCH >> 56_Ser

but this code filtering data with 96656 at any place in 1st column. But I need to filter based on first 5 digit of 1 column that match "96656".

Thanks in advance.

Last edited by Don Cragun; 11-22-2017 at 02:59 AM.. Reason: Get rid of SIZE tags.
Sponsored Links
    #2  
Old 11-21-2017
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 18 July 2018, 7:48 AM EDT
Location: Amsterdam
Posts: 11,853
Thanks: 546
Thanked 3,469 Times in 3,057 Posts
Hi, try:
Code:
awk -F, '$1~96656' STG3TOUDCMATCH >> 56_Ser

Sponsored Links
    #3  
Old 11-21-2017
shanul karim shanul karim is offline
Registered User
 
Join Date: Aug 2017
Last Activity: 21 November 2017, 11:06 PM EST
Posts: 20
Thanks: 5
Thanked 0 Times in 0 Posts
Thanks but this is not I am looking for. This prints data based on 96656 at any place.

Code:
966122896656,420012944069245 NA NOTMATCHED
966135996656,420012906026651 NA NOTMATCHED
9661740045165,420011902196656 NA NOTMATCHED
9661740085225,420011900196656 NA NOTMATCHED
9661740096656,420011900192131 NA NOTMATCHED
9661740160118,420011901966563 NA NOTMATCHED
9661740160267,420011901966564 NA NOTMATCHED
9661740160500,420011901966566 NA NOTMATCHED
9661740161648,420011901966568 NA NOTMATCHED
9661740171771,420011902096656 NA NOTMATCHED

I need to filter data based on first column starting with "96656".

like below

Code:
966565996656,420012906026651 NA NOTMATCHED
9665640160118,420011901966563 NA NOTMATCHED
9665640160500,420011901966566 NA NOTMATCHED
9665640171771,420011902096656 NA NOTMATCHED

    #4  
Old 11-22-2017
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 18 July 2018, 7:28 AM EDT
Location: San Jose, CA, USA
Posts: 11,407
Thanks: 649
Thanked 3,970 Times in 3,393 Posts
Assuming that the 1st field of your data starts in the 1st character of each line, try:
Code:
awk '/^96656/' STG3TOUDCMATCH > 56_Ser

Note that there must be a space between the single quote at the end of the awk script and the name of the file awk is supposed to process.
Sponsored Links
    #5  
Old 11-22-2017
MadeInGermany MadeInGermany is offline Forum Staff  
Moderator
 
Join Date: May 2012
Last Activity: 18 July 2018, 2:42 AM EDT
Location: Simplicity
Posts: 4,156
Thanks: 365
Thanked 1,419 Times in 1,275 Posts
If you only want a line filter then grep is perfect
Code:
grep '^96656' STG3TOUDCMATCH

If you later want to add more field-based filters or output formatting then use awk with its automatic field splitting
Code:
awk -F, '$1~/^96656/' STG3TOUDCMATCH

For example, you can add a condition "and field#2 must end with 56"
Code:
awk -F, '$1~/^96656/ && $2~/56$/' STG3TOUDCMATCH

Sponsored Links
👤 Login to reply

« Previous Thread | Next 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
Sum column values based in common identifier in 1st column. sargotrons Shell Programming and Scripting 8 10-24-2014 03:50 PM
awk to sum a column based on duplicate strings in another column and show split totals prashob123 Shell Programming and Scripting 5 01-10-2014 12:39 PM
Filtering lines for column elements based on corresponding counts in another column polsum Shell Programming and Scripting 1 03-05-2012 09:28 AM
Compare files column to column based on keys blackjack101 Shell Programming and Scripting 2 03-31-2010 06:46 PM



All times are GMT -4. The time now is 08:41 AM.

Unix & Linux Forums Content Copyrightę1993-2018. All Rights Reserved.
×
UNIX.COM Login
Username:
Password:  
Show Password





Not a Forum Member?
Forgot Password?