Inserting into long delimited string using perl.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Inserting into long delimited string using perl.
# 8  
Old 09-28-2012
Try:
Code:
#!/usr/bin/perl
use strict;
use warnings;
my $n=2;

while(<>) {
 my $count=0;
 my @arr = split /[|]/,$_,-1;
 foreach (@arr) {
  if(/\Apatt\Z/) {
   $count++
  }
  if($count == $n) {
   s/patt/repl/;
   last
  }
 }
 $_ = join '|', @arr;
 print
}

Replace n, patt and repl as per your requirement.
# 9  
Old 09-28-2012
Quote:
$x="START|one|two|three|four|five|six|seven" ;
$x=~s/((.*?\|){3})(.*?\|)/$1text|/;
print $x

Quote:
START|one|two|text|four|five|six|seven
# 10  
Old 09-28-2012
Thanks! I will try them and get back.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help/Advise please for converting space delimited string variable to comma delimited with quote

Hi, I am wanting to create a script that will construct a SQL statement based on a a space delimited string that it read from a config file. Example of the SQL will be For example, it will read a string like "AAA BBB CCC" and assign to a variable named IN_STRING. I then concatenate... (2 Replies)
Discussion started by: newbie_01
2 Replies

2. Shell Programming and Scripting

Inserting a long string (readable in sed)

Hi everyone, I am trying to insert a single very long string as the first line of a file, So the following sed commands does what I want; sed '1i\"","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"' file.txt Think that all the... (3 Replies)
Discussion started by: hayreter
3 Replies

3. Shell Programming and Scripting

Parsing a long string string problem for procmail

Hi everyone, I am working on fetchmail + procmail to filter mails and I am having problem with parsing a long line in the body of the email. Could anyone help me construct a reg exp for this string below. It needs to match exactly as this string. GetRyt... (4 Replies)
Discussion started by: cwiggler
4 Replies

4. Shell Programming and Scripting

Inserting a string in another sting

Hi Experts, I need to insert a sting into another string at a specified position. Like the below. Regards, Tin-Tin (3 Replies)
Discussion started by: tinufarid
3 Replies

5. Shell Programming and Scripting

How to make a long print string to shotcut form in perl?

print "1.readfromfile\n2.add_ex1(4,5)\n3.add_ex2(11,5)\n4.add_ex3(9,3)\n5.add_ex4(91,4)\n"; How to do it in this form: print "1.readfromfile\n 2.add_ex1(4,5)\n 3.add_ex2(11,5)\n 4.add_ex3(9,3)\n 5.add_ex4(91,4)\n"; (3 Replies)
Discussion started by: cola
3 Replies

6. Shell Programming and Scripting

Perl - Inserting text

Hey, I have 10 lines of text ... And I would like to Insert prefix for each line with static text. perl -pi -e 's/()/$1 TEST$./' data.txt Each line will have different static prefix, Code above works perfectly for 1st line ... I'm just not sure how I can run same command for 2nd line 3rd... (4 Replies)
Discussion started by: NDxiak
4 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

Inserting into first record using perl

hi all... i got some requirment where i need to insert some values into first record of flat file. i am using perl for that job. var1=456 var2=789 echo `perl -p -i -e "s/U/\${var1}U${var2}/g;" myFile.txt` but it is writing into all records which has U.... can anyone help me out in this... (7 Replies)
Discussion started by: shreekrishnagd
7 Replies

9. Shell Programming and Scripting

inserting a character between string

i have a file contains like this: i want to create a script that will insert a comma "." after the 10th character so it would be look like this thanks in advance (5 Replies)
Discussion started by: dakid
5 Replies

10. Shell Programming and Scripting

inserting a String in the file(s)

Hi, I'm a newbee to Unix shell scripting. I want to write a shell script that inserts a new String(name&value pair) into a file(s) at a particular place.I willl have to write one script which when executed should insert a new variable in all the files in that particular directory. Say for eg:... (4 Replies)
Discussion started by: 2tbee
4 Replies
Login or Register to Ask a Question