Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
perl actually is a "linux" "command" You will find it on all systems, just like awk. You will even find it on the various BSD operating systems, such as OS X.
Sorry, but I am not familier to perl and need this in shell scripts / any linux command only
Now, I tried by creating 2 files such as -
cat file name | cut -d "," -f1 | uniq -c > test1
cat file name | cut -d "," -f2 > test2
So now, I have 2 files with o/p as -
test1
2 1311629642
1 1311629906
1 1311630077
1 1311631671
1 1311632245
test2
abc
def
ghi
jkl
mno
pqr
now just want to paste the 2 files (test1 and test2) in such way that it should print last o/p as -
2 1311629642 abc def
1 1311629906 ghi
1 1311630077 jkl
1 1311631671 mno
1 1311632245 pqr
If there were a common field between the two files you could have used join. But going over the data as you have originally presented it you can use Linux's awk or perl commands.
better to leave everything... can you help me to get desired o/p using awk / sort / any shell script ?
Well, if you aren't going to use Linux's perl command, I'd still recommend that you put it on your todo list for the future. It is going to come up again and again in similar circumstances.
Back to awk, my approach would be to set the input Field Separator to a comma and then iterate through the data file. As each line is read, make an array element with the first field as a key and increment that element. Also make an element in a second array with the first field as the key, too, and append field two to its value using a comma or other separator. Then the output occurs in the END {} block by looping through the keys in the first array and printing the relevant key and element value. Then use split() to divide the corresponding element in the second array and loop through it, printing each element.
The printf() function is useful in all that.
It may not be the best approach, but it is simple to implement.
Add it in a piece at a time. We can walk you through the steps in awk or perl, but do show us what you have started and which guides you are working from.
You don't seem to get the fact that Perl is every bit a standard UNIX/Linux command as awk/sed/etc. Plus, it's much faster, more efficient and more powerful.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.