awk command for finding field in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk command for finding field in a file
# 1  
Old 11-23-2008
awk command for finding field in a file

hi guys i have this file with column number 7 as below:
0
1416
49
37
5
3
2
0
0
0
21
0
26
6
13
0
12
0
55
1
1
1
0
0
2
0
0
0
Device
when i select $7 i get all the values including "Device".
how do i extract only the numbers from the first 0 to the last 0 in awk?
thanks
# 2  
Old 11-23-2008
Assuming you want to print only the numeric fields until the first non-numeric field of your example:

Code:
awk 'int($7)==0 && $7!="0"{exit}{print $7}' file

# 3  
Old 11-23-2008
thanks that helped..

now i have this file which has the following as the last three fields..
the first fields have variable fields including spaces..

0 0.00 5
1416 85.82 1200
49 2.97 36
37 2.24 44
5 0.30 7
3 0.18 10
2 0.12 5
0 0.00 3
0 0.00 4
0 0.00 3
21 1.27 9
0 0.00 3
26 1.58 40
6 0.36 4
13 0.79 15
0 0.00 3
12 0.73 12
0 0.00 3
55 3.33 23
1 0.06 3
1 0.06 6
1 0.06 3
0 0.00 3
0 0.00 20
2 0.12 51
0 0.00 77
0 0.00 3
0 0.00 10

i want to print the column with 0,1416,49,...

i prefer to start from the last column..i know we can use $NF for last.. is there any way to use NF-3 to print the column containing 0,1416,49,...

thanks again..
# 4  
Old 11-23-2008
Quote:
Originally Posted by npatwardhan
i want to print the column with 0,1416,49,...

i prefer to start from the last column..i know we can use $NF for last.. is there any way to use NF-3 to print the column containing 0,1416,49,...

thanks again..
Have you try it out for yourself?

Regards
# 5  
Old 11-23-2008
ok i got it..
i am using the following:

awk '{i=NF-1;printf "\n%d", $i;}' try2 > try3

ok one last question. i have this one file and i am formatting (removing lines, extracting columns etc) right now i am creating a new file for every formatting.i have five things to extract so i generate 5 files.it is really inefficient. is there an easier way?
# 6  
Old 11-24-2008
Perhaps you can do the job with one awk command instead of using temporary files and more awk commands.
I should start a new thread. Post the original file and the desired output within code tags.

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk command to return only field with a number in it

What is an awk command to print only fields with a number in it?? Input file....... S,S,S,S,S,S,S,S,S 001S,S,S,S,S,S,S,S,S 00219S,23S,24S,43S,47S,S,S,S,S 00319S,10S,23S,41S,43S,47S,S,S,S 00423S,41S,43S,46S,47S,S,S,S,S 00510S,23S,24S,43S,46S,S,S,S,S 00610S,23S,43S,46S,47S,S,S,S,S... (2 Replies)
Discussion started by: garethsays
2 Replies

2. Shell Programming and Scripting

Command/script to match a field and print the next field of each line in a file.

Hello, I have a text file in the below format: Source Destination State Lag Status CQA02W2K12pl:D:\CAQA ... (10 Replies)
Discussion started by: pocodot
10 Replies

3. Shell Programming and Scripting

Insert field values in a record using awk command

Hi Friends, Below is my input file with "|" (pipe) as filed delimiter: My Input File: HDR|F1|F2||||F6|F7 I want to inser values in the record for field 4 and field 5. Expected output HDR|F1|F2||F4|F5|F6|F7 I am able to append the string to the end of the record, but not in between the... (3 Replies)
Discussion started by: Ajay Venkatesan
3 Replies

4. UNIX for Dummies Questions & Answers

awk command to copy field to bottom records

Hi All, I have a below input, I want to copy the Job_name and Created_by field information to other bottom records as shown in below Output Job_name Created_by Modified_on Modified_by CGI_ACLMIB n38504 2014-05-07 20:40:48 n38504 2014-05-07 20:40:57 n38504 2014-05-08 20:40:57 n48504... (1 Reply)
Discussion started by: somu_june
1 Replies

5. Shell Programming and Scripting

Pass awk field to a command line executed within awk

Hi, I am trying to pass awk field to a command line executed within awk (need to convert a timestamp into formatted date). All my attempts failed this far. Here's an example. It works fine with timestamp hard-codded into the command echo "1381653229 something" |awk 'BEGIN{cmd="date -d... (4 Replies)
Discussion started by: tuxer
4 Replies

6. Shell Programming and Scripting

Finding the displacement of a particular field in a file

Hi I have a file named Employee and it has the following format: File name:Employee 00000 0000:0000 YYNNN 20120302 NAME:010:C ACCOUNT NUM:015,00:N DATE:008,00:D #DATA# ROGER 00000002346123720120302 MAX 00000003654774020110612 JOHNSON 00000000000103420120713 #END#... (13 Replies)
Discussion started by: bobby1015
13 Replies

7. Shell Programming and Scripting

Help with Awk finding and replacing a field based on a condition

Hi everybody, I'm trying to replace the $98 field with "T" if the last field (108th) is T I've tried awk 'BEGIN{OFS=FS="|"} {if ($108=="T")sub($98,"T"); print}' test.txt but that doesn't do anything also tried awk 'BEGIN{OFS=FS="|"}{ /*T.$/ sub($98,"T")} { print}' test.txt but... (2 Replies)
Discussion started by: jghi123
2 Replies

8. UNIX for Advanced & Expert Users

Awk command to print the field

894344202808090;11122;040320 075858 166;101;0;0;10u;0;NA;65;221890;2;101973;185059;568674;Y; PRE;0;0;NA;NA;0;NA;0;NA;textmsg;textmsg_snd1;telusmob;TEXTMSG1;0.15000000;126037;2010/03/04 12:58:57gmt;0;70532192; plz tell me any awk command which on the basis of the yellow field which is... (1 Reply)
Discussion started by: madfox
1 Replies

9. Shell Programming and Scripting

awk command to separate a field

I have a log file that I am trying to convert. File contents something like this: aaaaa bbbbbb cccc dddddd\123 eeeee ffffffff I am trying to output the fields in a different order and separate field 4 so that the "123" and "dddddd" can be output separately. for example bbbbbb aaaaa 123... (5 Replies)
Discussion started by: jake1988
5 Replies

10. Shell Programming and Scripting

awk field separator or print command

Hello Experts, I am back, with another doubt. I am not sure what it relates to this time - awk or the print command actually. I'll explain the scenario: I have a huge file, and it has some traces(logs). In between those logs, there are statements with some SQL queries. All I want to do is... (4 Replies)
Discussion started by: hkansal
4 Replies
Login or Register to Ask a Question