LinuxQuestions.org
Review your favorite Linux distribution.
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 03-29-2011, 05:30 AM   #1
cedance
LQ Newbie
 
Registered: May 2005
Posts: 2

Rep: Reputation: 0
sort multiple columns + replace another column


Hi, I have two files as shown here:

File 1: http://cl.ly/2n0J3O2w1Z0p2A1j0o30
File 2: http://cl.ly/2j3B2h1J193Z0r1B3I43

I have to sort for the first column in reverse and then sort the second column numerically, which I managed to do with the command:

Code:
sort -k1,1r -k2,2n <file1> <file2>
The output is shown here: http://cl.ly/3a38450X0n260I1f0z3s

However, since the 4th column just counted each entry from 1 to n, I have duplicate entries after merge. I would like to have this column run from JUNC000001 to JUNC<no: of entries> again. I can get the number of entries with
Code:
cat <merged_file> | wc -l
But is it possible to replace this column with JUNC<number> running from 1 to number of entries directly via unix command line?

Thank you very much!
 
Old 03-29-2011, 06:59 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
It's rather hard to run tests when all we have are screenshots to work from. I'm not about to transcribe those images into actual text. Care to post some actual data in text format?

In any case, I'm not entirely clear on what you're asking. In your examples all entries in c1 are the same, and I don't see any duplicate lines in the output screenshot, only duplicates inside c4. If I understand correctly though, you want to replace everything in the c4 column with sequential numbers? That should be possible with awk. Something like this, perhaps?
Code:
awk 'BEGIN{format="%s %s %s JUNC%08d %s %s %s %s %s %s %s\n"} ; { i++ ; $4=i ;  printf(format , $1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11) }' merged-file
It's kind of messy though, because I assume you'd want to properly zero-pad the entries, and my knowledge of printf in awk is limited. I can't figure out how to do it without defining every field separately (if that's even possible). Perhaps someone with more experience can come along and simplify it.


BTW, it's not usually necessary to use cat and a pipe with wc (or most other cli tools for that matter). It can take a file name as an argument.
Code:
wc -l merged-file
 
  


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
[SOLVED] Insert column with awk or sed between two columns captainentropy Linux - Newbie 9 11-27-2014 12:49 PM
How to get parts of one column and prints all columns ? nasra2002 Linux - Software 2 12-13-2010 10:36 PM
awk multiple column into single column ilukacevic Programming 49 07-19-2010 08:23 PM
[SOLVED] [Perl] fail to sort a file with 300,000 lines by multiple columns Kunsheng Programming 10 11-13-2009 07:41 PM
sort by multiple columns wakatana Linux - Newbie 5 10-18-2009 04:35 PM


All times are GMT -5. The time now is 06:33 AM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration