awk based on first 5 filed of column


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers awk based on first 5 filed of column
# 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:
466565996656,820012906026651 NA NOTMATCHED
4661740045165,820011902196656 NA NOTMATCHED
4661740085225,820011900196656 NA NOTMATCHED
4665640160118,820011901966563 NA NOTMATCHED
4661740160267,820011901966564 NA NOTMATCHED
4665640160500,820011901966566 NA NOTMATCHED
4661740161648,820011901966568 NA NOTMATCHED
4665640171771,820011902096656 NA NOTMATCHED

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

Code:
466565996656,820012906026651 NA NOTMATCHED
4665640160118,820011901966563 NA NOTMATCHED
4665640160500,820011901966566 NA NOTMATCHED
4665640171771,820011902096656 NA NOTMATCHED

I am trying below code

Code:
 awk -F'=|,|' '$1 == "46656" {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 hicksd8; 04-26-2020 at 08:32 AM..
# 2  
Old 11-21-2017
Hi, try:
Code:
awk -F, '$1~46656' STG3TOUDCMATCH >> 56_Ser


Last edited by hicksd8; 04-26-2020 at 08:32 AM..
# 3  
Old 11-21-2017
Thanks but this is not I am looking for. This prints data based on 96656 at any place.

Code:
466122896656,820012944069245 NA NOTMATCHED
466135996656,820012906026651 NA NOTMATCHED
4661740045165,820011902196656 NA NOTMATCHED
4661740085225,820011900196656 NA NOTMATCHED
4661740096656,820011900192131 NA NOTMATCHED
4661740160118,820011901966563 NA NOTMATCHED
4661740160267,820011901966564 NA NOTMATCHED
4661740160500,820011901966566 NA NOTMATCHED
4661740161648,820011901966568 NA NOTMATCHED
4661740171771,820011902096656 NA NOTMATCHED

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

like below

Code:
466565996656,820012906026651 NA NOTMATCHED
4665640160118,820011901966563 NA NOTMATCHED
4665640160500,820011901966566 NA NOTMATCHED
4665640171771,820011902096656 NA NOTMATCHED


Last edited by hicksd8; 04-26-2020 at 08:34 AM..
# 4  
Old 11-22-2017
Assuming that the 1st field of your data starts in the 1st character of each line, try:
Code:
awk '/^46656/' 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.

Last edited by hicksd8; 04-26-2020 at 08:34 AM..
# 5  
Old 11-22-2017
If you only want a line filter then grep is perfect
Code:
grep '^46656' 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~/^46656/' STG3TOUDCMATCH

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


Last edited by hicksd8; 04-26-2020 at 08:35 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Awk/sed summation of one column based on some entry in first column

Hi All , I am having an input file as stated below Input file 6 ddk/djhdj/djhdj/Q 10 0.5 dhd/jdjd.djd.nd/QB 01 0.5 hdhd/jd/jd/jdj/Q 10 0.5 512 hd/hdh/gdh/Q 01 0.5 jdjd/jd/ud/j/QB 10 0.5 HD/jsj/djd/Q 01 0.5 71 hdh/jjd/dj/jd/Q 10 0.5 ... (5 Replies)
Discussion started by: kshitij
5 Replies

2. Shell Programming and Scripting

Transpose matrix based on second column using awk

Hi, Is it possible to transpose the matrix like this using awk ? Many thanks in advance Input abc Name_1 0 abc Name_2 1 abc Name_3 2 abc Name_4 0.4 def Name_1 0 def Name_2 9 def Name_3 78 def Name_4 1 Output abc def Name_1 0 ... (4 Replies)
Discussion started by: quincyjones
4 Replies

3. Shell Programming and Scripting

awk to sum a column based on duplicate strings in another column and show split totals

Hi, I have a similar input format- A_1 2 B_0 4 A_1 1 B_2 5 A_4 1 and looking to print in this output format with headers. can you suggest in awk?awk because i am doing some pattern matching from parent file to print column 1 of my input using awk already.Thanks! letter number_of_letters... (5 Replies)
Discussion started by: prashob123
5 Replies

4. Shell Programming and Scripting

Sort based on column 1, not working with awk

Hi Guru, I need some help regarding awking the output so it only show the first line (based on column) of each row. So If column has 1, three row, then it only show the first line of that row, based on similar character in column 1. So i am trying to achieve a sort, based on column one and... (3 Replies)
Discussion started by: Junes
3 Replies

5. Shell Programming and Scripting

awk filter based on column value (variable value)

Hi, I have a requirement to display/write the 3rd column from a file based on the value in the column 3. Ex: Data in the File (comma delimited) ID,Value,Description 1,A,Active 1,I,Inactive 2,S,Started 1,N,None 2,C,Completed 2,F,Failed I need to first get a list of all Unique IDs in... (7 Replies)
Discussion started by: kiranredz
7 Replies

6. Shell Programming and Scripting

Pick the column value based on another column using awk or CUT

My scenario is that I need to pick value from third column based on fourth column value, if fourth column value is 1 then first value of third column.Third column (2|3|4|6|1) values are cancatenated. Please someone help me to resolve this issue. Source column1 column2 column3 column4... (2 Replies)
Discussion started by: Ganesh L
2 Replies

7. Shell Programming and Scripting

Help with awk in counting characters based on a column

Hello, I am using Awk in UBUNTU 12.04. I have a file as follows with 2172 rows and 44707 columns. ABO and GPO are the names of my populations. ABO_1 1 2 ABO_1 1 2 ABO_2 1 1 ABO_2 1 2 GPO_1 1 1 GPO_1 2 2 GPO_2 1 0 GPO_2 2 0I want to count the number of 1s and 2s in... (7 Replies)
Discussion started by: Homa
7 Replies

8. Shell Programming and Scripting

KSH or Awk first and last line based on column 2

Example: 10.160.101.160,0707073711,22.203.203.200 10.160.101.160,0707075132,22.203.210.249 10.160.101.160,0707085436,22.203.210.249 10.160.101.160,0707091712,22.203.221.176 10.160.101.160,0707091811,22.203.221.176 10.160.101.160,0707091845,22.203.221.176... (1 Reply)
Discussion started by: BrownBob
1 Replies

9. Emergency UNIX and Linux Support

awk cut column based on string

Using awk I required to cut out column contain word "-Tag" regardles of any order of contents and case INsensitive -Tag:messages -P:/var/log/messages -P:/var/log/maillog -K:Error -K:Warning -K:critical Please Guide ...... --Shirish Shukla ---------- Post updated at 05:58 AM... (15 Replies)
Discussion started by: Shirishlnx
15 Replies

10. Shell Programming and Scripting

AWK: how to get average based on certain column

Hi, I'm new to shell programming, can anyone help me on this? I want to do following operations - 1. Average salary for each country 2. Total salary for each city and data that looks like - salary country city 10000 zzz BN 25000 zzz BN 30000 zzz BN 10000 yyy ZN 15000 yyy ZN ... (3 Replies)
Discussion started by: shell123
3 Replies
Login or Register to Ask a Question