Home Man
Search
Today's Posts
Register

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

Tags
awk, column, linux

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 11-21-2017
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.
# 2  
Old 11-21-2017
Hi, try:
Code:
awk -F, '$1~96656' STG3TOUDCMATCH >> 56_Ser

# 3  
Old 11-21-2017
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
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.
# 5  
Old 11-22-2017
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

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
Get maximum per column from CSV file, based on date column ejianu Shell Programming and Scripting 6 07-10-2017 05:08 AM
Sum column values based in common identifier in 1st column. sargotrons Shell Programming and Scripting 8 10-24-2014 03:50 PM
Check first column - average second column based on a condition jacobs.smith Shell Programming and Scripting 5 09-16-2014 10:36 AM
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
to add special tag to a column based on column condition ckwan Shell Programming and Scripting 9 11-30-2011 12:54 AM
need to remove duplicates based on key in first column and pattern in last column script_op2a Shell Programming and Scripting 13 05-18-2011 06:21 PM
Compare files column to column based on keys blackjack101 Shell Programming and Scripting 2 03-31-2010 06:46 PM
Change names in a column based on the symbols in another column repinementer Shell Programming and Scripting 8 08-14-2009 03:30 AM


All times are GMT -4. The time now is 06:20 PM.

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