Replace second to last but one consecutive occurence


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace second to last but one consecutive occurence
# 1  
Old 04-09-2014
Code Replace second to last but one consecutive occurence

Hi Unix Mates,

I have requirement where in which I have to replace second to last -1 occurence of space with a word and last occurence of a space with a word and a space,input would be a file.

Desired result:
Second consecutive to last-1 occurence of space replace with: HRU
LAST consecutive occurence : WRU[SPACE]

input
Code:
cat myfile
Wow what a[8spaces]wonderful feeling to be alive

Code:
<Code>
Wow what a HRUHRUHRUHRUHRUHRUHRUWRU wonderful WRU feeling to be alive

Thanks iN ADVANCE!!!

Last edited by Kingcobra; 04-09-2014 at 06:45 AM.. Reason: icode tags to code tags
# 2  
Old 04-09-2014
Hello Kingcobra,

Following may help you in same.

Code:
echo "Wow what a wonderful feeling to be alive" | awk '{for(i=1;i<=NF;i++){if(i==4){print "HRUHRUHRUHRUHRUHRUHRUWRU"} {if(i==5){print "WRU"}} {if(i!=4 || i!=5){print $i}}}}' ORS=" "

Output will be as follows.

Code:
Wow what a HRUHRUHRUHRUHRUHRUHRUWRU wonderful WRU feeling to be alive



Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 04-09-2014
@Kingcobra: could you elaborate what a consecutive space means to you. Is your input file is incorrect? What have you tried?


--
@Ravindersingh that is not a solution...
This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 04-09-2014
Quote:
Originally Posted by Scrutinizer
@Kingcobra: could you elaborate what a consecutive space means to you. Is your input file is incorrect? What have you tried?


--
@Ravindersingh that is not a solution...
Thanks mates.

Actaullt my spaces got supressed when I have put it inside code tag
Help required is
First space --->Do nothing
Second consecutive space--->REPLACE WITH HBU
third space --->REPLACE WITH HBU



n-1th consecutive space ----> REPLACE WITH HBU
Last space in consecutive sequence--->REPLACE WITH WBU[space]



Thanks Scrutinizer,Singh for your helpSmilie
# 5  
Old 04-09-2014
Try:
Code:
sed -e 's/\(  *\) /\1WRU /g' -e :a -e 's/  \([HW]RU\)/ HRU\1/;ta' file

I believe there should be six HRU's (2nd until last -1) not 7, correct?
This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 04-09-2014
Bulls eye!!! Scrutinizer you are an amazing guy.Proud to be in this forum
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

sed command to replace consecutive double quotes

I need to replace consecutive double quotes in a csv file, the data in the file is enclosed in double quotes but there are some places where the quotes are repeating Example is below Incoming data is : "Pacific Region"|"PNG"|"Jimmy""|""| Need output as: "Pacific... (10 Replies)
Discussion started by: abhilashnair
10 Replies

2. AIX

Replace consecutive occurrence of string in same line

Hi All, I have a requirement to replace consecutive occurence of same string nedd to be replaced. Below is the input and desired output. Input: --------- 123.5|ABC|.|.|. 234.4|DEF|.|.|.|.|.| Output: --------- 123.5|ABC|||. 234.4|DEF||||| so basically "|.|" need to be replaced with... (9 Replies)
Discussion started by: ureddy
9 Replies

3. Shell Programming and Scripting

How to filter out consecutive occurence of digits from file?

Hello, I have a text file containing the following for example: This number 123456 is in line number one. Line number two contains -333444 number. The third line has a 111111 in between and also 435. Line number four has :444444 in it. Line five has a in front like a555555. how can i... (5 Replies)
Discussion started by: james2009
5 Replies

4. Shell Programming and Scripting

Grep 2 consecutive lines and replace the second line in a file

I have a file lake this cat ex1.txt </DISCOUNTS> <B2B_SPECIFICATION elem="0"> <B2B_SPECIFICATION elem="0"> <DESCR>Netti 2 </DESCR> <NUMBER>D02021507505</NUMBER> </B2B_SPECIFICATION> <B2B_SPECIFICATION elem="1"> <DESCR>Puhepaketti</DESCR>... (2 Replies)
Discussion started by: Dhoni
2 Replies

5. Shell Programming and Scripting

awk - find first occurence and replace it

Hello, I have a requirement to replace the whole string with first occurence of value of a key-value pair. I have to do this inside awk, as the data I need to work is inside an awk loop. value of my variable(say LogText) looks like: LogText= date=04Mar message=hello1 name=caq... (4 Replies)
Discussion started by: cool.aquarian
4 Replies

6. Shell Programming and Scripting

Print between patterns - first occurence, second occurence etc

I have a file # cat asasas AAAAAA 11 22 33 44 BBBBB NILNILNIL AAAAAA 22 33 44 55 66 77 88 BBBBB NILNILNIL (2 Replies)
Discussion started by: anil510
2 Replies

7. Shell Programming and Scripting

[Solved] Find and replace till nth occurence of a special character

Hi, I have a requirement to search for a pattern in each line in a file and remove the in between words till the 3rd occurrence of double quote ("). Ex: CREATE TABLE "SCHEMANAME"."AMS_LTV_STATUS" (Note: "SCHEMANAME" may changes for different schemas. Its not a fixed value) I need to... (2 Replies)
Discussion started by: satyaatcgi
2 Replies

8. Shell Programming and Scripting

Script to replace last instance of . between two consecutive = sign by ,

Suppose you have a line like this: cn=user.blr.ou=blr.india.o=company The line should be converted like this: cn=user.blr,ou=blr.india,o=comapny Was wondering how to do that using shell script. Please use tags where appropriate, thank you (4 Replies)
Discussion started by: saurabhkoar
4 Replies

9. Shell Programming and Scripting

Replace matching nth occurence

Hi for eg my file has: val1 | val2 | val1 | val2 | val1 | val2 | val1 | val2 | here i need to replace '|' with '|\n' where the occurence of '|' is divisble by 2 so that the output comes like this val1 | val2 | val1 | val2 | val1 | val2 | val1 | val2 | Requesting suggestions in... (1 Reply)
Discussion started by: raghav288
1 Replies

10. Shell Programming and Scripting

Replace string B depending on occurence of string A

Depending upon the occurence of string 'xyz', I want to remove -t from the input file. There is not a fixed length input file. Any suggestions Input file: this is xyz line -t of the data this is line 2 of -t of the data xyz this is line 3 of -t the file this is line xyz of the -t file... (1 Reply)
Discussion started by: hemangjani
1 Replies
Login or Register to Ask a Question