Match a char with duplicates in a line and replace one of them


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Match a char with duplicates in a line and replace one of them
# 1  
Old 05-22-2014
Match a char with duplicates in a line and replace one of them

Hi,

i have a huge file that need to check for a pattern that occur more than once in a line like below:-

Code:
#lkk>cd-m>A0DV0>192.134.1.1 blablabladsdjsk
jshdfskfslfs
#lqk>cd-m>A1SV0>192.14.11.1 blalalbnalablab
balablablajakjakjakja
#pldqw>sf-w>PH67FR>168.55.1.1 balablabala
bkablablabablablablabalna
blablablaba

there are 3 ">" in the first line for each record. I need to change the third one to "whitespace" and the output shuld be like this:-

Code:
#lkk>cd-m>A0DV0 192.134.1.1 blablabladsdjsk
jshdfskfslfs
#lqk>cd-m>A1SV0 192.14.11.1 blalalbnalablab
balablablajakjakjakja
#pldqw>sf-w>PH67FR 168.55.1.1 balablabala
bkablablabablablablabalna
blablablaba

I don't know how to check for the third ">" and change it accordingly. the code that i am using will change all the ">". I used sed and awk to do that. anyone could help me with awk? thanks
# 2  
Old 05-22-2014
Try :

Code:
$ awk --re-interval 'match($0,/>[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/){$0 = substr($0,1,RSTART-1) OFS substr($0,RSTART+1)}1' file

#lkk>cd-m>A0DV0 192.134.1.1 blablabladsdjsk
jshdfskfslfs
#lqk>cd-m>A1SV0 192.14.11.1 blalalbnalablab
balablablajakjakjakja
#pldqw>sf-w>PH67FR 168.55.1.1 balablabala
bkablablabablablablabalna
blablablaba

---------- Post updated at 11:52 PM ---------- Previous update was at 11:38 PM ----------

OR this also might work

Code:
$ awk 'ORS=!(NR%3)?FS:">"' RS=">" file
#lkk>cd-m>A0DV0 192.134.1.1 blablabladsdjsk
jshdfskfslfs
#lqk>cd-m>A1SV0 192.14.11.1 blalalbnalablab
balablablajakjakjakja
#pldqw>sf-w>PH67FR 168.55.1.1 balablabala
bkablablabablablablabalna
blablablaba

This User Gave Thanks to Akshay Hegde For This Post:
# 3  
Old 05-22-2014
Hi Akshay Hedge,

Tried your codes. The first one give me this error
Code:
awk: not an option: --re-interval

but the second one worked perfectly and easier to understand. I will look for "re-interval" information as this is the first time i heard this term. Thanks a lot for your help.
# 4  
Old 05-22-2014
Sorry I forgot to mention, that you should have gnu awk to use --re-interval
This User Gave Thanks to Akshay Hegde For This Post:
# 5  
Old 05-22-2014
Code:
sed 's/>/ /3' file

This User Gave Thanks to in2nix4life For This Post:
# 6  
Old 05-22-2014
Hi in2nix4life,

This works perfectly too. thanks Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Replace \n char true line Separator

Unix File is pipe delimited with 17 fields. We may get extra pipes in data also. We may get \n char (1 or more \n in one field or multi fileds) in data in any field. Need to replace \n true ( line separator) with 'space and bell char space' chars (' \a ') Not data \n. Input:... (1 Reply)
Discussion started by: rajeshkumare
1 Replies

2. Shell Programming and Scripting

Rearrange or replace only the second line after pattern match or pattern match

Im using the command below , but thats not the output that i want. it only prints the odd and even numbers. awk '{if(NR%2){print $0 > "1"}else{print $0 > "2"}}' Im hoping for something like this file1: Text hi this is just a test text1 text2 text3 text4 text5 text6 Text hi... (2 Replies)
Discussion started by: invinzin21
2 Replies

3. Shell Programming and Scripting

I need to know how to replace a line after a pattern match with an empty line using SED

Hi How Are you? I am doing fine! I need to go now? I will see you tomorrow! Basically I need to replace the entire line containing "doing" with a blank line: I need to the following output: Hi How Are you? I need to go now? I will see you tomorrow! Thanks in advance.... (1 Reply)
Discussion started by: sags007_99
1 Replies

4. Shell Programming and Scripting

Help with replace line based on specific pattern match

Input file data20714 7327 7366 detail data20714 7327 7366 main data250821 56532 57634 detail data250821 57527 57634 main data250821 57359 57474 main data250821 57212 57301 main data250821 57140 57159 detail data250821 56834 57082 main data250821 56708 56779 main ... (3 Replies)
Discussion started by: perl_beginner
3 Replies

5. Shell Programming and Scripting

search pattern and replace x-y characters in nth line after every match

Hi, I am looking for any script which can do the following. have to read a pattern from fileA and copy it to fileB. fileA: ... ... Header ... ... ..p1 ... ... fileB: .... .... Header (3 Replies)
Discussion started by: anilvk
3 Replies

6. Shell Programming and Scripting

In vi editor I want to replace next line char by space

in vi editor I want to replace next line char by space help me eg: input: 123 123 123 output: 123 123 123 (5 Replies)
Discussion started by: RahulJoshi
5 Replies

7. Shell Programming and Scripting

Sed scripting, match text within line and replace

New to sed... Have a file foo.txt (below). Need to replace text on 2 lines, but can only feed sed the first few characters of each line (all lines are unique). So, in my example, I have put '$' in place of what I need to figure out how to feed the whole line. What I have thus far: sed -e... (6 Replies)
Discussion started by: boolean2222
6 Replies

8. Shell Programming and Scripting

SED: Place char at starting and replace selected line

Hello Experts, I am working on a small file editing script. Since all experts here are very generous to give me the complete code, I would take up the problem in steps so that I ensure my opportunity to learn. AIM: The script has some commented and some uncommented lines. I need to : ... (2 Replies)
Discussion started by: hkansal
2 Replies

9. UNIX for Dummies Questions & Answers

match a character in a line and replace

Hi, I have a file with large number of records. Sample below: 123456789QWERT2U 2 erter 987123678ZXCVB6Y 5 7689 934567123GHJKUI4O 7 - -- -- I want the 16th character in each record to be replaced with the below as follows;so 2 will become K, 6 will become O and 4 will become... (3 Replies)
Discussion started by: er_ashu
3 Replies

10. Shell Programming and Scripting

sed - Replace Line which contains the Pattern match with a new line

I need to replace the line containing "STAGE_DB" with the line "STAGE_DB $DB # database that contains the table being loaded ($workingDB)" Here $DB is passed during the runtime. How can I do this? Thanks, Kousikan (2 Replies)
Discussion started by: kousikan
2 Replies
Login or Register to Ask a Question