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

  • Thread starter Thread starter confi999
  • Start date Start date
AI Thread Summary
The discussion revolves around modifying a shell script to generate a series of lines formatted as "location: 200 200 X," where X ranges from 1 to 500. The initial request sought assistance with an awk command to append sequential numbers to existing lines. A participant suggested using the awk command 'awk '{ print $0, ++x; }'' for adding numbers to the end of lines. However, another user proposed a simple shell script that utilizes a for loop to create the desired output directly. The script iterates from 1 to 500, echoing the formatted location lines. Overall, the conversation highlights efficient methods for generating sequential data in shell scripting.
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.
 
Back
Top