Unix/Linux Go Back    


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

UNIX for Beginners Questions & Answers


Tags
awk, column, linux

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 11-21-2017   -   Original Discussion by shanul karim
shanul karim's Unix or Linux Image
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 03:59 AM.. Reason: Get rid of SIZE tags.
Sponsored Links
    #2  
Old Unix and Linux 11-21-2017   -   Original Discussion by shanul karim
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 23 January 2018, 12:16 PM EST
Location: Amsterdam
Posts: 11,713
Thanks: 532
Thanked 3,402 Times in 2,997 Posts
Hi, try:


Code:
awk -F, '$1~96656' STG3TOUDCMATCH >> 56_Ser

Sponsored Links
    #3  
Old Unix and Linux 11-21-2017   -   Original Discussion by shanul karim
shanul karim's Unix or Linux Image
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 Unix and Linux 11-22-2017   -   Original Discussion by shanul karim
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is online now Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 23 January 2018, 4:22 PM EST
Location: San Jose, CA, USA
Posts: 10,947
Thanks: 611
Thanked 3,824 Times in 3,268 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 Unix and Linux 11-22-2017   -   Original Discussion by shanul karim
MadeInGermany's Unix or Linux Image
MadeInGermany MadeInGermany is online now Forum Staff  
Moderator
 
Join Date: May 2012
Last Activity: 23 January 2018, 4:27 PM EST
Location: Simplicity
Posts: 3,953
Thanks: 335
Thanked 1,325 Times in 1,195 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
Reply

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
Sum column values based in common identifier in 1st column. sargotrons Shell Programming and Scripting 8 10-24-2014 04: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 01:39 PM
Filtering lines for column elements based on corresponding counts in another column polsum Shell Programming and Scripting 1 03-05-2012 10:28 AM
Compare files column to column based on keys blackjack101 Shell Programming and Scripting 2 03-31-2010 07:46 PM



All times are GMT -4. The time now is 05:46 PM.