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.
# 1  
Old 09-28-2012
Inserting into long delimited string using perl.

Hi,

I have a very long pipe delimited string. The length of the string could vary. For example:

Code:
 
START|one|two|three|four|five|six|seven
START|one|two|three|four|five|six|seven|eight|nine
START|one|two|three|four

I want to replace in the third occurence of string with another string. For example:

Code:
 
START|one|two|here|four|five|six|seven
START|one|two|here|four|five|six|seven|eight|nine
START|one|two|here|four

How do I do this in PERL.

Remember that the occurance to be replaced could be 99th from beginning and the whole string could be delimited with pipe upto say 200 times.
So simple regex with three four entries would not suffice.
# 2  
Old 09-28-2012
why not with sed..?

Code:
sed 's/pattern/replace_pattern/3'  file

# 3  
Old 09-28-2012
Like I said I am doing it in perl code. It also has the substitution feature.
Can you write the search pattern. It needs to be generic in nature meaning it should replace the 54th occurance while required and 99th occurance when required. In short it should substitute any nth occurance.
# 4  
Old 09-28-2012
Quote:
cat 123.txt
START|one|two|three|four|five|six|seven
START|one|two|three|four|five|six|seven|eight|nine
START|one|two|three|four

perl -lne 'BEGIN{$occurence = 3 }{ @out=split /\|/ ; $out[$occurence] = "here" ; print join "|" , @out } ' 123.txt
START|one|two|here|four|five|six|seven
START|one|two|here|four|five|six|seven|eight|nine
START|one|two|here|four

---------- Post updated at 04:00 AM ---------- Previous update was at 04:00 AM ----------

Quote:
perl -lne 'BEGIN{$occurence = 3 }{ @out=split /\|/ ; $out[$occurence] = "here" ; print join "|" , @out } ' 123.txt
START|one|two|here|four|five|six|seven
START|one|two|here|four|five|six|seven|eight|nine
START|one|two|here|four
# 5  
Old 09-28-2012
Appreciate the response.
Can't it be done via normal regex substitution?

I am saying this because the actual string which I will substitute is not so simple but complex and I cannot simply join it with pipes. I am not looking to split and then join the line but to simple insert at correct position.

Last edited by som.nitk; 09-28-2012 at 06:15 AM..
# 6  
Old 09-28-2012
what is problem using sed..?
# 7  
Old 09-28-2012
This is not a one liner problem to be solved with sed.
I am coding with perl and want some perl code preferably substitution.
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