Simple awk script needed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Simple awk script needed
# 1  
Old 12-29-2012
Simple awk script needed

but I'm stumped...please help

I have a file like this.......

Code:
1000   1    34
1000   10   34
1000   11   35
1000   20   35
1000   21   36
1000   30   36
2000    1    34
2000    10   34

which I would like printed out as 40 lines

Code:
1000  1   34
1000  2   34
1000  3   34
1000  4   34
1000  5   34
1000  6   34
1000  7   34
1000  8   34
1000  9   34
1000  10   34
1000  11   35
1000  12   35
1000  13   35
1000  14   35
1000  15   35
1000  16   35
1000  17   35
1000  18   35
1000  19   35
1000  20   35
1000  21   36
1000  22   36
1000  23   36
1000  24   36
1000  25   36
1000  26   36
1000  27   36
1000  28   36
1000  29   36
1000  30   36
2000  1   34
2000  2   34
2000  3   34
2000  4   34
2000  5   34
2000  6   34
2000  7   34
2000  8   34
2000  9   34
2000  10   34

---------- Post updated at 01:22 PM ---------- Previous update was at 01:15 PM ----------

Actually the input file is

Code:
1000 1 
1000 10 
1000 11 
1000 20
1000 21
1000 30 
2000 1 
2000 10

I would like to set $3 based on when the range was read, i.e. between $2 on line 1 and $2 on line2, $3 should be 34, between $2 on line 3 and $2 on line 4, $3 should be 35 etc

Last edited by jim mcnamara; 12-29-2012 at 10:30 AM..
# 2  
Old 12-29-2012
try:
Code:
awk '{getline x; $0=$0" "x; if (!a[$1]++) {e=s} else {e++}; for (i=$2; i<=$4; i++) print $1, i, e}' s=34 infile

# 3  
Old 12-29-2012
I took your example file:
Code:
1000 1 
1000 10 
1000 11 
1000 20
1000 21
1000 30 
2000 1 
2000 10

named it as unsorted.txt and ran the following statement...
Code:
sort -k 1 -k 2 -n unsorted.txt | awk ' { print $1 " " $2 " " 34+($2 - ($2 % 10))/10}'

and got this output...
Code:
1000 1 34
1000 10 35
1000 11 35
1000 20 36
1000 21 36
1000 30 37
2000 1 34
2000 10 35

I beleive that satisfies your requirement and uses awk?

---------- Post updated at 11:27 AM ---------- Previous update was at 11:13 AM ----------

Quote:
Originally Posted by rdrtx1
try:
Code:
awk '{getline x; $0=$0" "x; if (!a[$1]++) {e=s} else {e++}; for (i=$2; i<=$4; i++) print $1, i, e}' s=34 infile

This prints out 40 lines...which mine response failed to do Smilie.
I won't pretend I understand what it's doing. However I noted for cases where $2 is a multiple of 10 this produces the incorrect $3.

Code:
1000 10 34
1000 30 36

I wonder if you/he could combine the modulus method in my above response with this printing method?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Urgent simple script needed

Good Day All, I need your urgent support to get the following script (bash), (it should be as simple as possible please) : The script will check the .csv file( attached example) The script will generated 3 files from the initial one. The script must check one field “NEType”, and then ... (2 Replies)
Discussion started by: engkemo2002
2 Replies

2. Shell Programming and Scripting

help with simple awk script

Hi, I just don't understand awk. I think I'm close here but can someone give me a hand to finish this little parsing routine ? the input file is formatted like this: District 2502110 Gsub 2384889 Gsub 1428180 District 2502220 Gsub 1466390 Gsub 1466389 Gsub 1466388 Gsub 1466386 Gsub... (4 Replies)
Discussion started by: fwellers
4 Replies

3. Shell Programming and Scripting

Simple Script needed for Processing CSV file perl

I am new to perl, and need a script to pull a CSV file, strip out 2 leading columns, and 2 ending columns, and resave the file in a new location. The file is basic and has less than 10,000 records. Secondly, can I schedule perl scripts to run daily? Can someone provide the basic script to... (1 Reply)
Discussion started by: cobbjob
1 Replies

4. Shell Programming and Scripting

Simple AWK script problem.

Hi all, I have set up a simple awk script to calculate the average of values that are printed out a number of times per second (the number of time the printing occurs varies). The data is of the format shown below: 1 4.43 1 3.65 1 2.45 2 7.65 2 8.23 2 5.65 3 4.65 3 6.21 .. .. 120... (4 Replies)
Discussion started by: omnomtac
4 Replies

5. Shell Programming and Scripting

script help needed --- awk?

hello and thanks in advance. I need to consolidate 2 records of data into 1 record. here is a sample of the input: ------------------------------------------------------------------------------------------ totalcount fred thomas 99999.00 88888:00 total 77777.00... (7 Replies)
Discussion started by: scuderiaf1
7 Replies

6. Shell Programming and Scripting

Help with a simple script using awk

I need a hand with this simple script, in Unix i have a variable called portal: $ echo $portal chu0 when i use awk this variable is not recognized. How can i make awk recognize and print the value of this variable, since the output is not shown i.e. awk ' BEGIN {printf("%4s\t%14s\n",... (3 Replies)
Discussion started by: alexcol
3 Replies

7. Shell Programming and Scripting

need help with simple awk/ksh script

I need help finding out why this script wont run. The chmod is okay, but i get an error saying that I need '&&' on line 5. (18 Replies)
Discussion started by: tefflox
18 Replies

8. Shell Programming and Scripting

simple script help needed

I am relatively new to *nix and have set up a small home network with a red hat server, what i would like to do is write a small script to check to see if a computer is connected by using the ip address of the computer. can get the ping to return but cant get the echo line working. the output i... (3 Replies)
Discussion started by: nobius
3 Replies

9. Shell Programming and Scripting

simple awk script...

hi everyone, i have a script that I use regulary to look through my files and directories it works fine, but I would like to add a segment to the script to make the output more readable and userfriendly.. (i am not an expert on scripts so there is no comments or status exits as of yet.. )... (3 Replies)
Discussion started by: moxxx68
3 Replies

10. Shell Programming and Scripting

Simple awk script question

Hi, I'm a total beginner at awk and hope someone can advise what I have done wrong in the following script: I have a file which (to simplify things) may be something like this Fred Smith and Sue Brown Joe Jones and Jane Watts Sally Green and Jim O? Connor Freda O? Reiley and Pat O?... (2 Replies)
Discussion started by: Bab00shka
2 Replies
Login or Register to Ask a Question