LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 05-29-2018, 08:32 AM   #1
Aslah
LQ Newbie
 
Registered: May 2018
Posts: 2

Rep: Reputation: Disabled
how to make one row if its found same character in $1


Dears,

Need your assistance, I have a file which is contain below characters.

1234 ret_failure
2345 TMA_failure
5432 ANT_problem
1234 ANT_problem

in above file there are two cases with the same $1, but different $2 (1234 ret_failure & 1234 ANT_problem),

I want to make it as below output in unix (bash command).

1234 ret_failure, ANT_problem
2345 TMA_failure
5432 ANT_problem

Thanks in advance.
BR//Aslah
 
Old 05-29-2018, 09:26 AM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,230

Rep: Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724
Please post what you tried, it's a forum requirement
 
Old 05-29-2018, 09:43 AM   #3
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (current), FreeBSD, Win10, It varies
Posts: 9,951

Rep: Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146Reputation: 2146
learn some basic scripting in bash then you'll find it easier to tackle your needs to get something done in Linux.

https://linuxconfig.org/bash-scripti...-for-beginners
https://ryanstutorials.net/bash-scripting-tutorial/
https://www.tldp.org/LDP/abs/html/st...ipulation.html

If you're still having issues with bash and scripting ... post it. show your work. thanks

Last edited by BW-userx; 05-29-2018 at 09:44 AM.
 
Old 05-29-2018, 09:45 AM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 14,624

Rep: Reputation: 4756Reputation: 4756Reputation: 4756Reputation: 4756Reputation: 4756Reputation: 4756Reputation: 4756Reputation: 4756Reputation: 4756Reputation: 4756Reputation: 4756
would be nice to know at least the language you prefer. It is a relatively easy task for awk, but you can use obviously perl/python/whatever
 
Old 05-29-2018, 02:08 PM   #5
X-LFS-2010
Member
 
Registered: Apr 2016
Posts: 423

Rep: Reputation: Disabled
cat file | sort -s -k2,2 | sort -s -n -k1,1 | awk '{if($1==x){printf ","$2} else {print "";printf $1" "$2}}{x=$1}END{print ""}'

Last edited by X-LFS-2010; 05-29-2018 at 02:11 PM.
 
1 members found this post helpful.
Old 06-05-2018, 12:31 PM   #6
Aslah
LQ Newbie
 
Registered: May 2018
Posts: 2

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by X-LFS-2010 View Post
cat file | sort -s -k2,2 | sort -s -n -k1,1 | awk '{if($1==x){printf ","$2} else {print "";printf $1" "$2}}{x=$1}END{print ""}'

Thanks dude..its working...
 
Old 06-06-2018, 03:34 PM   #7
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,230

Rep: Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724
with perl
Code:
perl -lane 'push @{ $a{$F[0]} }, $F[1]; END{print "$_ ", join ", ", @{ $a{$_} } for sort keys %a}' file
(you would have got more replies if you posted some code you tried...)

Last edited by keefaz; 06-06-2018 at 03:36 PM.
 
Old 06-06-2018, 10:46 PM   #8
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 18,890

Rep: Reputation: 3207Reputation: 3207Reputation: 3207Reputation: 3207Reputation: 3207Reputation: 3207Reputation: 3207Reputation: 3207Reputation: 3207Reputation: 3207Reputation: 3207
Similar can be achieved in awk using a ternary operator - no need for sorts first.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to make the output in multiple row become single row? ahpin Linux - Newbie 4 07-19-2013 07:49 AM
[SOLVED] find a null value in a row/column and delete entire row umix Linux - Newbie 10 10-13-2011 01:26 AM
[SOLVED] MySQL make a row first in result of query Skyer Programming 2 08-12-2011 06:31 AM
Shell script to parse csv-like output, row by row utahnix Linux - General 8 12-08-2007 05:03 AM
vi editor - how to delete a character in each row of a column? flipwhy Linux - Newbie 6 01-22-2007 02:12 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration