generating a string pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting generating a string pattern
# 1  
Old 04-13-2011
generating a string pattern

hi all
i have a file which consists of lines of numbers separated with , i.e. comma separated numbers in a line
there are several such lines with variable lengths
e.g. the file may consist

Code:
12,23,45,1,2,3,5,54,8,4,1,65,6,6
1,2,3,4,5

i need such a script that it will generate the output for each line in the following way

e.g. Input line 1,2,3,4,5
the output shud be

Code:
1,2   1,3  1,4   1,5
2,3   2,4   2,5
3,4   3,5
4,5



how can i do that ?
i cud just split the string but stucked Smilie
please help me out

thanks in advance

Last edited by radoulov; 04-13-2011 at 09:29 AM.. Reason: Code tags, please!
# 2  
Old 04-13-2011
How about this,
Code:
awk -F, '{for(i=1;i<=NF;i++) {for(k=i+1;k<=NF;k++){printf " " $i FS $k}printf "\n"}}' inputfile

This User Gave Thanks to pravin27 For This Post:
# 3  
Old 04-13-2011
Code:
awk -F, '{for(i=1;i<NF;i++){for(j=i+1;j<=NF;j++) x=(x?x" ":y)$i","$j;print x;x=y}}' infile

Use nawk instead of awk if on SunOS or Solaris :

Code:
# cat tst
12,23,45,1,2,3,5,54,8,4,1,65,6,6
1,2,3,4,5
# nawk -F, '{for(i=1;i<NF;i++){for(j=i+1;j<=NF;j++) x=(x?x" ":y)$i","$j;print x;x=y}}' tst
12,23 12,45 12,1 12,2 12,3 12,5 12,54 12,8 12,4 12,1 12,65 12,6 12,6
23,45 23,1 23,2 23,3 23,5 23,54 23,8 23,4 23,1 23,65 23,6 23,6
45,1 45,2 45,3 45,5 45,54 45,8 45,4 45,1 45,65 45,6 45,6
1,2 1,3 1,5 1,54 1,8 1,4 1,1 1,65 1,6 1,6
2,3 2,5 2,54 2,8 2,4 2,1 2,65 2,6 2,6
3,5 3,54 3,8 3,4 3,1 3,65 3,6 3,6
5,54 5,8 5,4 5,1 5,65 5,6 5,6
54,8 54,4 54,1 54,65 54,6 54,6
8,4 8,1 8,65 8,6 8,6
4,1 4,65 4,6 4,6
1,65 1,6 1,6
65,6 65,6
6,6
1,2 1,3 1,4 1,5
2,3 2,4 2,5
3,4 3,5
4,5


Last edited by ctsgnb; 04-13-2011 at 10:05 AM..
This User Gave Thanks to ctsgnb For This Post:
# 4  
Old 04-13-2011
The following approach works by manipulating the comma-delimited string into valid brace expansion syntax.

Code:
#!/bin/ksh

while read seq; do
    while [ "${seq//[!,]}" ]; do
        echo ${seq/,(*,*)/,{\1}}
        seq=${seq#*,}
    done
done


Note that while the echo is safe given the sample data, should this be required to handle arbitrary strings, it would be best to use printf instead:
Code:
printf '%s\n' ${seq/,(*,*)/,{\1}} | paste -s -

Or, if trailing whitespace is inconsequential:
Code:
printf '%s ' ${seq/,(*,*)/,{\1}}

Regards,
Alister
This User Gave Thanks to alister For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Generating a Random String of 'n' length

Hi, How can I generate a string of random characters (alpha+numeric) of a particular length ? For e.g. for n=5, output = 'kasjf' n=10, output = 'hedbcd902k' Also, please let me know if random (valid) dates could also be generated. Thanks (7 Replies)
Discussion started by: rishigc
7 Replies

2. Shell Programming and Scripting

PHP - Regex for matching string containing pattern but without pattern itself

The sample file: dept1: user1,user2,user3 dept2: user4,user5,user6 dept3: user7,user8,user9 I want to match by '/^dept2.*/' but don't want to have substring 'dept2:' in output. How to compose such regex? (8 Replies)
Discussion started by: urello
8 Replies

3. Shell Programming and Scripting

Search for a pattern in a String file and count the occurance of each pattern

I am trying to search a file for a patterns ERR- in a file and return a count for each of the error reported Input file is a free flowing file without any format example of output ERR-00001=5 .... ERR-01010=10 ..... ERR-99999=10 (4 Replies)
Discussion started by: swayam123
4 Replies

4. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

5. UNIX for Dummies Questions & Answers

Append a string on the next line after a pattern string is found

Right now, my code is: s/Secondary Ins./Secondary Ins.\ 1/g It's adding a 1 as soon as it finds Secondary Ins. Primary Ins.: MEDICARE B DMERC Secondary Ins. 1: CONTINENTAL LIFE INS What I really want to achieve is having a 1 added on the next line that contain "Secondary Ins." It... (4 Replies)
Discussion started by: newbeee
4 Replies

6. Shell Programming and Scripting

extract a string within a string using a pattern

hi all, i have a file name using the following pattern: PREFIX: AR SOURCE: LEGACY DATETIME: YYYYMMDD_HH24MISS SUFFIX: .txt sample filename: AR_LEGACY_20101104_105500.txt i want to extract the source which is LEGACY in this case. how do i do this using shell? thanks. (4 Replies)
Discussion started by: adshocker
4 Replies

7. Shell Programming and Scripting

Looking for pattern next to variable string

Hi, I need to help to find the way to get specific data from a log, an unknow string beside a string that is the same always, for example: Input file: ctmdefine -TASKTYPE COMMAND -TABLE PRUEBAS_BIMLAR -JOBNAME PRUEBAJOB12 \ -GROUP PRUEBAS_BIM2 -APPLICATION PRUEBAS_BIM2 -NODEGRP gtwtran... (4 Replies)
Discussion started by: mgcorona
4 Replies

8. Emergency UNIX and Linux Support

String search and generating file

Hi Gurus, I have a requirement as below I have text file a.txt which contains hi hello process update status Output for file Ok to Proceed no issues good data arrangement My requirement here is i need to read the file and check for the words "OK to Proceed" and if it is available... (14 Replies)
Discussion started by: pssandeep
14 Replies

9. Shell Programming and Scripting

Ksh - finding pattern in file and generating a new file

I am trying to find for the pattern in first 5 bytes of every line in a text file and then generate a new file with the pattern found. Example: expected pattern is '-' to be serached in first 5 bytes of file only. Input File ab-cd jan-09 ddddd jan09 cc-ww jan09 dsgdq jan-09 ... (2 Replies)
Discussion started by: net
2 Replies

10. Shell Programming and Scripting

how to know if a string contains a certain pattern

say i want to know if string1 contains string2. thanks a lot! (11 Replies)
Discussion started by: Deanne
11 Replies
Login or Register to Ask a Question