HomeОбразованиеRelated VideosMore From: theurbanpenguin

Using AWK to format output

655 ratings | 105933 views
More videos like this online at http://www.theurbanpenguin.com AWK is a very powerful text processing tool in Unix and Linux. many people may use SED but AWK just scares them. Like anything it is all about confidence and how familiar you are with the tools. In this video we show some simple formatting techniques to parse and print the passwd file. Display selected columns with correct size and header information.
Html code for embedding videos on your blog
Text Comments (53)
Abhay Deshpande (1 month ago)
Hi sir, this tutorial is very useful. i am trying for for((i=1;i<=$count;i++)) do var=`awk 'NR==$i {print NR,$0}' file.txt` echo "$var" done But it will not work. Need your help to solve this sir.......
Abhay Deshpande (1 month ago)
Hi sir, this tutorial is very useful. i am trying for for((i=1;i<=$count;i++)) { var=`awk 'NR==$i {print NR, $0}' , but
Beastie06 (3 months ago)
Good tutorial - and it's not your fault - but who in the world can remember all that?
CessnaacePVP (4 months ago)
How would you pull individual pieces of data if they are all eliminated by spaces?
DotBowder (6 months ago)
This video is very informative. I would suggest users watch at 1.5x speed.
Anis Dhouieb (6 months ago)
Very good tutorial Mr. Mallett !!! Thank you again !
ibobsie (9 months ago)
he is actually moving incredibly fast too fast actually...these peopel just dont uderstand pacing
theurbanpenguin (9 months ago)
I am sorry that you think I spoke to fast. Others have commented it is too slow. I hope that you appreciate that it is difficult to please everyone. If you have questions though feel free to ask
90hijacked (10 months ago)
How would one progromatically define cell width with printf? baz=foo; echo bar | awk -v baz=$baz '{print baz $1}' returns foobar a=10; echo get | awk -v a=$a '{printf "%{a}s %5s", $1, "help"}' fails
90hijacked (10 months ago)
I've been messing around some, i think i've figured it out here is a paste https://pastebin.com/rUjD4j67 Also, Thank you for sharing the knowledge. really appreciate what you're doing here
90hijacked (10 months ago)
nah that's not what i meant disclaimer: I'm studying as a hobby, you may find this painfully ignorant Consider parsing output of the ps command, in the simplest form ps aux | sort -rk4 | head -n 10 | tr -s " " | cut -d " " -f 4,11-12 Now say i wanted to display top MEM and CPU consuming programs running alongside abc(){ps aux | tail -n +2 | sort "-rk"$1 | head -n $((`tput lines` / 2 - 1)) }; abc 3 > /tmp/abc3; abc 4 > /tmp/abc4; awk 'NR==FNR{a[FNR]=$3;b[FNR]=$4;c[FNR]=$11;next}{printf"%80s %-5s %-5s %1s %5s %5s %-80s\n",c[FNR],b[FNR],a[FNR],"|",$3,$4,$11}' /tmp/abc3 /tmp/abc4 ( making awk read files in /tmp/ because piping `((something)&(something2)) | into awk treats it as the same file ( NR and FNR are equal ) ) # edit: This is slightly unreadible, here's a paste https://pastebin.com/cDdi2kPS Now how would i align this gadget to the middle of the screen? I've tried passing "-v myvar=`tput cols`" but i fail to act on this variable in printf because the field is quoted Someone recommended something along the lines of sprintf or using a different syntax,( That being printf("%*$",myvar,$1) But doing so prevents me from specifying other field widths in there )
theurbanpenguin (10 months ago)
awk -F: '{printf "%18s\n", $1}' /etc/passwd
youyi qin (1 year ago)
Hassan Ali (1 year ago)
This was great! Thank you!
Alexandre Formagio (1 year ago)
Thank you, amazing example to ilustrate the use of awk.
Philthy Phil (1 year ago)
Nice tutorial man, thanks bruva
lmind2 (1 year ago)
Great stuff Mr Mallett. Please reconsider your choice of colors while in vim so it's easier on folks trying to contrast dark blue against black. Perhaps :colo desert instead of :background dark?
iMPRE7ed (1 year ago)
very much useable. thanks!
arathnas (1 year ago)
Thanks a lot theurbanpenguin. Wonderful explanation
Michael Huynh (1 year ago)
thank you for taking your time and speaking slowly. you are being very considerate for your audience.
Learn Linux (1 year ago)
Before this video, I just KNOW you're going to go through and do something.
Vagelis Prokopiou (1 year ago)
Tyrone Slothdrop (1 year ago)
Thank you for all the high-quality videos.
Adelly Ganesh (1 year ago)
Excellent explanation! Great video sir
Rajeev Pandit (1 year ago)
Thanks a lot Mr.theurbanpenguin for this great video. I also visited your website and it is superb site i have ever seen specially for Linux. Once again thanks a lot.
Getting to Data (1 year ago)
A good introduction to AWK. It's a bit intimidating, but this video clarifies pulling out fields into reports.
Aaron Goshine (1 year ago)
Great tuts.. but the dark red fonts on black background is not very readable especially when youtube add compress for low band width.
AcidRain (1 year ago)
Great Tutorial , Thanks mate :)
kHiZZz (2 years ago)
so nice
李豪斌 (2 years ago)
Great ! Thank you.
CriticalMass (2 years ago)
Would you also be able to sort this list alphabetically ?
Learn Linux (1 year ago)
3p1k5auc3 Using sort on its own sorts by the character's ASCII value.
3p1k5auc3 (1 year ago)
You could try piping the output through sort by printing out the header information first, then with a second awk command in the same script pipe that into sort: awk { [script to print the header] } awk{ [script that prints out formatted user info] } [filename] | sort or something to that effect.
discoveringmypath (2 years ago)
Nice video, very clear and easy to follow. Curious, how would I essentially do the same thing, but instead of picking the lines you specified 1-10, how would I display lines with a pattern (example: sh | bash ) to weed out the system accounts. great video! Thanks!
tractorbooty15 (2 years ago)
Well explained!!I have been trying to get to grips with this for ages.Excellent!!
Gregory Cave (2 years ago)
Thanks very much mate!
Juan2003gtr (2 years ago)
Suscribed...you are a great teacher and you make it fun in contrast to all the other linux freaks that make videos with boring slides.......
Phu Son Trinh (2 years ago)
thanks a lot Mr.theurbanpenguin, great tut.
theurbanpenguin (2 years ago)
mozayer (3 years ago)
Good but the color is really eye-pain
M W (3 years ago)
Great VIDEO! Help me alot
Andrew Weaver (3 years ago)
Thank you, this helped clarify a lot of what I was trying to learn in class.
Rich Santink (3 years ago)
Great video!  Just as a note %d as in C (which it borrows from),  represents integer (or whole number) values, rather than 'decimals' which are included with %f (floats).  :)
Harsh Kanakhara (3 years ago)
Thank you very much. Helped me a lot to understand awk in easy manner...!!! :-)
Lucifer 1776 (3 years ago)
Great Video!
suresh rai (3 years ago)
Syangjali Thito Magar (3 years ago)
Very helpful, tx ! 
Rohit Sachan (3 years ago)
awk is a nice command
Prashant Mishra (4 years ago)
Thank u. This really helped me to understand the formatting output of a script using awk.
Calvin Zhou (4 years ago)
How do you sort with out sorting the header?
Satish N (4 years ago)
Thank you. You have explained very nicely step by step usage of awk command with various options.
Vladimir Kuznetsov (5 years ago)
Yet, echo "set background=dark" >> ~/.vimrc
The Blockchain Gamer (1 month ago)
Why is the default colorscheme used when it's so unreadable???

Would you like to comment?

Join YouTube for a free account, or sign in if you are already a member.