Replacing nth occurrence


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing nth occurrence
# 1  
Old 12-25-2011
Replacing nth occurrence

There is already one thread with the same heading. But here the case is little different.

i have a line which have a field separator '|'

Code:
 abc|def|ghi|jkl|mno|pqr|stu|vwx|yz

I want to replace every 3rd occurance + next character with the same + newline character.. I mean i want to enter a newline character after the next character of the third pipe

output should be like this
Code:
abc|def|ghi|j
kl|mno|pqr|s
tu|vwx|yz

I tried the below command, but using which i cant include the next letter to the 3rd field separator

Code:
 print 'abc|def|ghi|jkl|mno|pqr|stu|vbbwx|yz' |  perl -pe's/(\|)/++$c%3?$1:"$1\n"/ge'

which will give the output like this
Code:
abc|def|ghi|
jkl|mno|pqr|
stu|vbbwx|yz

# 2  
Old 12-25-2011
Code:
echo "abc|def|ghi|jkl|mno|pqr|stu|vwx|yz" | perl -e 'chomp($x=<>); $x =~ s/(.*?\|.*?\|.*?\|.)/$1\n/g; print $x'


Last edited by balajesuri; 12-25-2011 at 12:32 PM..
# 3  
Old 12-25-2011
that works.. thanks, Bt how can increase the repetition from 3 to more. as i want a newline character after 146 count.. Smilie
# 4  
Old 12-26-2011
Code:
echo 'abc|def|ghi|jkl|mno|pqr|stu|vbbwx|yz' |  perl -pe 's/(.*?\|.)/++$c%3?$1:"$1\n"/ge'


Last edited by balajesuri; 12-26-2011 at 12:15 AM..
# 5  
Old 12-28-2011
Code:
my $str='abc|def|ghi|jkl|mno|pqr|stu|vwx|yz';
$str =~ s/([|](?{$cnt++}).)/($cnt%3==0)?$1."\n":$1/eg;
print $str,"\n";


Last edited by Franklin52; 12-29-2011 at 10:37 AM..
# 6  
Old 12-28-2011
Code:
echo "abc|def|ghi|jkl|mno|pqr|stu|vwx|yz" | 
awk -vFS=''  '{ 
 for(i=1;i<=NF;i++) 
 {   
   if(j==3)
   { 
       $i=$i"\n";j=0;
   } 
   printf("%s", $i); 
   if($i=="|") 
   {
     j++
   }  
 }  
              }
END{ print "\n"}  '


Last edited by Franklin52; 12-29-2011 at 10:37 AM.. Reason: Please use code tags for data and code samples, thank you
# 7  
Old 12-29-2011
Hi ratheeshjulk,

One way:
Code:
$ echo 'abc|def|ghi|jkl|mno|pqr|stu|vbbwx|yz' |  perl -pe's/((?:[^|]*\|){3}\w)/$1\n/g'
abc|def|ghi|j
kl|mno|pqr|s
tu|vbbwx|yz

Regards,
Birei
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Trim after nth occurrence with loop

Hello, Below command trims right after the nth occurrence of a string. When I try in while loop, it is not working. In Terminal IFS=/ ; read -ra val < Textfile ; echo "${val:0:3}" It gives only one line: sunday/monday/tuesday Textfile: sunday/monday/tuesday/wednesday/thursday... (2 Replies)
Discussion started by: baris35
2 Replies

2. Shell Programming and Scripting

Replacing nth field with nth_text for each line in a file

Hi All, I am very new to shell scripting and tried to search this in the forum but no luck. Requirment: I have an input file which is comma separated. I need to replace the value in 4th column with another value. This has to happen for all the lines in the file. Sample data: Input... (2 Replies)
Discussion started by: arunkumarsd
2 Replies

3. Shell Programming and Scripting

How to insert file contents after nth occurrence of a string using sed?

Hi, I would like to know how, using sed, be able to insert contents of file2 in file1 after say the second occurrence of a given string? e.g. > cat file1 banana apple orange apple banana pear tangerine apple > cat file2 I don't like apples What would be the sed command to insert... (5 Replies)
Discussion started by: dimocn
5 Replies

4. Shell Programming and Scripting

Extract the text between the nth occurrence of square brackets

Please can someone help with this? I have a file with lines as follows: word1 word2 word3 word4 word5 word6 word7 word8 word1 word2 word3 word4 word5 word6 word7 word8 word1 word2 word3 word4 word5 word6 word7 word8 word1 word2 word3 word4 word5 word6 word7 word8 When I use the... (7 Replies)
Discussion started by: Subhadeep_Sahu
7 Replies

5. Shell Programming and Scripting

Insert new pattern in newline after the nth occurrence of a line pattern - Bash in Ubuntu 12.04

Hi, I am getting crazy after days on looking at it: Bash in Ubuntu 12.04.1 I want to do this: pattern="system /path1/file1 file1" new_pattern=" data /path2/file2 file2" file to edit: data.db - I need to search in the file data.db for the nth occurrence of pattern - pattern must... (14 Replies)
Discussion started by: Phil3759
14 Replies

6. Shell Programming and Scripting

How to output all lines following Nth occurrence of string

Greetings experts. Searched the forums (perhaps not hard enough?) - Am searching for a method to capture all output from a log file following the nth occurrence of a known string. Background: Using bash, I want to monitor my Oracle DB alert log file. The script will count the total # of... (2 Replies)
Discussion started by: cjtravis
2 Replies

7. Shell Programming and Scripting

find string nth occurrence in file and print line number

Hi I have requirement to find nth occurrence in a file and capture data from with in lines (between lines) Data in File. <QUOTE> <SESSION> <ATTRIBUTE NAME='Parameter Filename' VALUE='file1.parm'/> <ATTRIBUTE NAME='Service Name' VALUE='None'/> </SESSION> <SESSION> <ATTRIBUTE... (6 Replies)
Discussion started by: tmalik79
6 Replies

8. Shell Programming and Scripting

Replacing nth occurence

Hi My input file is like this for eg: abc abc abc abc abc abc i would like to replace "abc" with "cba" where the occurrence is divisible by 2 of eg here 2nd, 4th and 6th occurence shud be replace can anyone suggest in awk or sed (11 Replies)
Discussion started by: raghav288
11 Replies

9. Shell Programming and Scripting

Replacing a string in nth line

Hello All, How to replace a string in nth line of a file using sed or awk. For Ex: test.txt Line 1 : TEST1 TEST2 TEST3 Line 2 : TEST1 TEST2 TEST3 TEST4 Line 3 : TEST1 TEST2 TEST3 TEST5 Line 4 : TEST1 TEST2 TEST3 TEST6 Line 5 : TEST1 TEST2 TEST3 TEST7 i want to go to 4th line of a... (1 Reply)
Discussion started by: maxmave
1 Replies

10. UNIX for Dummies Questions & Answers

Finding nth occurrence in line and replacing it

Hi, I have several files with data that have to be imported to a database. These files contain records with separator characters. Some records are corrupt (2 separators are missing) and I need to correct them prior to importing them into the db. Example: ... (5 Replies)
Discussion started by: stresing
5 Replies
Login or Register to Ask a Question