Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 03-29-2011, 04:30 AM   #1
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:
File 2:

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:

sort -k1,1r -k2,2n <file1> <file2>
The output is shown here:

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
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, 05:59 AM   #2
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,826

Rep: Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973Reputation: 1973
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?
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.
wc -l merged-file


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

All times are GMT -5. The time now is 07:23 AM.

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