Sed - Get Separator From String


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Sed - Get Separator From String
# 1  
Old 03-02-2010
CPU & Memory Sed - Get Separator From String

Hi All,

I have the following data in a korn shell variable:
Code:
   a="FirstValue|SecondValue|ThirdValue"

The value between "FirstValue", "SecondValue" and "ThirdValue" can change, in this case is a comma: "," and I need to print it only once. I need to know what is the separator value.

I am using the following sed, but it is returning an error:
Code:
   echo $a | sed 's/.*([\||,|:|;]).*/\1/'
   sed: command garbled: s/.*([\||,|:|;]).*/\1/

But if I execute without printing 1, it executes ok:
Code:
   echo $a | sed 's/.*([\||,|:|;]).*//'
   FirstValue|SecondValue|ThirdValue

As you can see, the separator can be: "|" "," ":" ";"

Let me know if my question is not clear.

Thanks!
# 2  
Old 03-02-2010
Hammer & Screwdriver Another approach

Code:
a='FirstVal|SecVal|ThirdVal'
b='|,:;'
echo $a | tr -cd "$b" | tr -s "$b"

When I tried this, it gave me which delimiter was used in the file.
# 3  
Old 03-02-2010
Try:

Code:
echo $a  | sed 's/[A-Za-z]*\(.\).*/\1/'

# 4  
Old 03-02-2010
Hi!

Thanks for the replies!

I was able to return the separator as you can see in the following:
Code:
a="FirstValue|SecondValue|ThirdValue"; echo $a | sed 's/.*\([\||,|:|;]\).*/\1/'
a="FirstValue,SecondValue,ThirdValue"; echo $a | sed 's/.*\([\||,|:|;]\).*/\1/'
a="FirstValue:SecondValue:ThirdValue"; echo $a | sed 's/.*\([\||,|:|;]\).*/\1/'
a="FirstValue;SecondValue;ThirdValue"; echo $a | sed 's/.*\([\||,|:|;]\).*/\1/'

Thanks again!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Use string as Record separator in awk

Hello to all, Please some help on this. I have the file in format as below. How can I set the record separator as the string below in red "No. Time Source Destination Protocol Length Info" I've tried code below but it doesn't seem to... (6 Replies)
Discussion started by: cgkmal
6 Replies

2. Shell Programming and Scripting

sed multilines + separator confusion !!

Hi Seders, i am new to this forum, but i think it's quite the best place to post. So, here is my pb : I have a csv file, with comma separator and text enclosed by ". First pb is with text in " ......... ", wich sometimes includes lines break, and commas And to complicate a little more,... (4 Replies)
Discussion started by: yogeek
4 Replies

3. Shell Programming and Scripting

Need a separator string between fields in cut -c command

Hi All, I'm trying to view data using cut command for a fixed length file using the below command: cut -c 1-3,4-5 FALCON_PIS_00000000.dat I want to mention a separator say | (pipe) in between 1-3 and 4-5. Please let me know how to achieve this. Thanks in Advance, (3 Replies)
Discussion started by: HemaV
3 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. Shell Programming and Scripting

replace (sed?) a string in file with multiple lines (string) from variable

Can someone tell me how I can do this? e.g: a=$(echo -e wert trewt ertert ertert ertert erttert erterte rterter tertertert ert) How do i replace the STRING with $a? I try this: sed -i 's/STRING/'"$a"'/g' filename.ext but this don' t work (2 Replies)
Discussion started by: jforce
2 Replies

6. Shell Programming and Scripting

awk, string as record separator, transposing rows into columns

I'm working on a different stage of a project that someone helped me address elsewhere in these threads. The .docs I'm cycling through look roughly like this: 1 of 26 DOCUMENTS Copyright 2010 The Age Company Limited All Rights Reserved The Age (Melbourne, Australia) November 27, 2010... (9 Replies)
Discussion started by: spindoctor
9 Replies

7. Shell Programming and Scripting

sed to insert a separator

My txt file consists of records with 6 numbers followed by 3 characters. Is there a simple “sed” which will insert a | separator between the 6th and 7th position ? Many thanks (3 Replies)
Discussion started by: malts18
3 Replies

8. Shell Programming and Scripting

how to search for string that includes folder separator ?

I want to select only second line. I want to search for any line that has both -> input.txt /userA/aaaaaaabbb/jakarta /userA/aaaaaaa/jakarta /userB/aaaaaaabbb/jakarta /userB/aaaaaaa/jakarta This command does have effect of --> $>cat input.txt | /usr/xpg4/bin/grep -E... (13 Replies)
Discussion started by: kchinnam
13 Replies

9. Shell Programming and Scripting

Trying to change date separator with sed

Hi there I am trying to convert some date seperators in a large newline delimited file. each line i am interested in has a date in the format 27/05/2009 all I want is to convert the slashes to tildes(~) I have come up with the following code but it does nothing. sed... (5 Replies)
Discussion started by: RadRod
5 Replies

10. Shell Programming and Scripting

sed problem - replacement string should be same length as matching string.

Hi guys, I hope you can help me with my problem. I have a text file that contains lines like this: 78 ANGELO -809.05 79 ANGELO2 -5,000.06 I need to find all occurences of amounts that are negative and replace them with x's 78 ANGELO xxxxxxx 79... (4 Replies)
Discussion started by: amangeles
4 Replies
Login or Register to Ask a Question