How to merge number?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to merge number?
# 1  
Old 09-27-2011
How to merge number?

hi everyone

I try to make script to help my work on Centos
my data is number in 1 colum like below

1
2
3
4
5
6
9
10
11
15
18

I want result show in new file like
1-6,9-11,15,18

What script can use to make result like above?

Thank you.
# 2  
Old 09-28-2011
Using awk, this is one way:

Code:
awk '
    function showit()
    {
            if( first++ )
                printf( ", " );
            if( last != start )
                printf( "%d - %d", start, last );
            else
                printf( "%d", start );
    }

    BEGIN {last = - 9e9; }
    {
        if( last != $1 - 1 )
        {
            if( start )
                showit()
            start = $1;
        }

        last = $1;
    }

    END {
        showit();
        printf( "\n" );
    }
'

This works for your example, but will need to be modified if your starting value is 0. Input records are read from standard input.

Last edited by agama; 09-28-2011 at 12:05 AM.. Reason: redid print to match request
This User Gave Thanks to agama For This Post:
# 3  
Old 09-28-2011
I like pipes (and cats I like too Smilie ):
Code:
cat INPUTFILE                            |                                   
while read n; do                         #
  [ "$n" -ne $((prev+1)) ] && echo       # 
  printf "$n,"                           #
  prev=$n                                #
done                                     | 
sed -r 's/([0-9]+),.*,([0-9]+,)$/\1-\2/' | 
tr -d '\n'                               |
sed 's/.$/\n/'
1-6,9-11,15,18

POSIX shell and GNU sed (-r option for ERE - extended regular expressions). Change ERE to BRE if you don't have it.

Last edited by yazu; 09-28-2011 at 12:34 AM..
This User Gave Thanks to yazu For This Post:
# 4  
Old 09-28-2011
onemore question
if I want result have input filename as well like

aaa.txt 1-6,9-11,15,18

how to do that

now I use

grep xxx aaa.txt l awk '{print $4 }' |
while read n; do #
[ "$n" -ne $((prev+1)) ] && echo #
printf "$n," #
prev=$n #
done |
sed -r 's/([0-9]+),.*,([0-9]+,)$/\1-\2/' |
tr -d '\n' |
sed 's/.$/\n/'
# 5  
Old 09-28-2011
Code:
(echo aaa.txt
grep xxx aaa.txt | awk '{print $4 }'     |                                   
while read n; do                         #
  [ "$n" -ne $((prev+1)) ] && echo       # 
  printf "$n,"                           #
  prev=$n                                #
done                                     | 
sed -r 's/([0-9]+),.*,([0-9]+,)$/\1-\2/' | 
tr -d '\n'                               |
sed 's/.$/\n/')

This User Gave Thanks to yazu For This Post:
# 6  
Old 09-28-2011
Quote:
Originally Posted by GeodusT
hi everyone

I try to make script to help my work on Centos
my data is number in 1 colum like below

1
2
3
4
5
6
9
10
11
15
18

I want result show in new file like
1-6,9-11,15,18

What script can use to make result like above?

Thank you.
what is the logic for the output format ?
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

List files with number to select based on number

Hi experts, I am using KSH and I am need to display file with number in front of file names and user can select it by entering the number. I am trying to use following command to display list with numbers. but I do not know how to capture number and identify what file it is to be used for... (5 Replies)
Discussion started by: mysocks
5 Replies

2. Shell Programming and Scripting

awk to find number in a field then print the line and the number

Hi I want to use awk to match where field 3 contains a number within string - then print the line and just the number as a new field. The source file is pipe delimited and looks something like 1|net|ABC Letr1|1530||| 1|net|EXP_1040 ABC|1121||| 1|net|EXP_TG1224|1122||| 1|net|R_North|1123|||... (5 Replies)
Discussion started by: Mudshark
5 Replies

3. Shell Programming and Scripting

Shell code for split/merge the file with certain number of columns

i have a file with some number of colums and each row have different number of fields. now my target is supposed to be, each row should have same number of columns. example: src file: 111,S3mobile,Samsu ng 112,Lu mia,Nok ia 113,brav ia,Sonyerichson tgt file: 111,S3mobile,Samsung... (8 Replies)
Discussion started by: abhilash_nakka
8 Replies

4. Shell Programming and Scripting

Help with sort word followed by exponential number and numeric number at the same time

Input file: ID_34 2E-69 2324 ID_1 0E0 3254 ID_1 0E0 5434 ID_5 0E0 436 ID_1 1E-14 2524 ID_1 5E-52 46437 ID_3 65E-20 45467 ID_1 0E0 6578 ... Desired output file: ID_1 0E0 6578 ID_1 0E0 5434 ID_1 0E0 3254 ID_1 5E-52 46437 ID_1 1E-14 2524 ID_3 65E-20 45467 (5 Replies)
Discussion started by: cpp_beginner
5 Replies

5. Shell Programming and Scripting

The difference between end number in the early row and the start number in the next

Hi Power User, I'm trying to compute this kind of text file format: file1: jakarta 100 150 jakarta 170 210 beijing 220 250 beijing 260 280 beijing 290 320 new_york 330 350 new_york 370 420 tokyo 430 470 tokyo 480 ... (2 Replies)
Discussion started by: anjas
2 Replies

6. Shell Programming and Scripting

Perl : print the sequence number without missing number

Dear Perl users, I need your help to solve my problem below. I want to print the sequence number without missing number within the range. E.g. my sequence number : 1 2 3 4 5 6 7 8 11 12 13 14 my desired output: 1 -8 , 11-14 my code below but still problem with the result: 1 - 14 1 -... (2 Replies)
Discussion started by: mandai
2 Replies

7. UNIX for Dummies Questions & Answers

Merge two files with common IDs but unequal number of rows

Hi, I have two files that I would like to merge and think that there should be a solution using awk. The files look something like this: file 1 IDX1 IDY1 IDX2 IDY2 IDX3 IDY3 file 2 IDY1 dataA data1 IDY2 dataB data2 IDY3 dataC data3 Desired output IDX1 IDY1 dataA data1 IDX2 ... (5 Replies)
Discussion started by: katie8856
5 Replies

8. UNIX for Advanced & Expert Users

merge two tab delimited file with exact same number of rows in unix/linux

Hi I have two tab delimited file with different number of columns but same number of rows. I need to combine these two files in such a way that row 1 in file 2 comes adjacent to row 1 in file 1. For example: The content of file1: field1 field2 field3 a1 a2 a3 b1 b2 b3... (2 Replies)
Discussion started by: mary271
2 Replies

9. Shell Programming and Scripting

Merge two non-consecutive lines based on line number or string

This is a variation of an earlier post found here: unixcom/shell-programming-scripting/159821-merge-two-non-consecutive-lines.html User Bartus11 was kind enough to solve that example. Previously, I needed help combining two lines that are non-consecutive in a file. Now I need to do the... (7 Replies)
Discussion started by: munkee
7 Replies
Login or Register to Ask a Question