Practical grep commands examples useful in real-world debugging

While troubleshooting any issue,  log analysis is the most important step.  Mostly the log files capture enormous amount of information, and reading those becomes a difficult and time consuming task. In our daily debugging we need to analyze logs files of various products.

 Analyzing the logs to isolate and resolving the issues, can be complex and requires special debugging skills which are gained through experience or by god’s grace .  During debugging we might need to extract some data from the log files, or we need to play with a log file which can not be done by just reading through the log file line by line , there is need for special commands to reduce the overall efforts and provide the specific information we seek.

There are many commands in linux which are used by debuggers like grep, awk, sed, wc, taskset, ps, sort, uniq, cut, xargs etc .

In this blog we will see some examples of practical usage of <strong>grep</strong>,  useful in real world debugging&nbsp; in Linux . The examples which we will see in this blog are super basic but very useful in real life which a beginner should read to enhance the debugging skills

Grep (global search for regular expression and print out)</strong> is a linux command searches a file for a given pattern, and displays the lines which match the pattern.  The pattern is also referred to as regular expression.

Let’s Go to the Practical Part.

Lets say we have a file “”file1.log””, which has following lines.

root@localhost playground]# cat file1.log
hello
i am sahil
i am software engineer
Sahil is a software engineer
sahil is a software engineer

Search the lines which contains some particular word

root@localhost playground]# grep 'sahil' file1.log
i am sahil
sahil is a software engineer

Search number of lines matched for a particular word in a file

grep -c 'sahil' file1.log
2

Another way :

grep 'sahil' file1.log | wc -l
2

Search all the lines which contains some word (case insensitive)

root@localhost playground]# grep -i 'sahil' file1.log
i am sahil
Sahil is a software engineer
sahil is a software engineer

Search the lines in which either of two words are present in a file

root@localhost playground]# grep 'sahil|software' file1.log
i am sahil
i am software engineer
Sahil is a software engineer
sahil is a software engineer

Search lines in which two words are present

root@localhost playground]# grep 'sahil' file1.log | grep 'software'
sahil is a software engineer

Search lines excluding some word

root@localhost playground]# grep -v 'sahil' file1.log
hello
i am software engineer
Sahil is a software engineer

Exclude words case insensitively

root@localhost playground]# grep -iv 'sahil' file1.log
hello
i am software engineer

Search the lines that start with a string

root@localhost playground]# grep '^sahil' file1.log
sahil is a software engineer

Search the lines that end with a string

grep 'engineer$' file1.log
i am software engineer
Sahil is a software engineer
sahil is a software engineer

Getting n number of lines after each match

root@localhost playground]# grep 'hello' file1.log
hello

root@localhost playground]# grep -A 1 'hello' file1.log
hello
i am sahil

root@localhost playground]# grep -A 2 'hello' file1.log
hello
i am sahil
i am software engineer

Getting n number of lines before each match

root@localhost playground]# grep 'i am sahil' file1.log
i am sahil

root@localhost playground]# grep -B 1 'i am sahil' file1.log
hello
i am sahil

root@localhost playground]# grep -B 2 'i am sahil' file1.log
hello
i am sahil

in the second case only one line is printed as it is the only line before our pattern

Get n lines after and m lines before every match

root@localhost playground]# grep -A 2 -B 1 'i am sahil' file1.log
hello
i am sahil
i am software engineer
Sahil is a software engineer

Get some word in more than one file in current directory

For this purpose we will assume we also have a second file “”file2.log”” in the same directory

root@localhost playground]# cat file2.log
hello
i am sahil
i am tech blogger
Sahil is a tech blogger
sahil is a tech blogger

Grep can be used to search in more than one file or within a directory

root@localhost playground]# grep 'sahil' file1.log file2.log
file1.log:i am sahil
file1.log:sahil is a software engineer
file2.log:i am sahil
file2.log:sahil is a tech blogger

Grep some word in all files in current directory

root@localhost playground]# grep 'sahil' *
file1.log:i am sahil
file1.log:sahil is a software engineer
file2.log:i am sahil
file2.log:sahil is a tech blogger

Check how many lines matched in each file

root@localhost playground]# grep -c 'sahil' *
file1.log:2
file2.log:2
file.log:0

Note : the above output signifies, we have a third file in the directory “”file.log””, but it has no lines that have a word “”sahil””

Grep using regular expression

Regular expressions are patterns used to match character combinations in strings

Suppose the content of files are as follows

root@localhost playground]# cat file3.log
time taken by api is 1211 ms
time taken by api is 2000 ms
time taken by api is 3000 ms
time taken by api is 4000 ms
time taken by api is 50000 ms
time taken by api is 123 ms
time taken by api is 213 ms
time taken by api is 456 ms
time taken by api is 1000 ms

Now suppose we want to grep all the lines in which time taken by any api is more than 1 second or more than 1000 ms , it means it should have minimum 4 digit number, grep command for this will be as follows

root@localhost playground]# grep -P '[0-9]{4} ms' file3.log
time taken by api is 1211 ms
time taken by api is 2000 ms
time taken by api is 3000 ms
time taken by api is 4000 ms
time taken by api is 50000 ms
time taken by api is 1000 ms

If we want to get 5 digit number

root@localhost playground]# grep -P '[0-9]{5} ms' file3.log
time taken by api is 50000 ms

Recursively search in a directory and sub directories

root@localhost playground]# grep -R 'sahil' .
./dir1/file.log:i am sahil
./dir1/file.log:sahil is a software engineer
./file1.log:i am sahil
./file1.log:sahil is a software engineer
./file2.log:i am sahil
./file2.log:sahil is a tech blogger

All above are basic use cases of grep . One can mix all the command options of grep to achieve the complex use cases and one can also mix different grep commands using pipe operator to achieve complex use cases

In future blogs we will explain some complex use cases and example how to achieve that using linux commands which can ease logs debugging.

Stay Tuned . . .

See Original Posts at hello-worlds

and at medium

and at dzone

..

Author

Sahil Aggarwal
Senior Software Engineer 3/Tech Lead at Ameyo

I am a Tech Professional working in IT industry from 10 years . I have worked on many technologies like java, databases, linux, netrowking, security and lot more . I generally believe in taking outside-in approach to any new product you are looking for . This behaviour helped me lot in debugging issues of new products or even issues of any other system throughout by journey. I like to write tech blogs and I have my personal blog sites also, I write on medium and dzone also . I am very adaptive working between Individual Contributor and as a Leader/Manager. Apart from technology, I like eating fast and junk food . I also like to listen and write shyari.

10 best entry level tech jobs and skills required to get hired

In today’s world of technology, IT is the most flourishing field. In fact, industry body NASSCOM has recently pointed out that before the pandemic, Indian IT companies were reporting annual growth rates of around 6-7%, which have now increased to around 15-20%. [1]

IT industry is one sector which is growing very fast worldwide. It helps to provide jobs and opportunities for professional success. It can really be a source of quick employment, opportunities in all sectors, numerous career paths, and well-paid salaries. Here we describe the 10 entry-level IT positions while you search for a job.

It is important to note that some roles do provide entry-level positions but some of these are specialized roles and generally companies do look for knowledge of systems and networks as well as prior experience, so it is a good idea to start with roles 1-5 and then opt for the other roles based on your interests and experience.

1. Help Desk Technician

Help Desk technician forms the first level of IT support, a help desk technician provides technical support/assistance, whether on the phone /in-person/ or remotely. When an employee/computer user (within the organization or at a client’s end) has problems with hardware, software, or a network, they call on a help desk to assist. As a help desk technician, your main responsibility is to receive the requests and be responsible for responding to email, chat, or phone queries, and offering technical support to customers using the tools available.

The title for the role may vary, help desk technicians may also be called, help desk analysts, desktop support, service desk technicians, etc. collectively referred to as help desk positions. Many IT professionals start their career in help desk roles, it is a good position to consider if you don’t know where to start in IT, you get the necessary exposure to the domain and as you gain experience and knowledge you can further decide your career path.

Skills required: Help desk roles need basic IT / Troubleshooting skills, normally organizations don’t as for certifications. Even non-IT graduates are eligible for these roles. You will need good communication and process handling skills for these roles.

2. Technical Support / IT Technician

Technical Support roles are similar to help desk technician in way that they also support the technical issues faced by end users, but while help desk job is to support user issues, technical support works on actual technical issues that cause the problem. Technical support is a trained person who has the knowledge of operating systems and networks and is able to troubleshoot the issues that arise due to them.

IT technicians can also be called IT associates, IT analysts, or IT specialists. Some companies may have IT technicians do help desk work as well. This position is a very good start to enter in IT sector jobs and later when you gain experience, you can enter other fields such as network administrator, DBA, or IT security specialist.

Skills required: Technical Support / IT technician roles need to be from IT background, companies seek good knowledge of operating systems, Networking, and in some cases SQL as well. Good communication skills and troubleshooting skills are also needed for this role.

3. Application Support Engineer

Application support engineer role is similar to IT Technician, as they also need to work on technical issues, but in this case in addition to the general operating system and network issues they actually support the applications issues as well which is a product of the company or for which company offers the services.

Here in addition to the operating system and network knowledge, additional knowledge on SQL, HTML, and Scripting may also be required, the organizations also provide product-specific training to those who join in these roles. Similar to IT Technician, this position is a very good start to enter in IT sector jobs as this provides the necessary exposure and once you gain experience, you can enter other fields such as network administrator, DBA or IT security specialist.

Skills required: Companies look for candidates from IT backgrounds with good knowledge of the operating system, Networking, and SQL. Apart from these companies also seek knowledge in web technologiesGood communication skills and troubleshooting skills are also needed for this role.

4. System Administrator

System Administrator is a very important person in the organization as he is responsible for the smooth working of the IT systems on day-to-day basis. He looks after servers, network performance, security tools, operating systems etc. Unlike tech support roles system administrator roles are more specialized in ensuring systems within the organization function smoothly. Their roles can overlap with the Network Administrators.

System Administrator’s should have full knowledge about hardware and software and have plans to resolve any issue. They should also have good communication with management employees so that they can reach out to them whenever required. This role also provides opportunities to grow, as you gain experience and expand your skill set you can advance to roles like systems engineer or systems architect. 

Skills required: Companies look for candidates from IT backgrounds with experience and sound knowledge of networking. Companies look for certifications and associated degrees. Good communication skills and troubleshooting skills are also needed for this role.

5. Network Engineer / Network Support Engineer

Network Engineer / Network support engineer role is similar to technical support role, but the focus here is limited to networking knowledge and troubleshooting of network / “network device” related issues, In a way, this is a more specialized role.

Here you need limited knowledge of operating systems, and hardware but sound knowledge of networking concepts and specialized certification for any of network hardware vendors like cisco is needed., and they support the technical issues in the network or network devices.

Skills required: Companies look for candidates from IT backgrounds with experience and sound knowledge of networking. Companies look for certifications and associated degrees. Good communication skills and troubleshooting skills are also needed for this role.

6. Database Administrator

Database Administrator is responsible for the business aspects of the organization. They manage and maintain company databases. They monitor the database performance and take corrective action, address any issues related to the database, and ensure security and backup of databases as per company policies.

It is a specialized role generally companies look for experience candidates, basic knowledge of operating systems, networking, etc is also required as databases are also a part of the systems. Database is offered by different vendors ( MS SQL, Oracle etc or NoSQL databases or opensource database like mysql and postgresql). Companies either look for skills in a specific database technology or in some cases general skills across databases.

Skills required: Again, this is a specialized role and companies do look for experience and IT background with solid knowledge of systems, operating systems, servers, cloud technologies, and networking. Apart from this companies look for the ability to program in high-level languages like python, knowledge of microservice architecture (Docker, Kubernetes) ability use Devops / CICD tools

8. Information Security Analyst

In this era when everything is online and tech-based, a lot of companies have faced the problem of breaches. An Information Security Analyst is a person who helps to protect you from such issues.

Their job is to detect the different aspects of security like network security and software security. He also makes sure that data is available to the authorized people in the company. They conduct security audits, are responsible for conducting security assessment tests, and coordinate with key stakeholders to ensure remediation activities are performed.

Skills required: This is a specialized role and companies do look for experience and IT background with strong knowledge of systems, operating systems, servers, cloud technologies, and networking. Apart from this companies look for a sound knowledge of penetration testing principles, tools and techniques, knowledge system and application security threats and vulnerabilities, and skills in scripting languages.

9. Cloud Engineer

Cloud Engineer is someone who deals with cloud computing. This includes managing, planning, architecting, and monitoring cloud systems. He should also know how to implement the best practices to deploy an application on the cloud.

Sometime back cloud computing was considered a luxury but today it has become a necessity. There is a lot of scope and high salary in this field.

Skills required: This is a specialized role and companies do look for experience and companies not only look for skills in cloud technologies (AWS, Azure, Oracle) but also the knowledge of operating systems, networking, and fundamentals of hardware (cpu, storage, RAM etc)

10. Web Developer / Software Developer / Mobile Developer

Requirements: Ability to program / Experience with programming languages. Associate, bachelor’s, or master’s degrees may be requested.

Web Developers / Software Developers need programming skills, and depending upon the role the expertise may wary. Web developer roles in generally attributed to those building websites, whereas software developers build the applications/ software products and applications, with a lot of businesses having their web portals as their prime products these roles also overlap.

Skills required: Companies look for skills in front end technologies, back end technologies or full stack developers, with proficiency in programming languages. For web development, you will need skills in HTML, CSS, and Javascript while for backend you will need skills in python, Ruby or PHP. For software development, you will need skills in programming languages like Java, C++ etc . While for mobile development you will need skills in specific mobile app development languages

References
1 – https://www.business-standard.com/article/companies/indian-it-crosses-200-bn-revenue-mark-hits-227-bn-in-fy22-nasscom-122021500828_1.html