awk Loop 4 digit fields


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk Loop 4 digit fields
# 1  
Old 11-23-2011
awk Loop 4 digit fields

Can somebody help me on this script with a loop?

Code:
gawk --re-interval '{if(($1 ~ /[[:digit:]]{4}/) && ($2 ~ /[[:digit:]]{4}/) && ($3 ~ /[[:digit:]]{4}/)) print $0}'

I would like to loop until NF and while $i does not match ($i ~ /[[:digit:]]{4}/) anymore then print all matching $i and exit.

Last edited by sdf; 11-23-2011 at 06:24 PM..
# 2  
Old 11-23-2011
Quote:
Originally Posted by sdf
I would like to loop until NF and while $i does not match ($i ~ /[[:digit:]]{4}/) anymore then print all matching $i and exit.
Code:
gawk '{for(;i++<NF;) {if($i ~ /[[:digit:]]{4}/)print $i};exit}' file

Is that what you want?
# 3  
Old 11-24-2011
Quote:
Originally Posted by danmero
Code:
gawk '{for(;i++<NF;) {if($i ~ /[[:digit:]]{4}/)print $i};exit}' file

Is that what you want?
Thanks, though need all fields of $0 adjacent. Can somebody fix this?

Code:
'{for(;i++<NF;) {if($i ~ /[[:digit:]]{4}/) s= s+$i; print s}}'

# 4  
Old 11-24-2011
Code:
awk '{for(;i++<NF;) {if($i ~ /[[:digit:]]{4}/) {s=(s?s FS:x)$i}};print s;exit}'

# 5  
Old 11-24-2011
Quote:
Originally Posted by danmero
Code:
awk '{for(;i++<NF;) {if($i ~ /[[:digit:]]{4}/) {s=(s?s FS:x)$i}};print s;exit}'

ThanksSmilie, can you pls explain what this does?

Code:
{s=(s?s FS:x)$i}

# 6  
Old 11-24-2011
if s is not empty then s=s FS $i
if s is empty then s=x $i
Code:
if(s){
  s=s FS $i
} else {
  s=x $i
}

HTH
--ahamed
This User Gave Thanks to ahamed101 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk sort based on difference of fields and print all fields

Hi I have a file as below <field1> <field2> <field3> ... <field_num1> <field_num2> Trying to sort based on difference of <field_num1> and <field_num2> in desceding order and print all fields. I tried this and it doesn't sort on the difference field .. Appreciate your help. cat... (9 Replies)
Discussion started by: newstart
9 Replies

2. Shell Programming and Scripting

awk - compare 1st 15 fields of record with 20 fields

I'm trying to compare 2 files for differences in a selct number of fields. When differnces are found it will write the whole record of the second file including appending '|C' out to a delta file. Each record will have 20 fields, but only want to do comparison of 1st 15 fields. The 1st field of... (7 Replies)
Discussion started by: sljnk
7 Replies

3. Shell Programming and Scripting

How to print 1st field and last 2 fields together and the rest of the fields after it using awk?

Hi experts, I need to print the first field first then last two fields should come next and then i need to print rest of the fields. Input : a1,abc,jsd,fhf,fkk,b1,b2 a2,acb,dfg,ghj,b3,c4 a3,djf,wdjg,fkg,dff,ggk,d4,d5 Expected output: a1,b1,b2,abc,jsd,fhf,fkk... (6 Replies)
Discussion started by: 100bees
6 Replies

4. Shell Programming and Scripting

Join fields comparing 4 fields using awk

Hi All, I am looking for an awk script to do the following Join the fields together only if the first 4 fields are same. Can it be done with join function in awk?? a,b,c,d,8,,, a,b,c,d,,7,, a,b,c,d,,,9, a,b,p,e,8,,, a.b,p,e,,9,, a,b,p,z,,,,9 a,b,p,z,,8,, desired output: ... (1 Reply)
Discussion started by: aksijain
1 Replies

5. Shell Programming and Scripting

Problem with awk double digit fields

Hello Everyone, This 10th field is giving too much problem in AWK. How do i overcome ? pls suggest Thanks! cat /tmp/data_rank| awk 'BEGIN{ print "HT No" "," "Name" "," "Maths" "," "Physics" "," "Chemistry" "," "Total" "," "Gender" "," "University" "," "RANK Global" "," "Rank... (2 Replies)
Discussion started by: InduInduIndu
2 Replies

6. Shell Programming and Scripting

awk length of digit and print at most right digit

Have columns with digits and strings like: input.txt 3840 3841 3842 Dav Thun Tax Cahn 146; Dav. 3855 3853 3861 3862 Dav Thun Tax 2780 Karl VI., 3873 3872 3872 Dav Thun Tax 3894 3893 3897 3899 Dav Thun Tax 403; Thun 282. 3958 3959 3960 Dav Thun Tax 3972 3972 3972 3975 Dav Thun Tax... (8 Replies)
Discussion started by: sdf
8 Replies

7. Shell Programming and Scripting

Awk numeric range match only one digit?

Hello, I have a text file with lines that look like this: 1974 12 27 -0.72743 -1.0169 2 1.25029 1974 12 28 -0.4958 -0.72926 2 0.881839 1974 12 29 -0.26331 -0.53426 2 0.595623 1974 12 30 7.71432E-02 -0.71887 3 0.723001 1974 12 31 0.187789 -1.07114 3 1.08748 1975 1 1 0.349933 -1.02217... (2 Replies)
Discussion started by: meridionaljet
2 Replies

8. Shell Programming and Scripting

awk sed cut? to rearrange random number of fields into 3 fields

I'm working on formatting some attendance data to meet a vendors requirements to upload to their system. With some help on the forums here, I have the data close. But they've since changed what they want. The vendor wants me to submit three fields to them. Field 1 is the studentid field,... (4 Replies)
Discussion started by: axo959
4 Replies

9. Shell Programming and Scripting

using awk make an field as 5 digit and display

using awk convert 3 rd fileld of file as 5 digit and then display changed file. like 1 2 23445 3452 3343 3 5 6 6 ================ o/p:- 1 2 23445 3452 03343 3 5 00006 6 (1 Reply)
Discussion started by: RahulJoshi
1 Replies

10. Shell Programming and Scripting

AWK create loop for fields

Hi everybody: I just create awk script. This script calculate the average of a field, but i want to do it for every fields. The original field has 40 fields of numbers like that: 38.00 51.00 10.00 -99.90 75.00 47.00 4.00 -99.90 69.00 121.00 62.00 6.00 70.00 43.00 36.00 49.00 8.00 36.00 50.00... (1 Reply)
Discussion started by: tonet
1 Replies
Login or Register to Ask a Question