PDA

View Full Version : Bash, Sed & Awk tutorials



Drenriza
April 8th, 2011, 12:44 PM
Hi all.

Anyone know some really good / professional / usefull bash, awk and sed tutorials. That are up to date. I have worked with the 3 "languages" or w/e they are, on and off. But would like to get more in debt with it.

And if anyone know of some good online tutorials, books, nuggets, others. That i'm all ears.

Thanks on advance.
Kind regards.

Grenage
April 8th, 2011, 12:51 PM
I've always found this (http://www.grymoire.com/Unix/Sed.html) resource to be very handy:

SeijiSensei
April 8th, 2011, 03:34 PM
For sed and awk, this is the Bible (http://oreilly.com/catalog/9781565922259).

Rubi1200
April 8th, 2011, 03:38 PM
I've always found this (http://www.grymoire.com/Unix/Sed.html) resource to be very handy:
Excellent link Grenage; thanks :-)

nothingspecial
April 8th, 2011, 03:54 PM
For bash

http://mywiki.wooledge.org/BashGuide

Check the faq and pitfalls section as well.

ghostdog74
April 9th, 2011, 03:09 AM
You only need to learn awk out of sed/awk. Forget about sed. With awk, you can do what sed does , and much more, because Awk is a programming language itself. If you want to get more in depth on awk, look at its manual. (see my sig)

jerome bettis
April 9th, 2011, 08:48 AM
^ i'll agree with that in a genral sense, awk is more powerful and can do everything sed can. But for a lot of smaller things, sed is far quicker. my personal favorite is

find /dir/ -iname "fileToSub*.txt" -exec sed -i -e 's/replace/new/g' {} \+

try to do that with awk. :p

OP, "advanced bash scripting guide" is good

set -x
set -e

ghostdog74
April 9th, 2011, 12:10 PM
^ i'll agree with that in a genral sense, awk is more powerful and can do everything sed can. But for a lot of smaller things, sed is far quicker. my personal favorite is

find /dir/ -iname "fileToSub*.txt" -exec sed -i -e 's/replace/new/g' {} \+

try to do that with awk. :p



don't forget, -exec is an option for the find command


find . -type f -name "file" -exec awk '{gsub(/replace/,"new")}1' "{}" > temp \; -exec mv temp "{}" \;


true, awk doesn't have in place file editing feature, but i just showed you it can be done with just awk as well... and don't forget, sed -i also creates temp files in the back end.

geirha
April 9th, 2011, 01:11 PM
find . -type f -name "file" -exec awk '{gsub(/replace/,"new")}1' "{}" > temp \; -exec mv temp "{}" \;



That will only work if there's at most one matching file. It's equivalent to


find . -type f -name "file" -exec awk '{gsub(/replace/,"new")}1' "{}" \; -exec mv temp "{}" \; > temp


This'll edit the files using awk.

find . -type f -name "file" -exec awk '{gsub(/replace/,"new"); print > FILENAME}' {} +

Oh and to the OP, for learning bash, stay away from the bash guides at tldp.org. Read the bash guide that nothingspecial posted. It's the only one that teaches good practice.

jerome bettis
April 9th, 2011, 06:01 PM
don't forget, -exec is an option for the find command


find . -type f -name "file" -exec awk '{gsub(/replace/,"new")}1' "{}" > temp \; -exec mv temp "{}" \;


true, awk doesn't have in place file editing feature, but i just showed you it can be done with just awk as well... and don't forget, sed -i also creates temp files in the back end.

i know it can be done, but look how much more work that was. thanks for helping me prove my point so well :lolflag:

bash/sed/awk are all worth learning. i've been meaning to look at some other shells one of these days zsh/ksh/csh .. ive seen some people doing even more powerful stuff over there. But i'm so used to bash it is a bit weird.

ghostdog74
April 10th, 2011, 04:01 AM
i know it can be done, but look how much more work that was. thanks for helping me prove my point so well :lolflag:

that was just a poor example.. You can see geirha's example. (@geirha's thanks,been dormant for a while and forgot about FILENAME). Sorry what point are you proving again?



bash/sed/awk are all worth learning.

when it comes to parsing files, bash (to call awk ) and awk is all you need. The rest of the tools that can read files -> wc, tr, sed, grep, cat, nl, head, etc one can chuck it in his toolbox and never use it. But for some, like tail, is sometimes indispensable due to its purpose. (although you can also use awk to simulate "tail")

Drenriza
April 12th, 2011, 07:25 AM
You only need to learn awk out of sed/awk. Forget about sed. With awk, you can do what sed does , and much more, because Awk is a programming language itself. If you want to get more in depth on awk, look at its manual. (see my sig)

If awk is a programming language, what is it designed to program? can it program an entire program, with a gui and such?

ghostdog74
April 12th, 2011, 10:07 AM
If awk is a programming language, what is it designed to program? can it program an entire program, with a gui and such?
Have you read the link "Effective awk" in my sig? awk is little language that provides you with the basics for programming, arrays, variables, functions, if/else, switch etc. What can it program ? anything that you can produce with those programming features! Except GUI...because it doesn't have that kind of library (not like fully featured ones like Python or Ruby). See here (http://awk.info/?awk100) for more on what awk can do

matt_symes
April 12th, 2011, 10:27 AM
Hi


For bash

http://mywiki.wooledge.org/BashGuide

Check the faq and pitfalls section as well.

+1 to this. I liked the site so much i put it in my sig. Well worth reading before any tldp documentation.

Kind regards

graphicsmanx1
November 1st, 2012, 11:30 PM
awesome glad I ran across this :popcorn: