Problem editting the first occurence of a pattern in the first uncommented line


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Operating Systems Linux Problem editting the first occurence of a pattern in the first uncommented line
# 1  
Problem editting the first occurence of a pattern in the first uncommented line

Hi

I have to replace a pattern found in the first uncommented line in a file. The challenge I'm facing is there are several such similar lines but I have to edit only the first uncommented line.

Eg:

#this is example
#/root/xyz:Old_Pattern
/root/xyz:Old_Pattern
/root/xyz:Old_Pattern


Want to change it to

#this is example
#/root/xyz:Old_Pattern
/root/xyz:Changed_Pattern
/root/xyz:Old_Pattern

The only delimiter i can use is ":"

The present code im using is
`sed -ie '1,/^#.*.:.*/!s/:.*/:Changed_Pattern/1' /root/tmp.file`

however it is editing all the similar uncommented lines

Eg; happening
#this is example
#/root/xyz:Old_Pattern
/root/xyz:Changed_Pattern
/root/xyz:Changed_Pattern

Pls can someone help
Thank You
# 2  
Pls use code tags as advised.
Try
Code:
$ awk -F: '!/^#/ && !Done{sub("Old_Pattern","Changed_Pattern",$2);Done++}1' OFS=":" file
#this is example
#/root/xyz:Old_Pattern
/root/xyz:Changed_Pattern
/root/xyz:Old_Pattern

# 3  
You can use sed's q option to quit reading further lines. So try
Code:
$ sed -ie '/^#/!{
> s/:.*/:Changed_Pattern/
> q
> }' inputfile

# 4  
Quote:
Originally Posted by michaelrozar17
You can use sed's q option to quit reading further lines. So try
Code:
$ sed -ie '/^#/!{
> s/:.*/:Changed_Pattern/
> q
> }' inputfile

Wouldn't this lose any further lines?
# 5  
I believe it would not. Could you test it? My machine does not support -i option.
# 6  
Hi RudiC

It works and thanks for your time but I have problem there as I cant give the pattern "Old_Pattern" as that can be anything and I used "*" which didnt work. Im really new to awk so can you help me

---------- Post updated at 03:48 AM ---------- Previous update was at 03:38 AM ----------

Hi michaelrozar17

The code that you have posted deletes all the further lines
# 7  
So you want to set field 2 of first non-comment line to "Changed_Pattern" regardless of its previous value? Try
Code:
$ awk -F: '!/^#/ && !Done{$2="Changed_Pattern");Done++}1' OFS=":" file

Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #466
Difficulty: Medium
NTP was designed by David L. Mills of the University of New York.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to replace line through sed , but without uncommented?

I am looking to replace minimum password value in login.defs file. it does change , but it change the commented one also. please 1. my script to be change 12 in the below line... sed -i '/PASS_MIN_LEN/c\PASS_MIN_LEN 12' /etc/login.defs action- It does change, but it change in 2 place. ... (3 Replies)
Discussion started by: yash_message
3 Replies

2. Shell Programming and Scripting

Extract whole word preceding a specific character pattern with first occurence of the pattern

Hello. Here is a file contents : declare -Ax NEW_FORCE_IGNORE_ARRAY=(="§" ="§" ="§" ="§" ="§" .................. ="§"Here is a pattern =I want to extract 'NEW_FORCE_IGNORE_ARRAY' which is the whole word before the first occurrence of pattern '=' Is there a better solution than mine :... (3 Replies)
Discussion started by: jcdole
3 Replies

3. Shell Programming and Scripting

How to get lines with only one occurence of pattern?

My data 20161220 20:30:01 MODE 1 TEST 1 SOURCE 1 SET 1 20161220 20:30:02 MODE 1 TEST 2 SOURCE 1 SET 1 20161220 20:30:02 MODE 1 TEST 3 SOURCE 1 SET 1 20161220 20:30:02 MODE 1 TEST 1 SOURCE 2 SET 1 20161220 20:30:04 MODE 1 TEST 1 SOURCE 1 SET 1 MODE 1 TEST 2 SOURCE 2 SET 1 20161220 20:30:02... (7 Replies)
Discussion started by: migurus
7 Replies

4. Shell Programming and Scripting

Hos to get the line number of uncommented line from the file

I have few lines in a text file. I am trying to get the line number of uncommented line from the text file using unix shell script. For example : I want the line number of Orange from the below text file. Here expected answer is 4 since the line 2 is commented. Apple #Orange grapes Orange (4 Replies)
Discussion started by: Brennon
4 Replies

5. UNIX for Dummies Questions & Answers

Vi delete line with second occurence of pattern

I have a large file and many lines are duplicated. I'm trying to delete lines with every second occurrence of a pattern. Did tried searching similar question but no luck. I can delete all lines matching pattern with :g/pattern/d but don't want to lose data. Sample pattern to delete... (6 Replies)
Discussion started by: homer4all
6 Replies

6. Shell Programming and Scripting

Match pattern and print the line number of occurence using awk

Hi, I have a simple problem but i guess stupid enough to figure it out. i have thousands rows of data. and i need to find match patterns of two columns and print the number of rows. for example: inputfile abd abp 123 abc abc 325 ndc ndc 451 mjk lkj... (3 Replies)
Discussion started by: redse171
3 Replies

7. Shell Programming and Scripting

Help with using awk to print pattern/occurence

Hi, Do anybody know how to use awk to count the pattern at specific column? Input file M2A928K 419 ath-miR159a,gma-miR159a-3p,ptc-miR159a 60 miR235a . . Output file M2A928K 419 ath-miR159a,gma-miR159a-3p,ptc-miR159a 60 miR235a 3 . . I plan to count how many "miR" in column 3... (2 Replies)
Discussion started by: cpp_beginner
2 Replies

8. Shell Programming and Scripting

Editting each line in a file

Hi Can anyone please help me in resolving my issue. I have a file with entries like this t9787ms 99970 22/08/2010 12:30 /www.google.com t9788ms 99942 22/08/2010 12:40 /www.google.com t4788ms 88942 22/08/2010 01:40 /www.google.com there are around 5 lakh records of this type my requirement... (4 Replies)
Discussion started by: mskalyani
4 Replies

9. Shell Programming and Scripting

Perl onliner to search the last line with an occurence of a pattern

Hi I need a perl onliner which seaches a line starting with a pattern(last occurence) and display it. similar to grep 'pattern' filename | tail -1 in UNIX Ex: I want to display the line starting with "cool" and which is a last occurence adadfadafadf adfadadf cool dfadfadfadfara... (4 Replies)
Discussion started by: ammu
4 Replies

10. UNIX for Dummies Questions & Answers

Pattern occurence in a file

How can we find the number of occurence of a specified pattern in a file? This command would be a part of a shell script. (5 Replies)
Discussion started by: videsh77
5 Replies

Featured Tech Videos