Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should i use sed and when should i use awk [closed]

Tags:

linux

sed

awk

Last few days and before that i ahve been asking questions about sed and awk. This happens everytime that when i ask awk question people say to use sed and when i ask sed question people say to use awk.

can anyone give me some examples on where i can use each one.

To me , awk was used to presentation and reporting no substitution and sed was used where something needs to replaced.

But still many people gave me examples where substition is done via awk with print commnad.

Now i am really confused what are the uses cases of that.

As an example. suppose

I have source code file in C++ and i want to add some code before particular pattern.

what should i use sed or awk. Pattern can span multiple lines as well

like image 539
user175386049 Avatar asked Jan 09 '13 06:01

user175386049


People also ask

Should I use sed or awk?

AWK, like sed, is a programming language that deals with large bodies of text. But while people use sed to process and modify text, people mostly use AWK as a tool for analysis and reporting.

Which is faster awk or sed?

Generally I would say grep is the fastest one, sed is the slowest. Of course this depends on what are you doing exactly. I find awk much faster than sed . You can speed up grep if you don't need real regular expressions but only simple fixed strings (option -F).

What is difference between awk and sed command in Linux?

awk – this command is a useful and powerful command used for pattern matching as well as for text processing. This command will display only the third column from the long listing of files and directories. sed – this is a powerful command for editing a 'stream' of text.

Can I use awk with sed?

sed and awk are two different executables. To use them together, you will have to "pipe" the output of one as input of another. For that matter you can pipe output of any process to input of any other process.


1 Answers

The link in the comment is quite valuable. I would add some lines from my personal feeling of sed/awk usage.

I emphasis that, many text processing tasks could be done by both sed and awk. The text below are just my personal feeling.

  • if I want to do text processing, and write back to the original file without generating temp file, sed would win.

  • I like the sed's address, it could shorten the codes a lot. e.g. 3,6{s/a/b/}, 1~2{s/f/b/} or 3~5{s/f/b/}

  • if you want to pass the matched parts to an external commands/programs, awk is easier than sed (Gnu sed). If you want to have some fun and give it a try with sed, you could play a game: match a text, and pass parts (\1,\2 etc) to different sed. that is, sed nesting. after 3 times, I am lost... :)

  • if the text is somehow "column" based, in 99% case, awk won't let you down.

  • I would choose awk if I want to do different text processing on multi-files. e.g, {001-003}.txt different substitution requirements.

  • awk would be good at join-like processing among multi-files

  • if the logic needs some structure like if-else, for loop, while, number calculation etc, I would go with awk. though can sed in many cases do the job as well.

  • both sed and awk don't support Perl Regex. That's a pity.

  • So basically, I think awk is more flexible and straightforward than sed. If the task is relatively simple or there are well-known magic sed solutions (check http://sed.sourceforge.net/sed1line.txt there are many magic one-liners), I would go with sed. otherwise I would think awk first.

Both sed and awk are very powerful text processing tools, there are many text processing tools in linux/unix box, comm, cut, join, paste, sort, uniq... there are many option parameters of them. I am lazy and cannot remember all of them, just those common used ones. But If I know how to work with awk, sed and grep. 90% text processing under linux could be done. If awk, sed and grep cannot do the job, I would turn to a programming language. e.g. python.

well I am not a perl guy.

is there something that I forgot?

my 2cents, hope it helps.

like image 178
Kent Avatar answered Nov 03 '22 00:11

Kent