Home > Back-end >  Can we use awk, sed and grep simultaneously for string operation in linux
Can we use awk, sed and grep simultaneously for string operation in linux

Time:01-27

I got a question from an interview, can we use awk, sed, and grep simultaneously? I am not sure why and how but is there any possibility that we use all of them simultaneously to manipulate strings from the file?

CodePudding user response:

That makes absolutely no sense: you use awk, sed and grep in order to alter a string or to filter information from it, why would you even want to combine that at the same time? I believe the question is related to the usage of both commands in one single line like grep "INF" file.txt | awk '{print $1}', which is one single commandline, containing both grep and awk, first for filtering only the lines, containing "INF" and then only showing the first column. This however does not mean that you are executing both commands simultaneously: first you perform the grep and afterwards the awk.

CodePudding user response:

The correct answer is yes because awk has co-processes - you can start the other two from awk.

The interviewer wanted to hear you reason about why/why not this is a good idea and to understand your level of competence with these tools.

For example: in some scenarios this could be a really bad idea because you can potentially edit the file while reading from it, leading to confusing or unpredictable results.

Generally you don't need grep if you are using awk, and finally IMHO mixing sed and awk in a "simultaneous" script is poor form because it's potentially hard to understand what's being done

  • Related