Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Getting strings before and after a character Post 302277147 by ricksj on Thursday 15th of January 2009 02:35:30 PM
Old 01-15-2009
Getting strings before and after a character

OK This one has me stumped. I have the following line,
program name - the program description that can also contain a hyphen - character.

I'm need to separate the "program name" from the program description.

I've tried using an array function with the - as delimiter, but I ran into a problem when I discovered some of the files I'm handling contain this second hyphen.

Using this code results in output that's out of order:
Code:
    split(s, array1, "-")
    for (i in array1) {
      print "ARRAY "i": " array1[i]
    }

Output:
Code:
ARRAY 2:  the program description that can also contain a hyphen 
ARRAY 3:  character.
ARRAY 1: program name

I use the array1[1] no problem, but how do I use the 2+ arrays?

I'm open to alternate methods for splitting this line of text.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl RegExp to remove last character from strings

I use SAS (a statistical software) and have to remove last character or the last 1/2 numbers that appear after characters from the string using Perl Regular Expression (which is recognized by SAS). Input: f183ii10 f183ii2 f182ii1 f182ii2 f183iim f22ii f22ii11 f22ii12 pmh4 pmhm Desired... (2 Replies)
Discussion started by: ospreyeagle
2 Replies

2. Shell Programming and Scripting

Finding character mismatch position in two strings

Hello, I would like to find an efficient way to compare a pair of strings that differ at one position, and return the difference and position. For example: String1 123456789 String2 123454789 returning something - position 6, 6/4 Thanks in advance, Mike (5 Replies)
Discussion started by: etherite
5 Replies

3. UNIX for Dummies Questions & Answers

Help to replace character strings

Hello Can Any1 help me. I want to replace a specific character string inside a file at a specific location with a particular character with the help of a command or a shell script. The tr command replaces a specific character with another for all the occurences of that character in the file. I... (5 Replies)
Discussion started by: rahulrathod
5 Replies

4. Shell Programming and Scripting

Change Hex character strings to HTML entities

Hi! I am not a whiz at awk and very unsure about the aplication of awk solve my problem. I was hoping for some quick pointers so I can figure this out. I have a file that looks like so: label.Asked=\u8CEA\u554F\u6E08\u307F button.Edit=\u7DE8\u96C6... (3 Replies)
Discussion started by: pinnochio
3 Replies

5. Shell Programming and Scripting

replace two character strings by two variables with sed command

Hello, I want to writte a script that replace two character strings by two variables with the command sed butmy solution doesn't work. I'm written this: sed "s/TTFactivevent/$TTFav/g && s/switchSLL/$SLL/g" templatefile. I want to replace TTFactivevent by the variable $TTFav, that is a... (4 Replies)
Discussion started by: POPO10
4 Replies

6. UNIX for Dummies Questions & Answers

character-by-character comparison of strings

This might be a dummy question, but is there a command in UNIX that compare two strings character-by-character and display the difference? ---------- Post updated at 11:25 AM ---------- Previous update was at 10:32 AM ---------- Or probably what I'm looking is how to break a string into... (3 Replies)
Discussion started by: Orbix
3 Replies

7. Shell Programming and Scripting

Selecting strings with - as first character in bash

I have a variable containing a list of strings, and want to create a string with the first character being -. Example: var="-name fred paul -surname winnett dimech" I want a string, namely errStr="-name -surname" (2 Replies)
Discussion started by: kristinu
2 Replies

8. Shell Programming and Scripting

Find position of character in multiple strings in a file

Greetings. I have a file with information like this: AMNDHRKEOEU?AMNDHRKEOEU?AMNDHRKEOEU?AMNDHRKEOEU? AMNDHRKEEU?AMNDHREOEU? AMNDHREU?AHRKEOEU?AMNDHRKEU?AMNDKEOEU? What I need to extract is the position, in every line, of every occurrence of '?' A desired output would be something... (6 Replies)
Discussion started by: Twinklefingers
6 Replies

9. UNIX for Dummies Questions & Answers

Extracting 22-character strings from text using sed/awk?

Here is my task, I feel sure this can be accomplished with see/awk but can't seem to figure out how. I have large flat file from which I need to extract every case of a pairing of characters (GG) in this case PLUS the previous 20 characters. The output should be a list (which I plan to make... (17 Replies)
Discussion started by: Twinklefingers
17 Replies

10. Shell Programming and Scripting

How to remove certain character strings with awk?

Hi all, I need to remove DBPATH= and /db from the string below using awk (or sed, as it also exists on the machine). Input: DBPATH=/some/path/database/db Desired output: /some/path/database Thank you! (8 Replies)
Discussion started by: ejianu
8 Replies
ARRAY_INTERSECT_UKEY(3) 						 1						   ARRAY_INTERSECT_UKEY(3)

array_intersect_ukey - Computes the intersection of arrays using a callback function on the keys for comparison

SYNOPSIS
array array_intersect_ukey (array $array1, array $array2, [array $...], callable $key_compare_func) DESCRIPTION
array_intersect_ukey(3) returns an array containing all the values of $array1 which have matching keys that are present in all the argu- ments. PARAMETERS
o $array1 - Initial array for comparison of the arrays. o $array2 - First array to compare keys against. o $... - Variable list of array arguments to compare keys against. o $key_compare_func - The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second. int callback (mixed $a, mixed $b) RETURN VALUES
Returns the values of $array1 whose keys exist in all the arguments. EXAMPLES
Example #1 array_intersect_ukey(3) example <?php function key_compare_func($key1, $key2) { if ($key1 == $key2) return 0; else if ($key1 > $key2) return 1; else return -1; } $array1 = array('blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4); $array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan' => 8); var_dump(array_intersect_ukey($array1, $array2, 'key_compare_func')); ?> The above example will output: array(2) { ["blue"]=> int(1) ["green"]=> int(3) } In our example you see that only the keys 'blue' and 'green' are present in both arrays and thus returned. Also notice that the values for the keys 'blue' and 'green' differ between the two arrays. A match still occurs because only the keys are checked. The values returned are those of $array1. SEE ALSO
array_diff(3), array_udiff(3), array_diff_assoc(3), array_diff_uassoc(3), array_udiff_assoc(3), array_udiff_uassoc(3), array_diff_key(3), array_diff_ukey(3), array_intersect(3), array_intersect_assoc(3), array_intersect_uassoc(3), array_intersect_key(3). PHP Documentation Group ARRAY_INTERSECT_UKEY(3)
All times are GMT -4. The time now is 08:37 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy