AWK series of numbers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK series of numbers
# 1  
Old 01-25-2012
AWK series of numbers

Hi,

I have a dataset say

Code:
1 2 3 4
5 5 6 7
6 7 8 9

I was wondering if there is a way to add another column with the following style...

Code:
1 2 3 4 xyz_1
5 5 6 7 xyz_2
6 7 8 9 xyz_3

It would be greatly appreciated if I can have an option of specifying what to write instead of xyz, range of values followed by xyz_ and the column.

The options for the above example would be

Text: xyz
range: 1-3
column: 5

Last edited by joeyg; 01-25-2012 at 04:04 PM.. Reason: Please use CodeTags for sample text and commands
# 2  
Old 01-25-2012
Code:
awk '{ $(NF+1)="xyz_" ((NR%3)+1) } 1' file

# 3  
Old 01-25-2012
Hi Corona,

Thanks a lot.

But I have more than 10000 rows and the one you gave me prints xyz_1, xyz_2 and xyz_3 repeatedly. I would like to have a series of numbers.

Thanks again.
# 4  
Old 01-25-2012
You told me range 1-3, and I took that literally. What you want is actually much easier Smilie

Code:
awk '{ $(NF+1)="xyz_" NR } 1' file

# 5  
Old 01-25-2012
You the best.

Can you please explain it?

Thanks
# 6  
Old 01-25-2012
NF is a special variable meaning 'number of fields'. $ is an operator that turns a number into a field. So $NF would be the last field.

$(NF+1) would be the field after the last field, then. So we're adding a new field.

The field is "xyz_", followed by the special variable NR, "number of records", generally the same thing as lines. Putting two things in a row concatenates them.

The '1' on the end is an expression which makes it print every line. It could be a regex if you only wanted it to print lines containing a certain string, or a comparision involving variables if you wanted to do more complicated logic based on things set previously.

The whole thing is done repeatedly, once per line, since there's no condition before the code block. If you did NR==1 { ... } 1 then it would only run the code block for the very first line ( though it would still print every line).
# 7  
Old 01-26-2012
Code:
$ nawk '$NF=$NF" xyz_"NR' test.txt
1 2 3 4 xyz_1
5 5 6 7 xyz_2
6 7 8 9 xyz_3

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Moving decimal point in a series of numbers

Hi, i need to move the decimal point from a file listing some numbers like this : 49899.50 49914.55 49894.48 49939.65 49879.44 49919.57 49934.62 49944.67 49954.72 (1 Reply)
Discussion started by: Board27
1 Replies

2. Programming

Resample time series data with replacement any way to do this in awk or just bash script

I have some time series data that I need to resample or downsample at some specific time intervals. The firs column is time in decimal hours. I am tryiong to resample this data every 3 minutse. So I need a data value ever 0.05. Here is the example data and as you can see, there time slot for 0.1500... (3 Replies)
Discussion started by: malandisa
3 Replies

3. Shell Programming and Scripting

Help with Using "while" loop to output series of numbers

Trying to use "while" loop command to create a series of numbers that looks like the following: 0 . 1 0 . 2 1 0 . 3 2 1 0 . 4 3 2 1 0 . 5 4 3 2 1 0 . 6 5 4 3 2 1 0 . 7 6 5 4 3 2 1 0 . 8 7 6 5 4 3 2 1 0 . 9 8 7 6 5 4 3 2 1 0 . I am very new to shell scripting and any help would be... (7 Replies)
Discussion started by: tarlz
7 Replies

4. Shell Programming and Scripting

awk - extracting data from a series of files

Hi, I am trying to extract data from multiple output files. I am able to extract the data from a single output file by using the following awk commands: awk '/ test-file*/{print;m=0}' out1.log > out1a.txt awk '/ test-string/{m=1;c=0}m&&++c==3{print $2 " " $3 " " $4 ;m=0}' out1.log >... (12 Replies)
Discussion started by: p_sun
12 Replies

5. UNIX for Dummies Questions & Answers

To find missing numbers from a number series

Hi, My requirement is I have an input file with a continuous series from 10000 to 99999. I have some numbers missing from those series. I want a output file which produces those missing numbers. Eg: 10002, 99999 are missing from the series then the output file should contain those... (4 Replies)
Discussion started by: rakeshbharadwaj
4 Replies

6. Homework & Coursework Questions

Help with shell script to find sum of first n numbers of Fibonacci series

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Shell script to find sum of first n numbers of Fibonacci series 2. Relevant commands, code, scripts,... (0 Replies)
Discussion started by: Kshitija
0 Replies

7. Shell Programming and Scripting

How to code a series of numbers in SH

Hi. How can I write this in a shorter way? for NUMs in 1 2 3 4 5 6 I looked at the"seq" command, but I do not think that is what I want. Can I code for NUMs (1..7) Hey!! Is that it? (8 Replies)
Discussion started by: Ccccc
8 Replies

8. Shell Programming and Scripting

How to sort out the latest one from a series of numbers?

Hi, I have a directory which contains a number of sub directories. They are named as 1.0.0, 1.0.1, 1.0.2...1.1.0..1.1.1...1.2.0..and so on.. Basically these are the tags created at the time of release. Tags are named as major.minor.buildnumber format for modules. Now I have to search the... (2 Replies)
Discussion started by: bhaskar_m
2 Replies

9. Shell Programming and Scripting

calculate the average of time series data using AWK

Hi, I have two time series data (below) merged into a file. t1 and t2 are in unit of second I want to calculate the average of V1 every second and count how many times "1" in V2 is occur within a second Input File: t1 V1 t2 V2 10.000000... (5 Replies)
Discussion started by: nica
5 Replies

10. Shell Programming and Scripting

How to generate a series of numbers

Hi All, I have a requirement where in I have an input as follows:- input=1-4,6,8-10,12-15 I need to explode this range into an output file as follows:- 1 2 3 4 6 8 9 10 12 13 14 15 My input may vary like 1,5-9,11-13,15-17....... (3 Replies)
Discussion started by: rony_daniel
3 Replies
Login or Register to Ask a Question