How to Use AWK to Generate a Series of Numbers in a Shell Script

  • Thread starter Thread starter confi999
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around generating a series of numbers in a shell script using AWK or alternative methods. Participants explore how to modify a script to produce a specific output format with sequential numbers in the last column.

Discussion Character

  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant requests assistance in generating a series of numbers from 1 to 500 in a specific format using AWK.
  • Another participant suggests using the AWK command `awk '{ print $0, ++x; }'` to append numbers to the end of each line.
  • A participant expresses the desire to create a complete script that generates all 500 lines with the specified format.
  • A shell script is provided by a participant that uses a loop to echo the desired output format for numbers 1 through 500.

Areas of Agreement / Disagreement

There is no explicit disagreement noted, as participants build upon each other's suggestions and provide solutions without contesting the validity of the approaches presented.

Contextual Notes

Participants do not clarify the specific requirements or constraints of the script beyond the output format, leaving some assumptions about the context of use unaddressed.

Who May Find This Useful

This discussion may be useful for individuals looking to automate the generation of formatted output in shell scripts, particularly those interested in using AWK or shell scripting techniques.

confi999
Messages
18
Reaction score
0
Hi,
I need to modify a shell script and add the following into it:
location: 200 200 1
location: 200 200 2
location: 200 200 3
---------
---------
---------
location: 200 200 449
location: 200 200 500

I thought using some awk command one could produce the series 1,2,... 500 in the last column of all the 500 lines. Can anyone help me with that command.
Thank you very much.
 
Physics news on Phys.org
confi999 said:
Hi,
I need to modify a shell script and add the following into it:
location: 200 200 1
location: 200 200 2
location: 200 200 3
---------
---------
---------
location: 200 200 449
location: 200 200 500

I thought using some awk command one could produce the series 1,2,... 500 in the last column of all the 500 lines. Can anyone help me with that command.
Thank you very much.

if you just want a filter to add numbers to the end of each line...

awk '{ print $0, ++x; }'
 
sylas said:
if you just want a filter to add numbers to the end of each line...

awk '{ print $0, ++x; }'

Hi,
Thanks!
I want to write a small script or an awk command (or if any other way) that will produce a file which has all those 500 lines in it.
 
confi999 said:
Hi,
Thanks!
I want to write a small script or an awk command (or if any other way) that will produce a file which has all those 500 lines in it.

Here's a shell script

Code:
#!/bin/sh
for (( n=1 ; n <= 500 ; n++ )) do
    echo location: 200 200 $n
done
 
sylas said:
Here's a shell script

Code:
#!/bin/sh
for (( n=1 ; n <= 500 ; n++ )) do
    echo location: 200 200 $n
done

Thank you sooo much.
 

Similar threads

Replies
3
Views
3K
Replies
31
Views
4K
  • · Replies 96 ·
4
Replies
96
Views
12K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 31 ·
2
Replies
31
Views
5K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
5K
Replies
5
Views
3K