Pages

Showing posts with label Awk. Show all posts
Showing posts with label Awk. Show all posts

Thursday, April 25, 2013

AWK Scripting: Learn AWK Built-in variables with examples

AWK inbuilt variables: FS, OFS, RS, ORS, NR, NF, FNR, FILENAME


AWK is supplied with good number of built-in variables which come in handy when working with data files. We will see each AWK built-in variables with one or two examples to familiarize with them. Without these built-in variables it’s very much difficult to write simple AWK code. These variable are used to format output of an AWK command, as input field separator and even we can store current input file name in them for using them with in the script. Some of the AWK concepts already covered are.

AWK scripting: What is an AWK and how to use it?

AWK built-in variables:

  • NR: Current count of the number of input records.
  • NF: Keeps a count of the number of fields
  • FILENAME: The name of the current input-file.
  • FNR: No of records in current filename
  • FS: Contains the "field separator" character
  • RS: Stores the current "record separator" or Row Separator.
  • OFS: Stores the "output field separator".
  • ORS: Stores the "output record separator" or Output RS.
Our sample DB file for this post is db.txt

cat db.txt

John,29,MS,IBM,M,Married
Barbi,45,MD,JHH,F,Single
Mitch,33,BS,BofA,M,Single
Tim,39,Phd,DELL,M,Married
Lisa,22,BS,SmartDrive,F,Married

In order to make it simple we can divide above  inbuilt variables in to groups on basis of their operations.

Group1: FS(input field separator), OFS,
Group2: RS(Row separator) and ORS(Output record separator)
Group3: NR, NF and FNR
Group4: FILENAME variable

Group1: FS(input field separator), OFS


Let us start with FS and OFS built-in variables.

FS AWK variable: This variable is useful in storing the input field separator. By default AWK can understand only spaces, tabs as input and output separators. But if your file contains some other character as separator other than these mention one's, AWK cannot understand them. For example Linux password file which contain ‘:’ as a separator. So in order to mention the input filed separator we use this inbuilt variable.

We will see what issue we face if we don’t mention the field separator for our db.txt.

Example1: Print first column data from db.txt file.

awk '{print $1}' db.txt

Output:

John,29,MS,IBM,M,Married
Barbi,45,MD,JHH,F,Single
Mitch,33,BS,BofA,M,Single
Tim,39,Phd,DELL,M,Married
Lisa,22,BS,SmartDrive,F,Married

If you see entire file is displayed which indicates AWK do not understand db.txt file separator ",". We have to tell AWK what is the field separator.

Example2: List only first column data from db.txt file which have field separator as ‘,’.

awk 'BEGIN{FS=","}{print $1}' db.txt\

Output:

John
Barbi
Mitch
Tim
Lisa

Example3: We can use AWK option –F for mentioning input field separator as shown in below example for printing 4th column.

awk -F',' '{print $4}' db.txt

Output:

IBM
JHH
BofA
DELL
SmartDrive

OFS AWK variable: This variable is useful for mentioning what is your output field separator which separates output data.

Example4: Display only 1st and 4th column and the separator between at output for these columns should be $.

awk 'BEGIN{FS=",";OFS=" $ "}{print $1,$4}' db.txt

Output:

John $ IBM
Barbi $ JHH
Mitch $ BofA
Tim $ DELL
Lisa $ SmartDrive

Note: I given space before and after $ in OFS variable to show better output. You can remove the spaces if required.

I will leave printing only first and fourth columns to readers without using OFS and see the issue.

Group2: RS(Row separator) and ORS(Output record separator)


RS(Row separator) and ORS(Output record separator).

RS AWK Variable: Row Separator is helpful in defining separator between rows in a file. By default AWK takes row separator as new line. We can change this by using RS built-in variable.

Example5: I want to convert a sentence to a word per line. We can use RS variable for doing it.

echo “This is how it works” | awk ‘BEGIN{RS=” ”}{print $0}’

Output:

This
is
how
it
works

ORS(Output Record Separator): This variable is useful for defining the record separator for the AWK command output. By default ORS is set to new line.

Example6: Print all the company names in single line which are in 4th column.

awk -F',' 'BEGIN{ORS=" "}{print $4}' db.txt

Output:

IBM JHH BofA DELL SmartDrive

Group3: NF, NR and FNR

 NF AWK variable: This variable keeps information about total fields in a given row. The final value of a row can be represented with $NF.

Example7: Print number of fields each row in db.txt file.

 awk '{print NF}' db.txt




Output:

5
5
4
5
4

Example8: Print last field in each row of db.txt file.

awk '{print $NF}' db.txt



Output:

77
45
37
95
47

Note: If you observe above two examples We used Just NF for giving us the count of fields in a given row and $NF for displaying last element in each row. $NF will come handy when you are not sure what is your last column number.

NR AWK variable: This variable keeps the value of present line number. This will come handy when you want to print line numbers in a file.

Example9: Print line number for each line in a given file.

awk '{print NR, $0}' db.txt

Output:

1 Jones 2143 78 84 77
2 Gondrol 2321 56 58 45
3 RinRao 2122234 38 37
4 Edwin 253734 87 97 95
5 Dayan 24155 30 47

 This can be treated as cat command -n option for displaying line number for a file.

FNR AWK variable: This variable keeps count of number of lines present in a given file/data. This will come handy when you want to print no of line present in a given file. This command is equivalent to wc -l command.

Example10: Print total number of lines in a given file.

awk 'END{print FNR}' db.txt

Output:

5

From the above output we can conclude that number of lines present in db.txt file is 5.

Group4: FILENAME variable



FILENAME AWK variable: This variable contain file awk command is processing.

Example11: Print filename for each line in a given file.

 awk '{print FILENAME, NR, $0}' abc.txt

Output:

abc.txt 1 Jones 2143 78 84 77
abc.txt 2 Gondrol 2321 56 58 45
abc.txt 3 RinRao 2122234 38 37
abc.txt 4 Edwin 253734 87 97 95
abc.txt 5 Dayan 24155 30 47

In our next post we will see how to use ARRAY's in AWK scripting.

Wednesday, April 24, 2013

AWK Scripting: How to define awk variables

AWK variables: This is our ongoing tutorials on AWK scripting. As we mention earlier AWK is a full pledged language with all statements, arrays, control structures, functions etc. Today we will see how to define a variable in AWK and use it when it’s required. We already covered following AWK concepts

AWK scripting: What is an AWK and how to use it?
AWK scripting: 14 AWK print statment examples
AWK scripting: 8 AWK printf statements examples
AWK scripting: 10 BEGIN and END block examples

What is a Variable?

A variable is defined as storage location to store some value in it so that we can use this in a program. Variables will protect us from varying values in the storage location. This will help us to avoid hardcode a value in to program where ever its used. We can define a variable at the start of program and use the variable across the program, if we want to change the value of it, we can change it where we define it and this value will be updated where ever we use that variable.

Defining a variable in AWK

We can define variable in AWK where ever we require. Variables can be used for
Initializations for values
Arithmetic operations
And many more.

For this concept we will use below db.txt file.

cat db.txt
 
Jones 2143 78 84 77
Gondrol 2321 56 58 45
RinRao 2122234 38 37
Edwin 253734 87 97 95
Dayan 24155 30 47

 

Lets learn AWK variables with examples


Example1:  Add all values in column 3 and display for each addition from our db.txt file. Frist value should be first value in column3, second in the list should be first value + second value and so on.
awk 'BEGIN{a=0}{a=a+$3;print a}' db.txt
Output:
78
134
172
259
289

In the above example we defined a variable a and initialized to zero in the BEGIN block and did arithmetic operation in main block.
Example2: I don’t want above output, I just want to print a final value once the sum of all value in 3rd column. We can use END block for doing a final print instead of printing per iteration.
awk 'BEGIN{a=0}{a=a+$3}END{print a}' db.txt
Output:
289
Example3: Print all the lines which contain numbers greater than VAR1 in its column 4

awk 'BEGIN{VAR1=57}(VAR1<$4){print $0}' db.txt
 
Output:

Jones 2143 78 84 77
Gondrol 2321 56 58 45
Edwin 253734 87 97 95


These are simple examples to start with Varaiables. In our next post we will see how get inputs from user.

Wednesday, February 27, 2013

AWK scripting: 10 BEGIN and END block examples

BEGIN and END statements are help full in displaying information before and after executing actual AWK script.

BEGIN block Uses:

  • Declaring variables.
  • Initialization variables for doing increment/decrements operations in main AWK code.
  • Printing Headings/info before actual AWK code output.


END block Uses:

  • Printing final results, after doing operations in main AWK block.
  • Printing Completion/info after actual AWK code output.


AWK tool is mainly used for reporting some data in useful manner. With out these BEGIN and END blocks the output will be meaning less. In this post we will see different ways to use BEGIN and END blocks.



For this post the example file is db.txt which contains below data:

Jones 2143 78 84 77
Gondrol 2321 56 58 45
RinRao 2122234 38 37
Edwin 253734 87 97 95
Dayan 24155 30 47

What you get from above data? They are just names and followed by some numbers separated by spaces. If we can give some information at start of the file, such as what each column corresponding to etc as shown below

Names total PPT Doc xls
Jones 2143 78 84 77
Gondrol 2321 56 58 45
RinRao 2122234 38 37 0
Edwin 253734 87 97 95
Dayan 24155 30 47 0

This is more helpful to understand what actually this data is about. This type of output can be achieved by using BEGIN block. In this post we will see how to use BEGIN and END blocks with examples.

AWK BEGIN block

This is a block code which is executed before executing actual AWK script

Syntax for BEGIN block is

awk 'BEGIN{awk initializing code}{actual AWK code}' filename.txt

Example1: Print a meaning full info before actual AWK output.

awk 'BEGIN{print "########################\nThis is the output of filtered data\n########################"}{print $0}' db.txt

Output:

##########################
This is the output of filtered data
##########################
Jones 2143 78 84 77
Gondrol 2321 56 58 45
RinRao 2122234 38 37
Edwin 253734 87 97 95
Dayan 24155 30 47

Example2: How about giving each column names along with data? We can name each column as Name, pass code,



awk 'BEGIN{print "Names\ttotal\tPPT\tDoc\txls"}{printf "%-s\t%d\t%d\t%d\t%d\n", $1,$2,$3,$4,$5}' db.txt

Output:

Names total  PPT Doc xls
Jones 2143 78 84 77
Gondrol 2321 56 58 45
RinRao 2122234 38 37 0
Edwin 253734 87 97 95
Dayan 24155 30 47 0



Example3: How about changing the output field separator to "|" we can do that by using OFS inbuilt variable in BEGIN block.


awk 'BEGIN{OFS="|"}{print $1,$2,$3,$4,$5}' db.txt

Output:

Jones|2143|78|84|77
Gondrol|2321|56|58|45
RinRao|2122234|38|37|
Edwin|253734|87|97|95
Dayan|24155|30|47|




Example4: How about converting /etc/passwd to above format? We can do that by defining input feild seperator "FS" to ":".

awk 'BEGIN{FS=":";OFS="|"; }{print $1,$2,$3,$4,$5,$6,$7}' /etc/passwd | head -5


Output:


root|x|0|0|root|/root|/bin/bash
daemon|x|1|1|daemon|/usr/sbin|/bin/sh
bin|x|2|2|bin|/bin|/bin/sh
sys|x|3|3|sys|/dev|/bin/sh
sync|x|4|65534|sync|/bin|/bin/sync


Note1: Make note that by default input field separator is either space or tab.
Note2: We can use either FS inbuilt variable or -F option to specify input field seperator.


awk -F":" 'BEGIN{OFS="|"; }{print $1,$2,$3,$4,$5,$6,$7}' /etc/passwd | head -5

Output:

root|x|0|0|root|/root|/bin/bash
daemon|x|1|1|daemon|/usr/sbin|/bin/sh
bin|x|2|2|bin|/bin|/bin/sh
sys|x|3|3|sys|/dev|/bin/sh
sync|x|4|65534|sync|/bin|/bin/sync



Example5: We can use even BEGIN block to initialize user defined variables in it. I want to add my 3rd column for every iteration to the same value.

My 3rd column

78
56
21
87
55

I want to add these numbers and display every addition when it happen

awk 'BEGIN{s=0}{s=s+$3;print s}' db.txt

Output:

78
134
155
242
297

We initialized s in BEGIN block and incremented column 3 values in actual AWK code block.

AWK END block

This is piece of block which is executed after executing all the AWK code. This is the last AWK code going to execute.

Example6: Print some meaning full info after processing AWK code.

awk '{print $0}END{print "#########################\nCompleted printing filtered data\n########################"}' db.txt

Output:

Jones 21 78 84 77
Gondrol 23 56 58 45
RinRao 25 21 38 37
Edwin 25 87 97 95
Dayan 24 55 30 47
#########################
Completed printing filtered data
#########################

Combining BEGIN and END blocks

Example7: Print some meaning full before and after AWK code execution.

awk 'BEGIN{print "##########################\nThis is the output of filtered data\n##########################"}{print $0}END{print "########################\nCompleted printing filterd data\n########################"}' db.txt

Output:

#########################
This is the output of filtered data
#########################
Jones 21 78 84 77
Gondrol 23 56 58 45
RinRao 25 21 38 37
Edwin 25 87 97 95
Dayan 24 55 30 47
########################
Completed printing filtered data
########################

Example8: How about printing headings for each column?



awk 'BEGIN{print "##########################\nThis is the output of filtered data\n###########################";print "Names\ttotal\tPPT\tDoc\txls"}{printf "%s\t%d\t%d\t%d\t%d\n", $1,$2,$3,$4,$5}END{print "###########################\nCompleted printing filtered data\n###########################"}' db.txt

Output:

###########################
This is the output of filtered data
###########################
Names total PPT Doc xls
Jones 21 78 84 77
Gondrol 23 56 58 45
RinRao 25 21 38 37
Edwin 25 87 97 95
Dayan 24 55 30 47
###########################
Completed printing filtered data
###########################


Example9: How to adding all the numbers in 4th column and print the total value at the end.

awk 'BEGIN{s=0}{s=s+$3}END{print s}' db.txt

Output:

297

There is a slight difference between Example 5 and this. We used END block to print the last sum instead of printing the sum for every iteration. In our next post we will see how to use AWK inbuilt variables  in detail.

Wednesday, February 20, 2013

AWK scripting: 8 AWK printf statements examples

This is our third post on AWK scripting. In this post we will cover printf statement which very much use full for formatting output as the padding spaces before and after the column entries etc. Already covered topics in this series are


printf is similar to AWK print statement but the advantage is that it can print with formatting the output in a desired manner. So before learning printf command I suggest you to learn about print command and then come to this printf statement.

printf syntax is similar to Bash, C type printf statement.

Syntax:

printf format, Arguments

For example you want to print decimal values of column 3 then the example will be.

awk '{printf "%d", $3}' example.txt

Printf can do two things which print command don't

1)Defining type of Data.
2)Padding between columns.

Type of data

printf can be useful when specifying data type such as integer, decimal, octal etc. Below are the list of some data types which are available in AWK.
%i or d --Decimal
%o --Octal
%x --hex
%c --ASCII number character
%s --String
%f --floating number

Note: Make sure that you pass exact data types when using corresponding formats as shown below. If you pass a string to a decimal formatting, it will print just zero instead of that string.

Lets start with some examples. for this post our test file contents are
Jones 21 78 84 77
Gondrol 23 56 58 45
RinRao 25 21 38 37
Edwin 25 87 97 95
Dayan 24 55 30 47

Example 1: Print first column values from db.txt file.

awk '{printf "%s\n", $1}' db.txt

Output:

Jones
Gondrol
RinRao
Edwin
Dayan

Note: printf will not have default new line char, so you have to include tat when ever you execute printf command as shown above.

Example 2: Try printing a string with decimal format and see the difference.

awk '{printf "%d\n", $1}' db.txt

Output:
0
0
0
0
0

So be careful when dealing with different data types.

Padding between columns

Let us explore the formatting the column's available with printf statements.

Types of formatting: We can format the columns to specify number of chars each column can use. We have following padding formats available with printf statement.

-n --Pad n spaces on right hand side of a column.
n --Pad n spaces on left hand side of a column.
.m --Add zeros on left side.
-n.m --Pad n spaces right hand side and add m zeros before that number.
n.m --Pad n spaces left hand side and add m zeros before that.

Let us start exploring above mention padding's with examples in detail.

Example4: Pad 5 spaces on right hand side of each column.

With out padding

awk '{printf "%d%d%d\n", $2,$3,$4}' db.txt

Output
217884
235658
252138
258797
245530

With padding 5 spaces on right hand side:

awk '{printf "%-5d%-5d%-5d\n", $2,$3,$4}' db.txt

Output:
21   78   84
23   56   58
25   21   38
25   87   97
24   55   30
Note: As for understanding purpose we given this example with padding and with out padding. We will add "|" between columnts output so that that padding can be clearly seen as show in below example.

awk '{printf "|%-5d|%-5d|%-5d|\n", $2,$3,$4}' db.txt

Output:

|21   |78   |84   |
|23   |56   |58   |
|25   |21   |38   |
|25   |87   |97   |
|24   |55   |30   |

Example 5: Pad 5 spaces on left hand side of each column.

awk '{printf "|%5d|%5d|%5d|\n", $2,$3,$4}' db.txt

Output:

|   21|   78|   84|
|   23|   56|   58|
|   25|   21|   38|
|   25|   87|   97|
|   24|   55|   30|

Example 6: add zero's on left hand side of each column element make it a 5 digit number.

 awk '{printf "|%.5d|%.5d|%.5d|\n", $2,$3,$4}' db.txt

Output:

|00021|00078|00084|
|00023|00056|00058|
|00025|00021|00038|
|00025|00087|00097|
|00024|00055|00030|

Example 7: Make the column element with 4 digits and 7 in length and print the number to left hand side.


 awk '{printf "|%-7.4d|%-7.4d|%-7.4d|\n", $2,$3,$4}' db.txt

Output:

|0021   |0078   |0084   |
|0023   |0056   |0058   |
|0025   |0021   |0038   |
|0025   |0087   |0097   |
|0024   |0055   |0030   |

Example 8: Make the column element with 4 digits and 7 in length and print the number to right hand side.

awk '{printf "|%7.4d|%7.4d|%7.4d|\n", $2,$3,$4}' db.txt

Output:

|   0021|   0078|   0084|
|   0023|   0056|   0058|
|   0025|   0021|   0038|
|   0025|   0087|   0097|
|   0024|   0055|   0030|

Hope these examples help you to understand about formatting output and padding using printf statement in AWK scripting. Keep visiting scripting.linuxnix.com for more scripting tutorials and tips.

Monday, February 18, 2013

AWK scripting: 14 AWK print statment examples


In our last post we covered about AWK and how is it useful. In this post we will see different print statement examples. We will learn AWK print statement with examples as print statement is one of the most used AWK command. For this we taken a test file as below

cat db.txt

Ip,Henry 29 32 23 27
Frank,Joe 26 29 22 28
Gill,Marry 25 30 20 25
Avery,Adam 25 31 19 21
Chow,Samuel 23 29 19 25
Dible,Liz 22 30 20 22
Warn,Suzanne 23 29 19 23
Dow,Juila 24 29 20 20
Low,juila 22 21 19 18
Joe,Sarah 19 21 18 20


Example1: Search for Dow word in the file and print those lines. This is like simulate grep using AWK

awk '/Dow/' db.txt

Sample output:
Dow,Juila 24 29 20 20


Example2: Search for a word and print the lines which contain either Juila or juila in the given file.

awk '/[Jj]uila/' db.txt

Output:
Dow,Juila 24 29 20 20
Low,juila 22 21 19 18


In the above example we used regular expression to match both Juila and juila. Know more about RegExp here.

Example3: Print particular column from db.txt file.

awk '{print $2}' db.txt

Output:
29
26
25
25
23
22
23
24
22
19


Exampl4: Print multiple columns from a given file.

awk '{print $2,$4}' db.txt

Output:
29 23
26 22
25 20
25 19
23 19
22 20
23 19
24 20
22 19
19 18

Example5: Print multiple columns with a tab between columns as separator

awk '{print $2,"\t",$4}' db.txt 

Output:
29 23
26 22
25 20
25 19
23 19
22 20
23 19
24 20
22 19
19 18

Example6: Search for Chow word and print corresponding third column. In most of the programming languages /searchterm/ is meant for searching for a word. AWK too uses same concept when search for a word in a given line.

awk '/Chow/{print $3}' db.txt
Output:
29

Example7: Print all the column 4 values between lines which contain Frank and Low

awk '/Frank/,/Low/{print $4}' db.txt
Output:
29
30
31
29
30
29
29
21

Example8: Print line numbers more than 5.
awk 'NR\>5'  db.txt
Output:
Dible,Liz 22 30 20 22
Warn,Suzanne 23 29 19 23
Dow,Juila 24 29 20 20
Low,juila 22 21 19 18
Joe,Sarah 19 21 18 20

Note1: NR is a inbuilt variable which keeps the Line numbers of a file, to know more about it just visit out post on AWK inbuilt variables.

Example9: Print lines from 3 to 6.

awk 'NR>3 && NR<7 db.txt="">
Output:
Avery,Adam 25 31 19 21
Chow,Samuel 23 29 19 25
Dible,Liz 22 30 20 22

Till this point we search for entire line and then printed desired output. Following examples will search for a particular column.

Example10: Print only lines which have 29 in its third column.

awk'$3 ~/29/' db.txt

Output:

Frank,Joe  26 29 22 28
Chow,Samuel 23 29 19 25
Warn,Suzanne 23 29 19 23
Dow,Juila 24 29 20 20

We can use Regexp for more control on what we want to print.

Example11: Print column 5 if the column 3 contain 31 in it.

awk '$3 ~/31/{print $5}' db.txt

Output:
21

Example12: Print column 3 for the lines more than 4.


awk '(NR>4){print $3}' db.txt
Output:
29
30
29
29
21
21

Example13: Want to print something other than file content at the start of the output?. We can use inbuilt Block called BEGIN block. Suppose I want to print "This is the filtered output" use below code.

awk 'BEGIN{print "This is the filtered output"}(NR>4){print $3}' db.txt

Output:
This is the filtered output
29
30
29
29
21
21

Example14: What about printing after the filter output?. We can use END block for doing this.

awk '(NR>4){print $3}END{print "This is the end of filter output"}' db.txt
Output:
29
30
29
29
21
21
This is the end of filter output

In our next post we will see about AWK printf statements.

Saturday, February 16, 2013

AWK scripting: What is an AWK and how to use it?

This is our first post on AWK, in this we will see some of the basic stuff of AWK like history, advantages syntax and how it works etc.


1. A brief history about AWK

2. Advantages and disadvantages of AWK

3. AWK syntax

4. How AWK works?

AWK is a command/tool available in all the Linux/Unix flavors to do text filtering, manipulation etc. This tool is mainly meant for processing text files and reporting. AWK can be treated as a programming language due to its capabilities such as Arithmetic operations, Binary operations, conditions, loops, functions etc. AWK is an interpreter language. This tool/programming language was developed in 1977 by Alfred V. Aho, Peter J. Weinberger, Brian W. Kernighan. AWK got its name from its creates family names.

Due to its capabilities AWK earned its nick name as “Awk the Swiss army knife of the Unix toolkit”. This is true because it can do text processing with ease when compared to other text parsing tools available in Linux/Unix.

Below are some advantages and disadvantages of AWK tool which I come across when using it.

AWK Advantages

  • Validate data
  • Managing small db files
  • Generating reports
  • Parsing command outputs
  • Parsing log files
  • Can parse more than one file at a time.


AWK Disadvantages


  • Many flavors are there: awk, nawk, gawk,mawk, tawk which makes portability issue.
  • Cannot be full-pledge scripting language like PERL, Python and Ruby.
  • Useful only for data processing most of the time.


The syntax for AWK command is as follows

 When executing an AWK command we are going to use one of the below syntax.

awk options 'awk-code' filename

Unix-command | awk options 'awk-code'

awk options -f awkscript-file filename

How AWK works?


AWK works line by line: As many assume that AWK works columns wise, but it’s not true. AWK works as SED works i.e. in horizontal manner, reading one line after the other.

Awk treats a file as group of columns: AWK when reading lines it will assign data to columns depending on the field separator.

Column number: AWK assigns each column with a number and they are represented as $1, $2, $3 and so on till the last column. The last column number is assigned with NF(Number of Fields). And entire line/record is represented with $0. NF is a built-in variable contains total number of columns. Awk knows the last column number and equals NF. Depend on different conditions AWK works on these columns to get desired output.

In our next post we will see about AWK inbuilt variable. These variable are very much important as they are frequently referred by AWK to do its activity.

Monday, December 17, 2012

Shell script: How to add multiple sequence numbers

This can be acheived in many ways.

Through for loop

Write a for loop using {}(know more about {} HERE) to mention the range of numbers and use let command to add those numbers. Below is the script which will uses for loop to add 1 to 100 numbers

#!/bin/bash

j=0
for i in {1..100}
do
let j=j+i
done
printf "Sum of 1 to 100 numbers is %d" $j

Explination: for will take each number in to i variable and adds up to j for each loop and give you the final result in j as shown in above printf command.
Note: Try to avoid echo command as much as possible as this is not protable.
Through while loop: Loop will continue while the condition is true.   #!/bin/bash
j=0
i=0
while [[ "$i" -le 100 ]]
do
let j=j+i,i++
done
printf "Sum of 1 to 100 numbers is %d" $j


Explination: While loop will check for condition that i less than equal to 100, the loop passes untill i reaches 100. let command is used to increment j and i. And then final value is stored in j.
Through untill loop: Untill loop is similar to while loop but in reverse. Loop will happen untill the condition is true.

#!/bin/bash

j=0
i=0
until [[ "$i" -gt 100 ]]
do
let j=j+i,i++
done
printf "Sum of 1 to 100 numbers is %d" $j



Through awk scripting.
seq 1 100 | awk '{ sum+=$1} END {print sum}'

Hope this helps to add multiple numbers for a given range.