How to use the grep command for better data retrieval

Automating the general performance of jobs via scripting is one thing we all strive to do as IT execs. But having it a stage further by employing grep provides a layer of granularity and universality to your scripts.

Image: iStock/HAKINMHAN

Interacting with data is the main function of any IT professional. Each individual purpose is exclusive and delivers with it certain difficulties, but at the heart of just about every part lies the very same simple premise: As IT, we interpret and answer to the facts we acquire and if it isn’t accurate, we take actions to right the knowledge stream. When we automate jobs based on scripts, it is ordinarily finished in response to identified parameters and provides a predetermined set of instructions to execute, thus automating repetitive responsibilities.

SEE: TechRepublic Premium editorial calendar: IT insurance policies, checklists, toolkits, and research for download (TechRepublic Top quality)

This is good and, I whole-heartedly recommend that IT pros automate exactly where feasible to optimize effectiveness and restrict downtime for all stakeholders. But what if you could execute commands or create scripts that would give information and facts and make alterations (execute instructions) dependent on the responses? You could automate responsibilities with a little bit of logic baked in to allow for the script to account for a range of possibilities  constrained only by your creativeness or script composing abilities.

Scripts like these may well be uncovered in several popular community forums or GitHub, and as prolonged as they are examined and confirmed to get the job done for your surroundings, you must absolutely apply them where by feasible. But if you happen to be far more of the do-it-yourself sort, would like to discover this arms-on, or simply cannot discover a appropriate, pre-authored option, may well I introduce the grep command to you?

Grep is natively found in Linux and macOS methods. It is also out there as an installable offer for Home windows.

What is grep?

According to the grep gentleman page in macOS, “The grep utility lookups any offered enter documents, selecting strains that match one particular or much more styles.” This signifies that grep in essence queries for data that matches a certain established of phrases or designs that you inform it to appear for. The enter for grep to look for may possibly arrive from files fed to the utility or is far more frequently employed in conjunction with the output of instructions that are piped into grep following execution to detect selected bits of data.

Why should really I use grep?

Other than slicing down, possibly dramatically, on manually exploring information or command outputs for distinct information or responses, as described in advance of, grep can be integrated in a chain of instructions with the output of a former command piped into the enter for grep to sift by means of. This makes it possible for for the output from grep to be piped into a subsequent command to execute a further command in opposition to, and so on. When mixed with other instructions in just a script, grep could be used to examine facts from just one file to establish if the command need to commence in 1 course or a different, chopping down on the range of scripts to preserve (and update).

SEE: Navigating information privacy (absolutely free PDF) (TechRepublic)

What are some examples of grep in action?

Beneath I am going to offer some genuine-world illustrations of grep in use within just scripts to build an knowing of how grep operates and highlight how effectively it performs with other commands. It really is up to you to see how it can be integrated to include universal functionality to your scripts.

1. Validate hash value for an update in advance of installation

In the instance under, we have a file termed “macOS_Update01.pkg” that we wish to put in but want to confirm the hash price to make guaranteed the file’s integrity is intact. We know the hash worth is “9aac9b799f3bb26da66f886024e1af58a1b4d3a7” and as is widespread exercise, we have a file named “hash.sha” with the SHA1 benefit saved in it. By operating the grep command underneath with the recognised hash value stored in the checksum file, the string will carry out a check on the values, evaluating them. If it matches, the Terminal will output the filename on-screen to verify integrity.

grep 9aac9b799f3bb26da66f886024e1af58a1b4d3a7 hash.sha 

2. Ascertain the software program model of macOS and export non-matched products

In this case in point, we are working a command to decide when the current functioning version of macOS is mounted on a group of equipment. The ones that do not match 11.1 will be exported to a list titled NeedsUpdating.csv for evaluate. The output of the initially command will be piped into grep to whittle down the data that matches the prerequisite.

sw_vers -productVersion | grep -v 11.1 > "NeedsUpdating.csv" 

Also see

Source link

You may also like