• Stack Overflow Public questions & answers
  • Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers
  • Talent Build your employer brand
  • Advertising Reach developers & technologists worldwide
  • About the company

Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Saving awk output to variable [duplicate]

Can anyone help me out with this problem?

I'm trying to save the awk output into a variable.

EDIT: $12 corresponds to a parameter running on that process.

Noam M's user avatar

4 Answers 4

Notice that there's no space after the equal sign.

You can also use $() which allows nesting and is readable.

thvwns's user avatar

I think the $() syntax is easier to read...

But the real issue is probably that $12 should not be qouted with ""

Edited since the question was changed, This returns valid data, but it is not clear what the expected output of ps -ef is and what is expected in variable.

NiKiZe's user avatar

as noted earlier, setting bash variables does not allow whitespace between the variable name on the LHS, and the variable value on the RHS, of the '=' sign.

awk can do everything and avoid the "awk"ward extra 'grep'. The use of awk's printf is to not add an unnecessary "\n" in the string which would give perl-ish matcher programs conniptions. The variable/parameter expansion for your case in bash doesn't have that issue, so either of these work:

The '-' int the awk record matching pattern removes the need to remove awk itself from the search results.

The [p] is a neat trick to remove the search from showing from ps

@Jeremy If you post the output of ps -ef | grep "port 10" , and what you need from the line, it would be more easy to help you getting correct syntax

Jotne's user avatar

Not the answer you're looking for? Browse other questions tagged linux bash shell awk centos or ask your own question .

Hot Network Questions

bash assign awk output to variable

Your privacy

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy .

Stack Exchange Network

Stack Exchange network consists of 181 Q&A communities including Stack Overflow , the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Ask Ubuntu is a question and answer site for Ubuntu users and developers. It only takes a minute to sign up.

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

How can I store the output of an awk command as a bash script variable? [duplicate]

When operating in the linux terminal I can enter the following: command

''' awk 'FNR==1 {print$5}' SNextractedstats.txt '''

To display the value in the first row and fifth column of a text file.

I would like to be able to save this value as a variable in a bash script to be able to do sums and call using Echo, how would I go about doing this? Thank you

Omar Al-kamil's user avatar

Read man bash or https://manpages.ubuntu.com/manpages/bionic/en/man1/bash.1.html an do something like

waltinator's user avatar

Not the answer you're looking for? Browse other questions tagged command-line bash scripts awk or ask your own question .

Hot Network Questions

bash assign awk output to variable

Your privacy

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy .

Stack Exchange Network

Stack Exchange network consists of 181 Q&A communities including Stack Overflow , the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

awk + set variable inside the awk

The following simple bash script prints the row number (from the file list.txt ):

But I would like to write it more elegantly.

Is it possible to set $1 as variable as awk '{print $val}' , so that if I call a function as get_row 1 , the variable $val gets the value 1 ?

Kusalananda's user avatar

2 Answers 2

Do you want something like this?

Above is returning the column match with $1 passing to the function. if you really need print the line match with line number in $1 from function, instead use below.

Or let shell evaluated and set the val value and print that within awk as following:

Here you are passing val with only numbers and if val was contain backslash escape character you will encounter a problem which awk does C escape sequence processing on values passed via -v val= and a shell variable with val="\\n" will change to value with \n by awk .

αғsнιη's user avatar

You can use:

it works in all POSIX compliant system, and you don't have to worry about code injection when using -v val=

Also the function should be named get_column instead of get_row .

cuonglm's user avatar

Your Answer

Sign up or log in, post as a guest.

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service , privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged bash awk or ask your own question .

Hot Network Questions

bash assign awk output to variable

Your privacy

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy .

Shell Programming and Scripting

Assign awk output to bash variable.

Member Information Avatar

10 More Discussions You Might Find Interesting

1. unix for beginners questions & answers, using read to assign value to bash variable not working, discussion started by: sand1234, 2. unix for beginners questions & answers, how do i assign the output of a command to a variable within a loop in bash, discussion started by: sankasu, 3. shell programming and scripting, assign output to dynamic variable, discussion started by: ganga.dharan, 4. shell programming and scripting, unable to assign command output to variable, discussion started by: funkeydude, 5. shell programming and scripting, help on awk---- need to assign the output of awk to a variable, discussion started by: nithz, 6. shell programming and scripting, in bash script, how to assign output of a command to a variable while keeping tabs, discussion started by: birddie, 7. shell programming and scripting, bash assign string to variable, discussion started by: lio123, 8. shell programming and scripting, assign bash command to variable, discussion started by: moxy, 9. shell programming and scripting, hot to assign output to a variable, discussion started by: walnut, 10. unix for dummies questions & answers, how to assign an output to a variable, discussion started by: vasikaran, member badges and information modal, featured tech videos.

bash assign awk output to variable

AWK Command in Linux with Examples

Home » SysAdmin » AWK Command in Linux with Examples

Introduction

The awk command is a Linux tool and programming language that allows users to process and manipulate data and produce formatted reports. The tool supports various operations for advanced text processing and facilitates expressing complex data selections.

In this tutorial, you will learn what the awk command does and how to use it.

AWK command in Linux with examples

Prerequisites

AWK Command Syntax

The syntax for the awk command is:

The available options are:

Note: You might also be interested in learning about the Linux curl command , allowing you to transfer data to and from a server after processing it with awk.

How Does the AWK Command Work?

The awk command's main purpose is to make information retrieval and text manipulation easy to perform in Linux. This Linux command works by scanning a set of input lines in order and searches for lines matching the patterns specified by the user.

For each pattern, users can specify an action to perform on each line that matches the specified pattern. Thus, using awk , users can easily process complex log files and output a readable report.

Note: The awk command got its name from three people who wrote the original version in 1977 - Alfred A ho, Peter W einberger, and Brian K ernighan.

AWK Operations

awk allows users to perform various operations on an input file or text. Some of the available operations are:

AWK Statements

The command provides basic control flow statements ( if-else , while , for , break ) and also allows users to group statements using braces {} .

The if-else statement works by evaluating the condition specified in the parentheses and, if the condition is true, the statement following the if statement is executed. The else part is optional.

For example:

Using the if-else statement in awk.

The output shows the lines in which duplicates exist and states No duplicates if there are no duplicate answers in the line.

The while statement repeatedly executes a target statement as long as the specified condition is true. That means that it operates like the one in the C programming language. If the condition is true, the body of the loop is executed. If the condition is false, awk continues with the execution.

For example, the following statement instructs awk to print all input fields one per line:

Using the while statement in awk.

The for statement also works like that of C, allowing users to create a loop that needs to execute a specific number of times.

An example of the for statement in awk.

The statement above increases the value of i by one until it reaches ten and calculates the square of i each time.

Note: The expressions in the condition part of if , while or for can include relational operators, such as <, , >=, == (is equal to), and != (not equal to). The expressions can also include regular expression matches with the match operators ∼ and !∼ , logical operators ||, && , and ! . The operators are grouped with parentheses.

The break statement immediately exits from an enclosing while or for . To begin the next iteration, use the continue statement.

The next statement instructs awk to skip to the next record and begin scanning for patterns from the top. The exit statement instructs awk that the input has ended.

Following is an example of the break statement:

Using the break statement in awk.

The command above breaks the loop after 5 iterations.

Note: The awk tool allows users to place comments in AWK programs. Comments begin with # and end at the end of the line.

AWK Patterns

Inserting a pattern in front of an action in awk acts as a selector . The selector determines whether to perform an action or not. The following expressions can serve as patterns:

The following sections explain the above-mentioned expressions and how to use them.

Note: Learn how you can search for strings or patterns with the grep command .

Regular Expression Patterns

Regular expression patterns are the simplest form of expressions containing a string of characters enclosed in slashes. It can be a sequence of letters, numbers, or a combination of both.

In the following example, the program outputs all the lines starting with "A". If the specified string is a part of a larger word, it is also printed.

An example of a regular expression pattern.

Relational Expression Patterns

Another type of awk patterns are relational expression patterns. The relational expression patterns involve using any of the following relational operators: <, <=, ==, !=, >= , and > .

Following is an example of an awk relational expression:

An example of a relational expression pattern.

Range Patterns

A range pattern is a pattern consisting of two patterns separated by a comma. Range patterns perform the specified action for each line between the occurrence of pattern one and pattern two.

Example of a range pattern.

The pattern above instructs awk to print all the lines of the input containing the keywords "clerk" and "manager".

Special Expression Patterns

Special expression patterns include BEGIN and END which denote program initialization and end. The BEGIN pattern matches the beginning of the input, before the first record is processed. The END pattern matches the end of the input, after the last record has been processed.

For example, you can instruct awk to display a message at the beginning and at the end of the process:

An example of a special expression pattern in awk.

Combining Patterns

The awk command allows users to combine two or more patterns using logical operators. The combined patterns can be any Boolean combination of patterns. The logical operators for combining patterns are:

Example of a combining pattern in awk.

The output prints the first and second fields of those records whose third field is greater than ten and the fourth field is less than 20.

AWK Variables

The awk command has built-in field variables, which break the input file into separate parts called fields . The awk assigns the following variables to each data field:

Other available built-in awk variables are:

Example of the NR variable in awk.

The command displays the line number in the output.

Example of the NF variable in awk.

For example, you can make the etc/passwd file ( user list ) more readable by changing the separator from a colon ( : ) to a dash ( / ) and print out the field separator as well:

An example of the FS variable in awk.

An example of the RS variable in awk.

Note: We first used the cat command to show the file's contents and then formatted the output with AWK .

An example of the ofs variable in awk.

AWK Actions

The awk tool follows rules containing pattern-action pairs. Actions consist of statements enclosed in curly braces {} which contain expressions, control statements, compound statements, input and output statements, and deletion statements. Those statements are described in the sections above.

Create an awk script using the following syntax:

An example of an awk action.

This simple command instructs awk to print the specified string each time you run the command. Terminate the program using Ctrl+D .

How to Use the AWK Command - Examples

Apart from manipulating data and producing formatted outputs, awk has other uses as it is a scripting language and not only a text processing command. This section explains alternative use cases for awk .

An example of using awk for performing arithmetic calculations.

In this example, we pipe into the df command and use the information generated in the report to calculate the total memory available and used by the mounted filesystems that contain only /dev and /loop in the name.

The produced report shows the memory sum of the /dev and /loop filesystems in columns two and three in the df output.

An example of using awk to filter a command output.

In this example, we ran the /etc/shells system file through awk and filtered the output to contain only the lines containing more than 8 characters.

An example of using awk to check running processes.

The output prints a list of all the processes running on your machine with the last field matching the specified pattern.

An example of using awk to count character number in each line of a file.

Note: Read also about gawk command , a text-processing and data-manipulating tool.

After reading this tutorial, you know what the awk command is and how you can use it effectively for various use cases.

The awk command is also a scripting language with many uses, and it is essential knowledge for every Linux user. Use it for powerful text manipulations, but also as a scripting language.

bash assign awk output to variable

IMAGES

  1. Using AWK in Bash Scripts

    bash assign awk output to variable

  2. Bash Pass Shell Variables To Awk Script

    bash assign awk output to variable

  3. assign a variable in bash Code Example

    bash assign awk output to variable

  4. Bash how to echo a variable

    bash assign awk output to variable

  5. Using AWK in Bash Scripts

    bash assign awk output to variable

  6. How to use Variables in Bash

    bash assign awk output to variable

VIDEO

  1. Contoh Menjadi MC (Master of ceremony) di Acara Seminar

  2. American Truck Simulator KENWORTH T800 CUSTOM MODOS 1.44 GRATIS #AMERICAN TRUCK #SIMULATION

  3. Digital Synth VRA8-U type-16 Demo (2023/01/06)

  4. MAKE UP GLOWING WAJAH BERCAHAYA

  5. HoverCam Neo 3 Buttons Tutorial

  6. Extech HDV600 High Definition VideoScope Inspection Monitor Camera from Burn Technology LTD

COMMENTS

  1. shell - How to assign awk result to an variable - Stack Overflow

    I need to assign the following awk result to an variable. Please guide me how to do in shell script awk -F: '$1==" {root}" {print $3}' /etc/passwd Like variable = `awk -F: '$1==" {root}" {print $3}' /etc/passwd` But it wont work shell awk Share Improve this question Follow edited Feb 10, 2015 at 16:26 Avinash Raj 171k 27 227 266

  2. linux - Saving awk output to variable - Stack Overflow

    The variable/parameter expansion for your case in bash doesn't have that issue, so either of these work: variable=$ (ps -ef | awk '/port 10 \-/ {print $12}') variable=`ps -ef | awk '/port 10 \-/ {print $12}'` The '-' int the awk record matching pattern removes the need to remove awk itself from the search results. Share Improve this answer Follow

  3. awk assign command result to a variable - Unix & Linux Stack ...

    first= ($ (awk (File [int (rand ()*records_count)]) )) looks like a line from shell script, not an awk script. Lose the ($ (awk ( and the ) )), then it looks like awk. Fourth: there is an awk exit command, but it is not needed in this instance. Applying all that gives:

  4. How can I store the output of an awk command as a bash script ...

    When operating in the linux terminal I can enter the following: command ''' awk 'FNR==1 {print$5}' SNextractedstats.txt ''' To display the value in the first row and fifth column of a text file. I would like to be able to save this value as a variable in a bash script to be able to do sums and call using Echo, how would I go about doing this?

  5. bash - awk + set variable inside the awk - Unix & Linux Stack ...

    function get_row { awk -v val=$1 'NR==val {print ; exit}' list.txt } Or let shell evaluated and set the val value and print that within awk as following: function get_column { awk ' {print $val}' val=$1 list.txt } function get_row { awk 'NR==val {print ; exit}' val=$1 list.txt }

  6. assign awk output to bash variable - UNIX

    assign awk output to bash variable greetings all, I am have a heck of a time trying to accomplish a very simple thing. I have an array of "shortname<spaces>id" created from a dscl output. I want to assign shortname=word1 and id=word2. I have tried

  7. AWK Command in Linux with Examples - Knowledge Base by phoenixNAP

    The awk command performs the pattern/action statements once for each record in a file. For example: awk ' {print NR,$0}' employees.txt. The command displays the line number in the output. NF. Counts the number of fields in the current input record and displays the last field of the file.