Putting values into order in a field using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Putting values into order in a field using awk
# 1  
Old 10-24-2012
Putting values into order in a field using awk

Hi,

I am using UBUNTU 12.04.

I have a dataset as follows:

Code:
Column#1 Column#2 Column#3 .... Column#50
                     1             154878 
                     1             145145
                     2             189565 
                     2             454121
                     .                     .
                     .                     .
                     .                    .
                   22              124579

My questions:
1. I want to divide all the values in column#3 by 1000000 and print all the file. I know doing it by writing #awk'{print $1,$2,$3/1000000,$4....$50}' but as it takes too much time, I wanted to see whether there is any more efficient way?

2. I want to order all the values in column#3 according to column#2, that is first it puts all the values in order for all 1s in column 2, then do it for all the 2s and so on. So, the output looks like this:
Code:
Column#1 Column#2 Column#3 .... Column#50
                     1             145145
                     1             154878 
                     2             145412
                     2             189565 
                                 
                     .                     .
                     .                     .
                     .                    .
                   22              124579

Thank you very much.

Last edited by radoulov; 10-25-2012 at 04:54 AM..
# 2  
Old 10-24-2012
try:
Code:
awk '{$3/=1000000; print}' infile | sort -n


Last edited by rdrtx1; 10-24-2012 at 02:06 PM..
# 3  
Old 10-24-2012
OR

Code:
awk '{$3/=1000000}1' infile | sort -n

# 4  
Old 10-25-2012
Thank you, although it doesn't sort the third column.

Using this command, how I can keep the spacing between my fields? I have used this command but it doesn' work properly.

Code:
#awk 'BEGIN{OFS=ORS=""}{$3/=1000000 print\n"}' infile


Moderator's Comments:
Mod Comment Please use code tags next time for your code and data.
# 5  
Old 10-25-2012
Quote:
Originally Posted by Homa
Thank you, although it doesn't sort the third column.
use this

Code:
awk '{$3/=1000000}1' infile | sort -nk3

Quote:
Originally Posted by Homa
Using this command, how I can keep the spacing between my fields? I have used this command but it doesn' work properly.

Code:
#awk 'BEGIN{OFS=ORS=""}{$3/=1000000 print\n"}' infile

OFS="" Thats why it is not showing any spacing. Use OFS=" " or by default it is space so no need to define OFS.
And after \n you need " there.
# 6  
Old 10-25-2012
Works perfectly, thanks
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 GSUB read field values from multiple text files

My program run without error. The problem I am having. The program isn't outputting field values with the column headers to file.txt. Each of the column headers in file.txt has no data. MEMSIZE SECOND SASFoundation Filename The output results in file.txt should show: ... (1 Reply)
Discussion started by: dellanicholson
1 Replies

2. 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

3. UNIX for Dummies Questions & Answers

Generating all possible combinations of values in field 1 (awk)

Input: A|1 B|2 C|3 D|4 Output: A+B|3 A+C|4 A+D|5 B+C|5 B+D|6 C+D|7 A+B+C|6 A+B+D|7 A+C+D|8 B+C+D|9 A+B+C+D|10 I only managed to get the output for pairs of $1 values (i.e. combination of length 2): (4 Replies)
Discussion started by: beca123456
4 Replies

4. UNIX for Dummies Questions & Answers

Values with common field in same line with awk

Hi all ! I almost did it but got a small problem. input: cars red cars blue cars green truck black Wanted: cars red-blue-green truck black Attempt: gawk 'BEGIN{FS="\t"}{a = a (a?"-":"")$2; $2=a; print $1 FS $2}' input But I also got the intermediate records... (2 Replies)
Discussion started by: beca123456
2 Replies

5. Shell Programming and Scripting

awk help - matching a field with certail values

Hello there, I have a file with few fields separated by ":". I wrote a below awk to manipulate this file: awk 'BEGIN { FS=OFS=":" }\ NR != 1 && $2 !~ /^98/ && $8 !~ /^6/{print $0}' $in_file > $out_file What I wanted was that if $8 field contains any of the values - 6100, 6110, 6200 -... (2 Replies)
Discussion started by: juzz4fun
2 Replies

6. Shell Programming and Scripting

SED/AWK to edit/add field values in a record

Hi Experts, I am new to shell scripting. Need some help in doing one task given by the customer. The sample record in a file is as follows: 3538,,,,,,ID,ID1,,,,,,,,,,, It needs to be the following: 3538,,353800,353800,,,ID,ID1,,,,,COLX,,,,,COLY, And i want to modify this record in... (3 Replies)
Discussion started by: sugarcane
3 Replies

7. Shell Programming and Scripting

AWK: combining consecutive values in a field

Hi, Here is my sample input X 2 AAA Y 3 BBB Y 2 CCC Z 4 DDD In field 1, if the value of one line is same as that of next line, I want to concatenate the corresponding value of the second line in the third field with the value of the third field of first line. And I dont need the third... (2 Replies)
Discussion started by: polsum
2 Replies

8. Shell Programming and Scripting

counting lines containing two column field values with awk

Hello everybody, I'm trying to count the number of consecutive lines in a text file which have two distinctive column field values. These lines may appear in several line blocks within the file, but I only want a single block to be counted. This was my first approach to tackle the problem (I'm... (6 Replies)
Discussion started by: origamisven
6 Replies

9. UNIX for Dummies Questions & Answers

Awk search for max and min field values

hi, i have an awk script and I managed to figure out how to search the max value but Im having difficulty in searching for the min field value. BEGIN {FS=","; max=0} NF == 7 {if (max < $6) max = $6;} END { print man, min} where $6 is the column of a field separated by a comma (3 Replies)
Discussion started by: Kirichiko
3 Replies

10. Shell Programming and Scripting

AWK - printing certain fields when field order changes in data file

I'm hoping someone can help me on this. I have a data file that greatly simplified might look like this: sec;src;dst;proto 421;10.10.10.1;10.10.10.2;tcp 426;10.10.10.3;10.10.10.4;udp 442;10.10.10.5;10.10.10.6;tcp sec;src;fac;dst;proto 521;10.10.10.1;ab;10.10.10.2;tcp... (3 Replies)
Discussion started by: eric4
3 Replies
Login or Register to Ask a Question