split string using sed, perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting split string using sed, perl
# 1  
Old 10-08-2012
split string using sed, perl

How can I split following input into stwo strings:

Input:

Code:
 
1^~^2^~^3^~^4^~^5^~^6^~^7^~^8^~^9

Output:
Code:
 
 
$string1 = 1^~^2^~^
$string2 = 3^~^4^~^5^~^6^~^7^~^8^~^9

Note: the length of string may vary, say upto 15. String 1 will contain only first two. string2 will contain rest.
# 2  
Old 10-08-2012
on which pattern you want to split..?

Code:
$ sed 's/3/\n&/' file
1^~^2^~^
3^~^4^~^5^~^6^~^7^~^8^~^9

Code:
$ sed 's/\^~\^/&\n/2' file
1^~^2^~^
3^~^4^~^5^~^6^~^7^~^8^~^9

Code:
$sed 's/[0-9]/\n&/3' file
1^~^2^~^
3^~^4^~^5^~^6^~^7^~^8^~^9


Last edited by pamu; 10-08-2012 at 03:19 AM..
# 3  
Old 10-08-2012
I want the code in perl and want to split the string and store in two variables as I said above.
The separator will be '^~^'
Please refer to my first post.
# 4  
Old 10-08-2012
Code:
#!/usr/bin/perl -w
use strict;

my $var='1^~^2^~^3^~^4^~^5^~^6^~^7^~^8^~^9';
my ($string1,$string2);

if ($var =~ /^((?:[0-9]+\^~\^){2})(.*)/) {
 $string1 = $1;
 $string2 = $2;
}

if (defined $string1 and defined $string2) {
print "String1 is : $string1\n";
print "String2 is : $string2\n";
}

producing
Code:
String1 is : 1^~^2^~^
String2 is : 3^~^4^~^5^~^6^~^7^~^8^~^9

# 5  
Old 10-08-2012
Quote:
Originally Posted by som.nitk
I want the code in perl
Please check your thread title. You have mentioned there in sed, perl.

If you want in perl only use elixir's solution.Smilie
# 6  
Old 10-08-2012
Why does it not match any alphanumeric characters with following?

Code:
 
if ($var =~ /^((?:[a-zA-Z0-9]*\^~\^){2})(.*)/) {
 $string1 = $1;
 $string2 = $2;
}


Last edited by som.nitk; 10-08-2012 at 03:31 AM..
# 7  
Old 10-08-2012
# Bash script

Code:
#!/bin/bash

string='1^~^2^~^3^~^4^~^5^~^6^~^7^~^8^~^9'

# splitting point
var1=3

string1=`echo "$string" | awk -F["$var1"] '{print $1}'`

string2=`echo "$string" | awk -F["$var1"] -v var1="$var1" '{print var1$2}'`

echo "Original String : $string"

echo "After splitting: "

echo "String1 is : $string1"

echo "String2 is : $string2"

# Usage

Code:
./strings.sh

# Result

Code:
Original String : 1^~^2^~^3^~^4^~^5^~^6^~^7^~^8^~^9
After splitting: 
String1 is : 1^~^2^~^
String2 is : 3^~^4^~^5^~^6^~^7^~^8^~^9


Last edited by ip_address; 10-08-2012 at 03:30 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Using sed to split hex string

Hi, I'm looking to split the following hex string into rows of four elements. I've tried the following but it doesn't seem to work. How can I tell sed to match based on a pair of number(s) and letter(s), and add a newline every 4 pairs? In addition, I need to add another newline after every... (5 Replies)
Discussion started by: sand1234
5 Replies

2. Shell Programming and Scripting

sed split string

Greetings, i have a string that looks like Network "123" "ABC" i need to make it look like: Network "123" Network "ABC" Help please? Thanks again Please use CODE tags when displaying sample input, sample output, and code segments (as required by forum rules). (2 Replies)
Discussion started by: hoyanet
2 Replies

3. Shell Programming and Scripting

Perl split string separated by special character

Hello I have string (string can have more sections) LINE="AA;BB;CC;DD;EE"I would like to assigne each part of string separated by ";" to some new variable. Can someone help? (4 Replies)
Discussion started by: vikus
4 Replies

4. Shell Programming and Scripting

Split the string in perl

Hi All, How to split the string KAR_Celltick_Ban_GSMGW3 and want to pickup the third filed. Sometime the string may be "KAR_Celltick_Ban" like this Thanks in advance (1 Reply)
Discussion started by: sujit_kashyap
1 Replies

5. Shell Programming and Scripting

Oneliner ---split string to character by piping shell output to perl

Hello, I was trying to split a string to characters by perl oneliner. echo "The quick brown fox jumps over the lazy dog" | perl -e 'split // ' But did not work as with bash script pipe: echo "The quick brown fox jumps over the lazy dog" | fold -w1 | sort | uniq -ic 8 1 T 1... (6 Replies)
Discussion started by: yifangt
6 Replies

6. 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

7. UNIX for Dummies Questions & Answers

Trim String in 3rd Column in Tab Delimited File...SED/PERL/AWK?

Hey Everybody, I am having much trouble figuring this out, as I am not really a programmer..:mad: Datafile.txt Column0 Column1 Column2 ABC DEF xxxGHI I am running using WGET on a cronjob to grab a datafile, but I need to cut the first three characters from... (6 Replies)
Discussion started by: rickdini
6 Replies

8. Shell Programming and Scripting

Split A String

Hi, I am new to scripting and need help splitting a string using space as the delimiter. How can I do that? I want the result to be stored in an Array. I tried using set -A arr $(echo $FILE) echo $arr The result of the above was ''. Thanks. (2 Replies)
Discussion started by: newbie187
2 Replies

9. Shell Programming and Scripting

Split a file based on pattern in awk, grep, sed or perl

Hi All, Can someone please help me write a script for the following requirement in awk, grep, sed or perl. Buuuu xxx bbb Kmmmm rrr ssss uuuu Kwwww zzzz ccc Roooowwww eeee Bxxxx jjjj dddd Kuuuu eeeee nnnn Rpppp cccc vvvv cccc Rhhhhhhyyyy tttt Lhhhh rrrrrssssss Bffff mmmm iiiii Ktttt... (5 Replies)
Discussion started by: kumarn
5 Replies

10. Shell Programming and Scripting

split string help

could anyone help in running split cmd split("String1,outputArray,"delimiter); with sample script?. for eg i have abc-def-ghi-sdf- my ultimate aim of asking this is i have a string containing hypens, i want to get the string before last n(2) Hypens (4 Replies)
Discussion started by: senthilk615
4 Replies
Login or Register to Ask a Question