filtering awk not using grep


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting filtering awk not using grep
# 1  
Old 11-28-2011
filtering awk not using grep

hallow all i have question about awk i want indexing last key (in BOLD number)

ex: input.txt
1
Code:
252468812
52468812
1281268819
1252468812
1252468923
468812
1252468812

so output will like this
output text
1:252468812
2:52468812
4:1252468812
6:468812
7:1252468812
output get the last number not grep number 812 and 1,2,4,6,7 is a line number of input file
thx for advice
# 2  
Old 11-28-2011
This is one way (assuming your data is in the first field of each record):

Code:
awk ' match( $1, "812$" ) { printf( "%d: %s\n", NR, $1 );   } '

If you need to pick from different columns:

Code:
awk -v col=1 ' match( $(col), "812$" ) { printf( "%d: %s\n", NR, $(col) );   } '

Replace the 1 in col=1 with the column number you want.
# 3  
Old 11-28-2011
Another awk solution:

Code:
awk '/812$/ { print NR":"$0 } ' infile

# 4  
Old 11-28-2011
With grep...
Code:
$ cat input.txt
252468812
52468812
1281268819
1252468812
1252468923
468812
1252468812

$ grep -n '812$' input.txt
1:252468812
2:52468812
4:1252468812
6:468812
7:1252468812

$

# 5  
Old 11-28-2011
thx @agama for your solve
how about if i have 2 printf with delimiter like this

input.txt
Code:
252468812|23133 
52468812|231233 
281268819|98812 
1252468812|00000
1252468923|4567 
468812|21884
12031|9856565
1252468812|23123

output.txt
Code:
1:252468812|23133 
2:52468812|231233
3:1281268819|98812 
4:1252468812|00000 
6:468812|21884 
8:1252468812|23123

thx for your advice
# 6  
Old 11-28-2011
This should work:

Code:
awk ' match( $1, "812|" ) { printf( "%d: %s\n", NR, $1 );   } ' input-file

Replaced the $ in the match which will match the 812 followed by the pipe symbol.

You could also use the same code with and supply a different field seperator:

Code:
awk -F \| ' match( $1, "812$" ) { printf( "%d: %s\n", NR, $1 );   } ' input-file

This User Gave Thanks to agama For This Post:
# 7  
Old 11-29-2011
Quote:
Originally Posted by agama
This should work:

Code:
awk ' match( $1, "812|" ) { printf( "%d: %s\n", NR, $1 );   } ' input-file

Replaced the $ in the match which will match the 812 followed by the pipe symbol.

You could also use the same code with and supply a different field seperator:

Code:
awk -F \| ' match( $1, "812$" ) { printf( "%d: %s\n", NR, $1 );   } ' input-file

yes this solve how about if like this
Code:
281268819|98812

812$ have in $2 not $1
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Filtering text with awk

I need to filter a file that is composed like that: >Cluster 0 0 292nt, >last294258;size=1;... * >Cluster 1 0 292nt, >last111510;size=1;... * 1 290nt, >last136280;size=1;... at -/98.62% 2 292nt, >last217336;size=1;... at +/99.66% 3 292nt, >last280937;size=1;... at -/99.32% >Cluster 2... (6 Replies)
Discussion started by: pedro88
6 Replies

2. Shell Programming and Scripting

awk using sub , filtering textfile

i have text file as below CMF_COMP_ELEM_ GSM2_B71.WORLD_20121114130908.log 107496444 rows inserted into ALL_S1_CMF_COMP_ELEM. CMF_COMP_ELEM_ GSM3_B71.WORLD_20121114130908.log 110729006 rows inserted into ALL_S1_CMF_COMP_ELEM. CMF_COMP_ELEM_ GSM4_B71.WORLD_20121114130908.log 92549475... (8 Replies)
Discussion started by: only4satish
8 Replies

3. Shell Programming and Scripting

awk data filtering

I am trying to filter out some data with awk. If someone could help me that would be great. Below is my input file. Date: 10-JUN-12 12:00:00 B 0: 00 00 00 00 10 00 16 28 B 120: 00 00 00 39 53 32 86 29 Date: 10-JUN-12 12:00:10 B 0: 00 00 00 00 10 01 11 22 B 120: 00 00 00 29 23 32 16 29... (5 Replies)
Discussion started by: thibodc
5 Replies

4. UNIX for Dummies Questions & Answers

Grep filtering issue

Hi, I am on uname -a HP-UX mymachine B.11.31 U ia64 3223107173 unlimited-user license ps -exx| grep java | grep -i "GrafiteEventsInterfaces*" 19955 ? 55:22 /opt/app/app1/jdk150_07/bin/IA64N/java -server -Xms1024m -Xmx1024m -XX:PermSize=256m -XX:MaxPermSize=512m... (2 Replies)
Discussion started by: mohtashims
2 Replies

5. Shell Programming and Scripting

filtering a numeric value which has '%' using awk

Hello Gurus, I have a requirement where I have to filter a value from some field which has 99% or greater than '99%'.. For ex: The Date (file -- sample.csv) will be like below Field1,Field2,Field3,Field4 860440512,844284992,16155520,99% 860440512,844284992,16155520,94%... (4 Replies)
Discussion started by: raghu.iv85
4 Replies

6. Shell Programming and Scripting

filtering with awk

i have question about awk ex: input.txt 1252468812,yahoo,3.5 1252468812,hotmail,2.4 1252468819,yahoo,1.2 msn,1252468812,8.9 1252468923,gmail,12 live,1252468812,3.4 yahoo,1252468812,9.0 1252468929,msn,1.2 output.txt 1252468812,yahoo,3.5 1252468812,hotmail,2.4 msn,1252468812,8.9... (3 Replies)
Discussion started by: zvtral
3 Replies

7. Shell Programming and Scripting

Filtering out text with awk

(0 Replies)
Discussion started by: nilekyle
0 Replies

8. Shell Programming and Scripting

Help with filtering trace file through grep

Hi, I am using NS2 and i need to filter a trace file (part of which is shown below): - 33.91576 2 3 tcp 1040 ------- 1 0.0 4.0 1115 2258 r 33.918907 4 3 ack 40 ------- 1 4.0 0.0 1107 2272 + 33.918907 3 2 ack 40 ------- 1 4.0 0.0 1107 2272 - 33.918907 3 2 ... (5 Replies)
Discussion started by: saqibshah
5 Replies

9. Shell Programming and Scripting

grep filtering problem

I'm working on writing a shell script that logs out whenever I have a firefox process running during certain times of the day (1 am - 8 am). I'll put it in crontab when it runs properly unless someone knows of a different and better way to do this. Here it is so far. if ps ax | grep... (4 Replies)
Discussion started by: cokedude
4 Replies

10. UNIX for Advanced & Expert Users

awk filtering ?

I have a Ques. Regarding awk I have few strings in a file, like.. ABC DEF_ABC GHI_ABC GHI Now I want string which has only 'ABC', not the part of any other string as it is also present in 'DEF_ABC' Output should be ABC Please guide me asap !! Thanks :b: (4 Replies)
Discussion started by: varungupta
4 Replies
Login or Register to Ask a Question