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 |
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. |
Quote:
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. |
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??? |
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 |
Quote:
Quote:
[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 Quote:
|
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. |
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') 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. |
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 }' |
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. |