Sponsored Content
Top Forums Shell Programming and Scripting Common prefix of a list of strings Post 302867669 by CarloM on Thursday 24th of October 2013 04:08:24 PM
Old 10-24-2013
Common prefix of a list of strings

Is there a simple way to find the longest common prefix of a space-separated list of strings, optionally by field?

For example, given input:
Code:
"aaa_b_cc aaa_b_cc_ddd aaa_b_cc aaa_b_cd"

with no field separator, output:
Code:
aaa_b_c

with _ field separator, output:
Code:
aaa_b

I have an awk solution which appears to work (although I haven't done much testing):
Code:
function get_common_prefix() {
        list="$1"
        sep="$2"

        printf "$list" | awk '(NR==1) {pcount=split($0,prefix)}
                               (NR>1) {for (i=pcount;i>0;i--) {if ($i!=prefix[i]) {pcount=i-1}}}
                                END {NF=pcount;print}' RS=' ' FS=$sep OFS=$sep
}

myprefix=$(get_common_prefix "$1" $2)

printf "[%s]\n" $myprefix

Searching didn't come up with anything more elegant that could handle both by character and by field. So, just wondering if the forum had any better solutions.


EDIT: The above (cygwin) doesn't seem to work very well on (non-gawk) AIX 6.1. Seems you can't fiddle with NF in END the way I've doing above (although just printing pcount fields of prefix works), and having a blank field separator seems equivalent to whitespace (i.e. it won't split by character). Smilie

Last edited by CarloM; 10-25-2013 at 04:45 AM.. Reason: Platforms
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

different take on common ?: search for two strings and remove lines between them

Thank you for assisting, I've got a partial solution just needs a tweak. Hulk-BASH$ cat somefile.txt oh there is some stuff here some more stuff here START_LABEL stuff I want more stuff I want END_LABEL other stuff here too and even more stuff here too Hulk-BASH$ Hulk-BASH$ sed... (8 Replies)
Discussion started by: laser
8 Replies

2. Shell Programming and Scripting

Simple script to find common strings in two files

Hi , I want to write a simple script. I have two files file1: BCSpeciality Backend CB CBAPQualDisp CBCimsVFTRCK CBDSNQualDisp CBDefault CBDisney CBFaxMCGen CBMCGeneral CBMCQualDisp file2: CSpeciality Backend (8 Replies)
Discussion started by: ramky79
8 Replies

3. UNIX for Advanced & Expert Users

Find common Strings in two large files

Hi , I have a text file in the format DB2: DB2: WB: WB: WB: WB: and a second text file of the format Time=00:00:00.473 Time=00:00:00.436 Time=00:00:00.016 Time=00:00:00.027 Time=00:00:00.471 Time=00:00:00.436 the last string in both the text files is of the... (4 Replies)
Discussion started by: kanthrajgowda
4 Replies

4. Shell Programming and Scripting

Script to find NOT common strings in two files

Hi all, I'd like you to help or give any advise about the following: I have two (2) files, file1 and file2, both files have information common to each other. The contents of file1 is a subset of the contents of file2: file1: errormsgadmin esdp esgservices esignipa iprice ipvpn irm... (0 Replies)
Discussion started by: hnux
0 Replies

5. Shell Programming and Scripting

Script to find NOT common strings in two files

Hi all, I'd like you to help or give any advise about the following: I have two (2) files, file1 and file2, both files have information common to each other. The contents of file1 is a subset of the contents of file2: file1: errormsgadmin esdp esgservices esignipa iprice ipvpn irm... (18 Replies)
Discussion started by: hnux
18 Replies

6. Shell Programming and Scripting

Need the script to remove common strings,tags etc

I have a file say "example.xml" and the contents of this example.xml are <project name="platform/packages/wallpapers/Basic" path="packages/wallpapers/Basic" revision="225e410f054c4ad5c828b0fec9be1b47c4376711"/> <project name="platform/packages/wallpapers/Galaxy4"... (3 Replies)
Discussion started by: acdc
3 Replies

7. Shell Programming and Scripting

AWK adding prefix/suffix to list of strings

75 103 131 133 138 183 197 221 232 234 248 256 286 342 368 389 463 499 524 538 (5 Replies)
Discussion started by: chrisjorg
5 Replies

8. UNIX for Dummies Questions & Answers

Listing no. of files in UNIX with common prefix name

Hi, I am entirely new to Unix, need your help to perform certain actions in unix: Can anyone please tell me how to list the number of files in UNIX with Common prefix name. "I want just the number of files and not the names of files". Thanks (12 Replies)
Discussion started by: Hitesh1008
12 Replies

9. Shell Programming and Scripting

Return the list of file name prefix with first 6 character

Good day people, Kindly advice what is the operator/command that I should try out to if I am about to return a list of prefix of my filename with first 6 character. I understand I could use sed to retrieve the first 6 charter itself. but i wonder if there is any aix command allow me to loop... (4 Replies)
Discussion started by: cielle
4 Replies

10. UNIX for Beginners Questions & Answers

How to pass strings from a list of strings from another file and create multiple files?

Hello Everyone , Iam a newbie to shell programming and iam reaching out if anyone can help in this :- I have two files 1) Insert.txt 2) partition_list.txt insert.txt looks like this :- insert into emp1 partition (partition_name) (a1, b2, c4, s6, d8) select a1, b2, c4, (2 Replies)
Discussion started by: nubie2linux
2 Replies
sasl_listmech(3SASL)			      Simple Authentication Security Layer Library Functions			      sasl_listmech(3SASL)

NAME
sasl_listmech - retrieve a list of the supported SASL mechanisms SYNOPSIS
cc [ flag ... ] file ... -lsasl [ library ... ] #include <sasl/sasl.h> int sasl_listmech(sasl_conn_t *conn, const char *user, const char *prefix, const char *sep, const char *suffix, const char **result, unsigned *plen,int *pcount); DESCRIPTION
The sasl_listmech() interface returns a string listing the SASL names of all the mechanisms available to the specified user. This call is typically given to the client through a capability command or initial server response. Client applications need this list so that they know what mechanisms the server supports. PARAMETERS
conn The SASL context for this connection user restricts the mechanism list to those mechanisms available to the user. This parameter is optional. user Restricts security mechanisms to those available to that user. The value of user may be NULL, and it is not used if called by the client application. prefix Appended to the beginning of result. sep Appended between mechanisms. suffix Appended to the end of result. result A null-terminated result string. result must be allocated or freed by the library. plen The length of the result filled in by the library. The value of plen may be NULL. pcount The number of mechanisms available. The value of pcount is filled in by the library. The value of pcount may be NULL RETURN VALUES
sasl_listmech() returns an integer that corresponds to a SASL error code. ERRORS
SASL_OK The call to sasl_listmech() was successful. See sasl_errors(3SASL) for information on SASL error codes. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWlibsasl | +-----------------------------+-----------------------------+ |Interface Stability |Evolving | +-----------------------------+-----------------------------+ |MT-Level |MT-Safe | +-----------------------------+-----------------------------+ SEE ALSO
sasl_errors(3SASL), attributes(5) SunOS 5.11 14 Oct 2003 sasl_listmech(3SASL)
All times are GMT -4. The time now is 08:04 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy