UrbanPro

Learn Unix Shell Scripting Training from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

In the following snippet: /xyz.txt 404 /abc.txt 506 /pqr.txt 404 /xyz.txt 404 /abc.txt 505 /abc.txt 406 /abc.txt 306 /xyz.txt 206 How to write a bash one liner so that the unique text files are counted? For the above snippet the result has to be like: 3 /xyz.txt 4 /abc.txt 1 /pqr.txt Can this scenario be tried in Perl one liner too?

Asked by Last Modified  

Follow 1
Answer

Please enter your answer

Experienced in UNIX Shell scripting and core java

Hi Devi, Your while loop solution will not work for all the scenarios. 1. U have not used sort before uniq so in case of unsorted data, the code will not give proper output. 2. Actually we should avoid using while loop in pipe sequence since piping spawns a new shell every time, if you use or assign...
read more
Hi Devi, Your while loop solution will not work for all the scenarios. 1. U have not used sort before uniq so in case of unsorted data, the code will not give proper output. 2. Actually we should avoid using while loop in pipe sequence since piping spawns a new shell every time, if you use or assign any variable some value inside that while loop, it's scope will only be inside the while loop even if the variable is assigned some initial value before the while loop. U cannot refer the new value of the variable outside the while loop. Since the shell in which while executed would be destroyed once the loop completes. read less
Comments

Trainer

Hi Rahul...I agree with you. Instead of using while loop it is better we use sort, uniq or awk commands for this scenario. Thanks for sharing such a good information and correcting me.
Comments

Python and Unix Professional(10 years IT Exp)

$ awk '{print $1}' sample |sort |uniq -c 4 /abc.txt 1 /pqr.txt 3 /xyz.txt Above command will definitely solve your task. Thanks, Umesh.
Comments

Tutor for Btech IT

nawk '{print $1}' filename|sort |uniq -c
Comments

Experienced in UNIX Shell scripting and core java

I consider that the above file names are in a text file say files.txt. Now to get the unique file count awk '{print $1}' files.txt | sort | uniq -c
Comments

UNIX Scripting Expert

The command should be like below: $ awk '{print $1}' sample |sort |uniq -dc uniq -dc will give you duplicate count .
Comments

Trainer

Mukesh...it has to be in descending order. So, the following command will work: sort -r|uniq -c Thanks for replying.
Comments

Trainer

It can be done with while loop as well, I took the lines from a log file having the following lines: 2015-05-11 23:36 /home.html 200 OK 2015-05-11 23:37 /next2.html 200 OK 2015-05-11 23:37 /xyz3.html 404 Not found 2015-05-11 23:37 /xyz3.html 405 OK Then the following also can be used: cut...
read more
It can be done with while loop as well, I took the lines from a log file having the following lines: 2015-05-11 23:36 /home.html 200 OK 2015-05-11 23:37 /next2.html 200 OK 2015-05-11 23:37 /xyz3.html 404 Not found 2015-05-11 23:37 /xyz3.html 405 OK Then the following also can be used: cut -d' ' -f3 access.log|uniq|while read line > do > num=`grep -c $line access.log` > echo $num $line > done|sort -r|head -5 read less
Comments

Python Trainer with 5 Years of expirience

$ cat inp.txt /xyz.txt 404 /abc.txt 506 /pqr.txt 404 /xyz.txt 404 /abc.txt 505 /abc.txt 406 /abc.txt 306 /xyz.txt 206 $ cat inp.txt | cut -d" " -f1|sort |uniq -c 4 /abc.txt 1 /pqr.txt 3 /xyz.txt
Comments

Senior DBA, Expert in Sql & No-sql

You can use sort | uniq -c
Comments

View 9 more Answers

Related Questions

shell scripting
A shell script is a computer program designed to be run by the UNIX shell, a command line interpreter. The various dialects of shell scripts are considered to be scripting languages. Typical operations...
Vishnu
I am a working professional and I have 4.5 years of experience. I want to learn on how to create complicated unix scripts in different flavours of unix OS. Please let me know possible course options for me.
Hi Nizar, To be able to create shell scripts, you should be first comfortable with Unix basic commands. I offer following two courses: 1) Unix Basics - covers all the useful Unix commands and utilities 2)...
Nizar
Hello everyone, I have done MBA. I want do career in IT field. Please suggest me best IT Course for get /entry for fresher.
Hello dear, MBA is done..what you wish to choose as career. kindly share details as in core interest. Regards
Priya

Now ask question in any of the 1000+ Categories, and get Answers from Tutors and Trainers on UrbanPro.com

Ask a Question

Related Lessons

Amazon written test for experience
For the below written test answer I defined with "*" at the end of the options NULL is the same as 0 for integer the same as blank for character * the same as 0 for integer and blank for character the...

How to check disk group size veritas symantec and volume size script?
#!/usr/bin/kshvxprint | grep Disk |awk {'print $3'} | grep -v rootdg >/var/tmp/dgnameprintf "\n......................................................................\n"printf "Server Name : "hostnamecat...

Unix Shell Scripting
UNIX Shell Scripting Unix – An Introduction:- Features & Advantages Unix Vs Linux Unix Architecture File Management Comparative study between Windows & Unix Vi Editor Unix –...

Internal and external commands Linux
Internal and external commands cmd: help To see all available internal commands cmd: type <your cmd> To check your cmd is internal or external.

Basics Of Unix
What is Unix ? The Unix operating system is a set of programs that act as a link between the computer and the user. The computer programs that allocate the system resources and coordinate all the details...
D

Dinesh Kumar G.

0 0
0

Recommended Articles

Information technology consultancy or Information technology consulting is a specialized field in which one can set their focus on providing advisory services to business firms on finding ways to use innovations in information technology to further their business and meet the objectives of the business. Not only does...

Read full article >

Business Process outsourcing (BPO) services can be considered as a kind of outsourcing which involves subletting of specific functions associated with any business to a third party service provider. BPO is usually administered as a cost-saving procedure for functions which an organization needs but does not rely upon to...

Read full article >

Whether it was the Internet Era of 90s or the Big Data Era of today, Information Technology (IT) has given birth to several lucrative career options for many. Though there will not be a “significant" increase in demand for IT professionals in 2014 as compared to 2013, a “steady” demand for IT professionals is rest assured...

Read full article >

Microsoft Excel is an electronic spreadsheet tool which is commonly used for financial and statistical data processing. It has been developed by Microsoft and forms a major component of the widely used Microsoft Office. From individual users to the top IT companies, Excel is used worldwide. Excel is one of the most important...

Read full article >

Looking for Unix Shell Scripting Training classes?

Learn from the Best Tutors on UrbanPro

Are you a Tutor or Training Institute?

Join UrbanPro Today to find students near you
X

Looking for Unix Shell Scripting Training Classes?

The best tutors for Unix Shell Scripting Training Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn Unix Shell Scripting Training with the Best Tutors

The best Tutors for Unix Shell Scripting Training Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more