programing tip

AWK를 배울 이유가 아직 있습니까?

itbloger 2020. 8. 20. 07:56
반응형

AWK를 배울 이유가 아직 있습니까?


나는 문제에 대한 올바른 솔루션을 사용하기를 좋아하기 때문에 구식 도구를 포함하여 새로운 도구를 지속적으로 배우고 있습니다.

그럼에도 불구하고 그들 중 일부를 배울 이유가 있는지 궁금합니다. awk예를 들어 나에게 흥미이지만, 간단한 텍스트 처리를 위해, 내가 사용할 수있는 grep, cut, sed, 등 복잡한 것들에 대한, 내가 파이썬 갈 것이다있다.

강력하고 편리한 도구가 아니라는 의미는 아닙니다. 그러나 새로운 도구를 배우려면 시간과 에너지 가 필요하므로 그만한 가치가 있습니까?


나는 당신이 처한 환경에 달려 있다고 생각합니다. 당신이 * nix 사람이라면 아는 awk것이 좋은 것입니다. 거의 모든 * nix에서 찾을 수있는 유일한 다른 스크립팅 환경은 sh. 따라서 grep, sed,awk이 현대 주류 linux배포판에서 확실히 대체 될 수 있지만 , 좀 더 이국적인 시스템으로 이동할 때 조금만 아는 awk것이 Real Handy가 될 것입니다.

awk텍스트 처리 이상의 용도로도 사용할 수 있습니다. 예를 들어, 내 상사 중 한 명이 천문학 코드를 작성 awk합니다. 이것이 그가 얼마나 오래된 학교 이고 대단한지 입니다. 당시에는이 작업에 가장 적합한 도구였습니다. 지금은 저와 같은 그의 학생들이 파이썬을 사용하는 것과 그렇지 않은 것에도 불구하고 그는 자신이 잘 알고있는 것을 고수하고 있습니다.

마지막으로, 전 세계에 많은 오래된 코드가 있습니다. 조금이라도 awk아프지 않을 것이라는 것을 알고 있습니다. 그것은 또한 당신을 더 좋게 만들 것입니다 * nix 사람 :-)


awk의 기초를 빨리 배우면 명령 줄에서 놀라운 일을 할 수 있습니다.

그러나 awk를 배우는 진짜 이유 는 저자 Aho, Kernighan, Weinberger가 멋진The AWK Programming Language 를 읽을 변명을 가지기 위해서 입니다. 이름에서, 그것은 단순히 당신에게 어색함을 가르쳐 준다고 생각할 것입니다. 사실 그것은 시작에 불과합니다. 문자열 조작을 쉽게하는 간결한 스크립팅 언어를 사용하면 해결 될 수있는 방대한 문제를 시작합니다. awk는 첫 번째였습니다. 독자에게 데이터베이스, 파서, 인터프리터 구현 방법을 가르치는 과정이 진행됩니다. , 그리고 (메모리가 나에게 도움이된다면) 작은 프로젝트 특정 컴퓨터 언어를위한 컴파일러! awk를 사용하여 예제 운영 체제도 프로그래밍했다면이 책은 컴퓨터 과학에 대한 상당히 완전한 설문 조사 소개가되었을 것입니다!

원본 C 언어 책처럼 명확하고 간결한 것으로 유명하며, 친근한 기술 문서 작성의 훌륭한 예이기도합니다. 인덱스조차도 장인 정신입니다.

Awk? 알고 있다면 가끔 명령 줄에서 사용 하겠지만, 더 큰 것은 파이썬과 같은 것이 액세스 할 수있는 인터넷과 시스템의 더 넓은 기능에 액세스 할 수없는 갇힌 느낌이들 것입니다. 하지만 그 책? 당신은 항상 당신이 그것을 읽고 기뻐할 것입니다!


내가 사용하는 유일한 이유 awk는 자동 분할입니다.

awk '{print $3}' < file.in

에서 공백으로 구분 된 세 번째 필드를 인쇄합니다 file.in. 다음보다 약간 쉽습니다.

tr -s ' ' < file.in | cut -d' ' -f3

파일에 열 / 필드 가 포함되어 있으면 awk가 좋습니다 . 여러 열 파일의 특정 열을 처리 / 분석 할 때 사용합니다. 또는 특정 열을 추가 / 삭제하려는 경우.

예 :

awk -F \t '{ if ($2 > $3) print; }' <filename>

탭으로 구분 된 파일의 두 번째 열 값이 세 번째 열 값보다 큰 경우에만 인쇄됩니다.

물론 Perl이나 Python을 사용할 수 있지만 awk는 간결한 한 줄 명령으로 훨씬 더 간단하게 만듭니다.

또한 awk를 배우는 것은 매우 저렴합니다. awk 기본 사항을 한 시간 이내에 배울 수 있으므로 다른 프로그래밍 / 스크립팅 언어를 배우는 것만 큼 많은 노력이 필요하지 않습니다.


HTML을 다룰 때 가끔 AWK를 사용합니다. 예를 들어 다음 코드는 테이블을 csv 파일로 변환합니다.

BEGIN {s=""; FS="n"}
/<td/ { gsub(/<[^>]*>/, ""); s=(s ", " $1);}
/<tr|<TR/ { print s; s="" }

화면 스크래핑을 할 때 좋습니다. 사실, 내가 AWK를 좋아하는 경우가 될 수 있습니다. 문제에 대한 잘못된 솔루션을 너무 빨리 구축 할 수 있기 때문 입니다. :) 더 많은 예제 . Jon Bentley의 사랑스러운 Programming Pearls 에서도 언급되었습니다 .


이 질문을 한 지 6 년이 지나면 이제 확실하게 대답 할 수 있습니다. 아니오, awk를 배우는 것은 그만한 가치가 없습니다.

기본 작업은 처리되지만 기본 bash 명령 또는 GUI 도구도 쉽게 처리됩니다. 더 복잡한 작업은 Python (fav 또는 mine) 또는 Ruby와 같은 최신 동적 언어를 사용하여 쉽게 다룰 수 있습니다.

많은 작업 (웹, 관리자, 데이터 크 런칭, 자동화 등)에 도움이되는 mordern 스크립팅 동적 언어를 확실히 배워야합니다. 그리고 그렇게함으로써 awk와 같은 도구를 배우는 것은 전혀 쓸모가 없으므로 매달 기껏해야 몇 초를 절약 할 수 있습니다.


나는 자주 awk를 사용합니다. 파이프 라인 중간에서 매우 간단한 텍스트 셔플 링에 적합합니다. 그것은 전혀 필요하지 않은 것과 Perl / Python / 무엇이든 채찍질 할 필요가있는 것 사이에서 매우 좁은 틈새 시장을 채 웁니다.

I wouldn't advise you spend a whole lot of time on it, but it might come in handy to know the basics of the syntax -- at least enough that you can consult the manual quickly should you ever want to use it.


Most awk one liners can be achieved with Perl one liners - if you choose to get into a Perl one liner mindset. Or, just use Perl three liners :)

If you're maintaining shell scripts written by someone who liked awk, then clearly, you're going to need to learn awk.

Even if there's no practical need, if you already know regex it won't take long to pick up the basics, and it's fun to see how things were designed back then. It's rather elegant.


If you already know and use sed, you might as well pick up at least a bit of awk. They can be piped together for some pretty powerful tricks. Always impresses the audience.


Computerworld recently did an interview with Alfred V. Aho (one of the three creators of AWK) about AWK. It's a quite interesting read. So maybe you'll find some hints in it, why it's a good idea learn AWK.


Learning AWK was invaluable for me in my last contract working on an embedded Linux system on which neither Perl nor most other scripting languages were installed.


It's useful mostly if you have to occasionally parse log files for data or output of programs while shell scripting, because it's very easy to achieve in awk that that would take you a little more lines of code in python.

It certainly has more power than that, but this seems to be tasks most people use it for.


Of course: I'm working in an environment where the only available languages are: (some shity language which generates COBOL, OMG, OMG), bash (old version), perl (I don't master it yet), sed, awk, and some other command line utilities. Knowing awk saved me several hours (and had generated several text processing tasks from my collegaues - they come to me at least three times a day).


awk has a very good ratio utility/difficulty, and "simple awk" works in every Unix/Linux/MacOS (and it can be installed in other systems too).

It was designed in Golden Age when people hated typing, so scripts can be very, very short and fast to write. I will try to instal mawk, a fast version, allegedly it accelerates the computation about 9 times, awk/gawk is rather slow, so if you want to use it instead of R etc. you may want mawk.


I'd say it's probably not worth it anymore. I use it from time to time as a much more versatile stream editor than sed with searching abilities included, but if you are proficient with python I do not know a task which you would be able to finish that much faster to compensate for the time needed to learn awk.

The following command is probably the only one for which I've used awk in the last two years (it purges half-removed packages from my Debian/Ubuntu systems):

$ dpkg -l|awk '/^rc/ {print $2}'|xargs sudo dpkg -P

Nope.

Even though it might be interesting, you can do everything that awk can do using other, more powerful tools such as Perl.

Spend your time learning those more powerful tools - and only incidentally pick up some awk along the way.


I'd say there is. For simple stuff, AWK is a lot easier on the inexperienced sysadmin / developer than Python. You can learn a little AWK and do a lot of things, learning Python means learning a whole new language (yes, I know AWK is a language is a sense too).

Perl might be able to do a lot of things AWK can do, but offered the choice in this day and age I would choose Python here. So yes, you should learn AWK. but learn Python too :-)


Now that PERL is ported to pretty much every significant platform, I'd say it's not worth it. It's more versatile than sed and awk together. As for auto-splitting, you can do it in perl like this:

perl -F':' -ane 'print $F[3],"\n";' /etc/passwd

EDIT: you might still want to get somewhat acquainted with awk, because some other tools are based on its philosophy of pattern-based actions (e.g. DTrace on Solaris).


I work in area the files are in column format. So awk is invaluable to me to REFORMAT the file so different software can work together. For non IT profession, using awk is enough and perfect. Now a day, computer speed is not an issue, so I can combine awk & unix to pipe many 1 liners command into a "script". With Awk search by field and record, I use it to check the file data very fast, instead of "vi" to open a file. I have to say awk capability brought joy to my job specially, I am able to assist co-worker to sort things out quickly using awk. Amazing code to me.


I was recently trying to visualize network pcap files logging a DOS attack which amounted to over 20Gbs. I needed the timestamp and the Ip addresses. In my scenario, AWK one-liner worked fabulously and pretty fast as well. I specifically used AWK to clean the extracted files, get the ip addresses and the total packet count from those IP addresses within grouped span of time. I totally agree with what other people have written above. It depends on your needs.


awk is a powertool language, so you are likely going to find awk being used somewhere if you are an IT professional of any sort. If you can handle the syntax and regular expressions of grep and sed then you should have no problem picking up awk and it is probably worthwhile to.

Where I've found awk really shine is in simplifying things like processing multi-line records and mangling/interpolating multiple files simultaneously.


One reason NOT to learn awk is that it doesn't have non-greedy matches in regular expressions.

I have an awk code that now I must rewrite only because I suddenly debugged that there is no such thing as non-greedy matches in awk/gawk thus it can't properly execute some regexes.


It depends on your team mates and you leader and the task you are working on.

if( team mates and leader ask to write awk ){
  if( you can reject that){
    if( awk code is very small){
      learn little just like learn Regex
    }else{
      use python or even java
    }
  }else{
    do as they ask
  }
}

I have been doing some coding in python at present. But I still do not know it well enough to use easily for simple one off file transformations.

With awk I can quickly develop a one line piece of code on the unix command line that does some pretty swish transformations. Every time I use awk, the piece of code I write will be disposable and no more than a few lines long. Maybe an "if" statment and "printf" statement here or there on the one line.

I have never written a piece of code that is more than 10 lines long with awk. I saw some such scripts years ago.

But anything that required many lines of code, I would resort to python.

I love awk. It is a very powerful tool in combination with sed.

참고URL : https://stackoverflow.com/questions/107603/is-there-still-any-reason-to-learn-awk

반응형