- 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.

- The problem is not really clear. What is the error you get? What is the line from ps -ef and what do you expect in $variable ? (if you can not provide real data, then generate a sanitized example that shows the problem instead.) – NiKiZe Sep 6, 2013 at 1:35
- Problem: variable does not show up in the printf statement. End goal: $12 corresponds to a serial number that is running with that process. I want to save that parameter into a variable so I can work with it. – Jeremy Sep 6, 2013 at 2:39
4 Answers 4
Notice that there's no space after the equal sign.
You can also use $() which allows nesting and is readable.

- thanks RUI & NiKiZe, it was the spacing before and after the equal sign. – Jeremy Sep 6, 2013 at 2:54
- @wens , how to save multiple variables, from a single command? – user2713461 Jun 26, 2020 at 9:06
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.
- 2 There's supposed to be an awk after the last pipe and before the '{printf . It was probably accidentally removed when the answer was edited. – aDroid Jan 27, 2017 at 22:02
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

Not the answer you're looking for? Browse other questions tagged linux bash shell awk centos or ask your own question .
- The Overflow Blog
- How Intuit democratizes AI development across teams through reusability sponsored post
- The nature of simulating nature: A Q&A with IBM Quantum researcher Dr. Jamie...
- Featured on Meta
- We've added a "Necessary cookies only" option to the cookie consent popup
- Launching the CI/CD and R Collectives and community editing features for...
- Staging Ground Beta 1 Recap, and Reviewers needed for Beta 2
- The [amazon] tag is being burninated
- Temporary policy: ChatGPT is banned
Hot Network Questions
- Minimising the environmental effects of my dyson brain
- Can Martian regolith be easily melted with microwaves?
- ncdu: What's going on with this second size column?
- How can I explain to my manager that a project he wishes to undertake cannot be performed by the team?
- Is there a single-word adjective for "having exceptionally strong moral principles"?
- How can this new ban on drag possibly be considered constitutional?
- What is the point of Thrower's Bandolier?
- What can a lawyer do if the client wants him to be acquitted of everything despite serious evidence?
- Do I need a thermal expansion tank if I already have a pressure tank?
- Why does Mister Mxyzptlk need to have a weakness in the comics?
- Has 90% of ice around Antarctica disappeared in less than a decade?
- Disconnect between goals and daily tasks...Is it me, or the industry?
- My code is GPL licensed, can I issue a license to have my code be distributed in a specific MIT licensed project?
- Bulk update symbol size units from mm to map units in rule-based symbology
- Why are physically impossible and logically impossible concepts considered separate in terms of probability?
- Does a summoned creature play immediately after being summoned by a ready action?
- Recovering from a blunder I made while emailing a professor
- Can airtags be tracked from an iMac desktop, with no iPhone?
- About an argument in Famine, Affluence and Morality
- Copyright issues when journal is defunct
- Should I put my dog down to help the homeless?
- Is it possible to rotate a window 90 degrees if it has the same length and width?
- Redoing the align environment with a specific formatting
- Did this satellite streak past the Hubble Space Telescope so close that it was out of focus? If so, how close was it?
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
- command-line
- tldp,org -> Bash guides – Hannu Apr 3, 2019 at 16:08
Read man bash or https://manpages.ubuntu.com/manpages/bionic/en/man1/bash.1.html an do something like
Not the answer you're looking for? Browse other questions tagged command-line bash scripts awk or ask your own question .
- The Overflow Blog
- How Intuit democratizes AI development across teams through reusability sponsored post
- The nature of simulating nature: A Q&A with IBM Quantum researcher Dr. Jamie...
- Featured on Meta
- We've added a "Necessary cookies only" option to the cookie consent popup
- Announcement: AI-generated content is now permanently banned on Ask Ubuntu
Hot Network Questions
- Why does Mister Mxyzptlk need to have a weakness in the comics?
- Checking system vs. SEPA and the like
- Bulk update symbol size units from mm to map units in rule-based symbology
- Disconnect between goals and daily tasks...Is it me, or the industry?
- What's the difference between a power rail and a signal line?
- Theoretically Correct vs Practical Notation
- Is there a proper earth ground point in this switch box?
- Has 90% of ice around Antarctica disappeared in less than a decade?
- Using indicator constraint with two variables
- Why zero amount transaction outputs are kept in Bitcoin Core chainstate database?
- The difference between the phonemes /p/ and /b/ in Japanese
- How do you get out of a corner when plotting yourself into a corner
- Knocking Out Zombies
- Is there a solution to add special characters from software and how to do it
- Follow Up: struct sockaddr storage initialization by network format-string
- base table view not found
- OK to delete Windows.db file on PC?
- Identify those arcade games from a 1983 Brazilian music video
- Do roots of these polynomials approach the negative of the Euler-Mascheroni constant?
- Can Martian regolith be easily melted with microwaves?
- My code is GPL licensed, can I issue a license to have my code be distributed in a specific MIT licensed project?
- Short story taking place on a toroidal planet or moon involving flying
- Minimising the environmental effects of my dyson brain
- Where does this (supposedly) Gibson quote come from?
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 ?

- 1 This doesn't print rows. It will print the 1st or 2nd column (field) of every line in the input file. Is that what you want? Or do you want the actual row (e.g. line 2) to be printed? – terdon ♦ Apr 24, 2018 at 11:31
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 .

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 .
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 .
- The Overflow Blog
- How Intuit democratizes AI development across teams through reusability sponsored post
- The nature of simulating nature: A Q&A with IBM Quantum researcher Dr. Jamie...
- Featured on Meta
- We've added a "Necessary cookies only" option to the cookie consent popup
Hot Network Questions
- What did Ctrl+NumLock do?
- Did this satellite streak past the Hubble Space Telescope so close that it was out of focus? If so, how close was it?
- Who owns code in a GitHub organization?
- Follow Up: struct sockaddr storage initialization by network format-string
- Theoretically Correct vs Practical Notation
- Does ZnSO4 + H2 at high pressure reverses to Zn + H2SO4?
- Is there a proper earth ground point in this switch box?
- Why are Suriname, Belize, and Guinea-Bissau classified as "Small Island Developing States"?
- Disconnect between goals and daily tasks...Is it me, or the industry?
- remove package from CTAN
- ncdu: What's going on with this second size column?
- Identify those arcade games from a 1983 Brazilian music video
- Do I need a thermal expansion tank if I already have a pressure tank?
- If you preorder a special airline meal (e.g. vegan) just to try it, does this inconvenience the caterers and staff?
- Checking system vs. SEPA and the like
- Replacing broken pins/legs on a DIP IC package
- Why do many companies reject expired SSL certificates as bugs in bug bounties?
- Largest Binary Area
- nicematrix: add ttfamily in the last-col
- Styling contours by colour and by line thickness in QGIS
- Recovering from a blunder I made while emailing a professor
- ERROR: CREATE MATERIALIZED VIEW ... WITH DATA cannot be executed from a function
- Can Martian regolith be easily melted with microwaves?
- Where does this (supposedly) Gibson quote come from?
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.

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.

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.

Prerequisites
- A system running Linux.
- Access to a terminal window.
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:
- Scan a file line by line.
- Split the input line/file into fields.
- Compare the input line or fields with the specified pattern(s).
- Perform various actions on the matched lines.
- Format the output lines.
- Perform arithmetic and string operations.
- Use control flow and loops on output.
- Transform the files and data according to a specified structure.
- Generate formatted reports.
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:

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:

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

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:

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:
- Regular expressions.
- Arithmetic relational expressions.
- String-valued expressions.
- Arbitrary Boolean combinations of the expressions above.
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.

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:

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.

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:

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:
- && (and)

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:
- $0 . Used to specify the whole line.
- $1 . Specifies the first field.
- $2 . Specifies the second field.
Other available built-in awk variables are:
- NR . Counts the number of input records (usually lines). The awk command performs the pattern/action statements once for each record in a file.

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.

- FS . Contains the character used to divide fields on the input line. The default separator is space, but you can use FS to reassign the separator to another character (typically in BEGIN ).
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:

- RS . Stores the current record separator character. The default input line is the input record, which makes a newline the default record separator. The command is useful if the input is a comma-separated file (CSV).

Note: We first used the cat command to show the file's contents and then formatted the output with AWK .
- OFS . Stores the output field separator, which separates the fields when printed. The default separator is a blank space. Whenever the printed file has several parameters separated with commas, the OFS value is printed between each parameter.

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:

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 .
- Calculations . The awk command allows you to perform arithmetic calculations. For example:

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.
- Filtering . The awk command allows you to filter the output by limiting the length of the lines. For example:

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.
- Monitoring . Check if a certain process is running in Linux by piping into the ps command. For example:

The output prints a list of all the processes running on your machine with the last field matching the specified pattern.
- Counting . You can use awk to count the number of characters in a line and get the number printed in the result. For example:

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.

IMAGES
VIDEO
COMMENTS
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
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
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:
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?
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 }
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
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.