LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Where are you using awk, grep and sed? (https://www.linuxquestions.org/questions/linux-newbie-8/where-are-you-using-awk-grep-and-sed-4175522307/)

sigint-ninja 10-16-2014 06:43 AM

Where are you using awk, grep and sed?
 
Hi guys,

i understand the functions and fundamental differences between grep sed and awk...but cannot understand where you would utilise them in the real world...

i know configuration files are manipulated via the command line...
but would you really need to replace keywords like the with THE
these are the examples my book is giving me regarding these types of functions using grep sed and awk

are these commands used more for scripting?

i know grep is useful in a pipe command if you looking up specifics on something for example

lspci (lists all pci hardware)
lspci |grep vga (could list all data pertaining to vga within pci devices etc...this i get

but where practically does sed and awk come in? office workers arent using text files to store data are they?

thanks

rtmistler 10-16-2014 08:41 AM

I use grep in the command line to find stuff. I've sparingly used it as part of scripts. If you check the return from grep as shown in the manual page, it varies depending on whether the find itself failed or whether the command was issued in error, so one has to be careful.

I use sed to modify files from within a script or to globally change something in multiple files from the command line.

Never used awk, however my impression is that it's more highly used than sed; just from me seeing scripts where it's used.

In my case I'm a developer so I'm less about writing scripts except to provide for duplication of a process needed as part of a release.

sycamorex 10-16-2014 09:07 AM

Quote:

Originally Posted by sigint-ninja (Post 5254540)
Hi guys,

i understand the functions and fundamental differences between grep sed and awk...but cannot understand where you would utilise them in the real world...

i know configuration files are manipulated via the command line...
but would you really need to replace keywords like the with THE
these are the examples my book is giving me regarding these types of functions using grep sed and awk

are these commands used more for scripting?

i know grep is useful in a pipe command if you looking up specifics on something for example

lspci (lists all pci hardware)
lspci |grep vga (could list all data pertaining to vga within pci devices etc...this i get

but where practically does sed and awk come in? office workers arent using text files to store data are they?

thanks

While the functionality of the three tools overlap in many cases, typically you'd use:
awk) for data files organised in columns
sed) operations on strings in files organised in lines
grep) operations on lines (but only matching, not manipulating them like sed)

The above is a massive oversimplification and I'd characterise them in that way. The tool you choose should depend on the job.

Most often awk/sed would be used to modify a log files/ lines of code, etc. where the number of files or changes makes manual editing unfeasible.

sigint-ninja 10-16-2014 09:13 AM

thanks for that guys...

sycamorex can you link me to or give me information that elaborates what you talking about...or a more real world example
or could i just go and google each command and play with it...i am just finding it difficult to place all of this in the real world
is it something you would only dive deep into once you were placed in a job???

sycamorex 10-16-2014 09:24 AM

For examples of problems that people wanted to solve using sed/awk, just use the search option of this forum (look for sed or awk), you'll see threads with people's problems and solutions.


The best sed tutorial out there
http://www.grymoire.com/Unix/Sed.html
http://www.grymoire.com/Unix/Awk.html
http://www.grymoire.com/Unix/Grep.html

You'll also need to be familiar with regular expressions:
http://www.grymoire.com/Unix/Regular.html

TenTenths 10-16-2014 09:29 AM

Quote:

Originally Posted by sycamorex (Post 5254591)
While the functionality of the three tools overlap in many cases, typically you'd use:
awk) for data files organised in columns
sed) operations on strings in files organised in lines
grep) operations on lines (but only matching, not manipulating them like sed)

grep/sed are great for doing things programmatically.
Quote:

Originally Posted by sigint-ninja (Post 5254595)
or a more real world example

here's a command line I was running on a number of servers to change values in a config file based on the number of processors on a system.

[Edit:I'll split the command line out into the individual parts to make it a bit easier to read!]
Code:

grep 'loadavg\.\|uptime\.' conftab
sed s/$(grep "LOAD.loadavg.warn" conftab)/*:LOAD.loadavg.warn::@0-$(expr $(grep ^processor /proc/cpuinfo | wc -l) / 2 )/g -i conftab
sed s/$(grep "LOAD.loadavg.crit" conftab)/*:LOAD.loadavg.crit::@0-$(grep ^processor /proc/cpuinfo | wc -l)/g -i conftab



Quote:

Originally Posted by sigint-ninja (Post 5254595)
is it something you would only dive deep into once you were placed in a job???

I use grep, awk and sed on pretty much a daily basis!

grail 10-16-2014 09:59 AM

I follow the same line as others that there is always a right tool for the job. As far as 'real world' examples, just search this forum and you will find plenty of instances where people
have used all the commands you are looking at. I think it will also show you the diversity of the commands and maybe an idea on which is best for which jobs.

rknichols 10-16-2014 10:18 AM

1 Attachment(s)
I too use sed and grep on a daily basis. Here's a one-liner that finds the names of all files that are shell scripts in and below the current directory and stores that in a shell variable Files:
Code:

Files=$(find . -type f -exec file {} + | sed -ne 's/:.*shell script.*//p')
And no, it's not very robust -- file names with embedded white space or colon characters will be mishandled. I know I don't have any shell scripts with names like that.

awk is most commonly used in scripts, though I'll occasionally type a one-liner there, too. The examples I have tend to be complex (that's why they're in scripts). I've attached one that parses named fields out of a CSV file and prints them in a user-defined format.

suicidaleggroll 10-16-2014 10:31 AM

Here's a little one-liner to grab the current temperature of a hard disk, using both grep and awk
Code:

/usr/sbin/smartctl -d sat -a /dev/sda | grep Temperature | awk '{ print $10 }'
This could then be nested inside a script that probes the temperature of all HDDs on a regular basis (say through a cron job) and takes action as necessary.

metaschima 10-16-2014 10:36 AM

I prefer awk to sed. Its syntax is clearer and it can probably do more than sed. However, for convenience reasons I'll use sed for substitutions. For most everything else I'll use awk.

I use grep for finding words in files for convenience reasons. It is very useful, but not as versatile as awk. Awk is actually a scripting language and very powerful one at that. If you want even more power, try perl.

Specific uses of awk would be parsing tables and CSV files as said above.


All times are GMT -5. The time now is 01:54 PM.