sed script - transform 1st matching line only


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed script - transform 1st matching line only
# 1  
Old 03-05-2010
sed script - transform 1st matching line only

How do transform only the first line that matches my regex? I've tried q but it just quit my script in the middle.
# 2  
Old 03-05-2010
For transforming the line in sed,you can use substitute command.For changing the first line alone.You use the following command.

Code:
sed -r '1s/^[0-9]+$/Numerical Values/' <filename>

The above command will change the first line into numerical values if it contains the numbers in the first line.
# 3  
Old 03-05-2010
Generally the sed command's substitution with the given pattern will replace the first occurrence of the match unless we explicitly specified the g flag.
# 4  
Old 03-05-2010
Yeah thillai,in substitute command if we didn't specify global flag,then it will replace only the first occurrence.But,I don't know why you have given this answer here?Did you read the question?Otherwise,you're saying in my solution,we need to give g flag?
# 5  
Old 03-05-2010
Well, I think the original question asked was to be able to replace only the FIRST macth of the reg expression and not necessarily the first line, which means, it can be match anywhere in the file but replace only the first occurance only..Confirmations?
# 6  
Old 03-05-2010
Thanks for the responses guys!

Yes, my question is the 1st regex match anywhere in the file. Not the 1st line of the file. I'm trying to do something like this:

Code:
/regex/ {
              transform only the first matching line
}

do some other stuff

# 7  
Old 03-05-2010
Try this:

Code:
awk 'BEGIN{cnt=0}$0 ~ /regexpr/ && cnt == 0{gsub(/regexpr/,"ReplacementString");cnt++}1' filename


cheers,
Devaraj Takhellambam
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replacing lines matching a multi-line pattern (sed/perl/awk)

Dear Unix Forums, I am hoping you can help me with a pattern matching problem. What am I trying to do? I want to replace multiple lines of a text file (that match a multi-line pattern) with a single line of text. These patterns can span several lines and do not always have the same number of... (10 Replies)
Discussion started by: thefang
10 Replies

2. UNIX for Dummies Questions & Answers

Sed: Adding new line after matching pattern

Hi I just wanted to add a new line after every matching pattern: The method doing this doesn't matter, however, I have been using sed and this is what I tried doing, knowing that I am a bit off: sed 'Wf a\'/n'/g' Basically, I want to add a new line after occurrence of Wf. After the line Wf... (5 Replies)
Discussion started by: MIA651
5 Replies

3. UNIX for Dummies Questions & Answers

sed - combination of line deletion and pattern matching

I want to delete all the blank lines from a file before a certain line number. e.g. Input file (n: denotes line number) 1: a 2: 3: b 4: c 5: 6: d I want to delete all blank lines before line number 3, such that my output is: a b c d I see that sed '/^$/d' in_file works... (9 Replies)
Discussion started by: jawsnnn
9 Replies

4. Shell Programming and Scripting

HELP: Shell Script to read a Log file line by line and extract Info based on KEYWORDS matching

I have a LOG file which looks like this Import started at: Mon Jul 23 02:13:01 EDT 2012 Initialization completed in 2.146 seconds. -------------------------------------------------------------------------------- -- Import summary for Import item: PolicyInformation... (8 Replies)
Discussion started by: biztank
8 Replies

5. Shell Programming and Scripting

sed or awk delete character in the lines before and after the matching line

Sample file: This is line one, this is another line, this is the PRIMARY INDEX line l ; This is another line The command should find the line with “PRIMARY INDEX” and remove the last character from the line preceding it (in this case , comma) and remove the first character from the line... (5 Replies)
Discussion started by: KC_Rules
5 Replies

6. Shell Programming and Scripting

How to use sed to modify a line above or below matching pattern?

I couldn't figure out how to use sed or any other shell to do the following. Can anyone help? Thanks. If seeing a string (e.g., TODAY) in the line, replace a string in the line above (e.g, replace "Raining" with "Sunny") and replace a string in the line below (e.g., replace "Reading" with... (7 Replies)
Discussion started by: sprinner
7 Replies

7. UNIX for Dummies Questions & Answers

Matching pattern script (sed or awk?)

Hi Guys, I am new to the forum and to scripting so bear with me. Thanks, Gary. I have 3 files - file1, file2, file3 I am trying to come up with a script that will check the output of these files and if the 1st nine fields are matched in all 3 files, echo "The following string had been... (2 Replies)
Discussion started by: gazza-o
2 Replies

8. Shell Programming and Scripting

SED Question: Search and Replace start of line to matching pattern

Hi guys, got a problem here with sed on the command line. If i have a string as below: online xx:wer:xcv: sdf:/asdf/http:https-asdfd How can i match the pattern "http:" and replace the start of the string to the pattern with null? I tried the following but it doesn't work: ... (3 Replies)
Discussion started by: DrivesMeCrazy
3 Replies

9. Shell Programming and Scripting

sed find matching pattern delete next line

trying to use sed in finding a matching pattern in a file then deleting the next line only .. pattern --> <ad-content> I tried this but it results are not what I wish sed '/<ad-content>/{N;d;}' akv.xml > akv5.xml ex, <Celebrant2First>Mickey</Celebrant2First> <ad-content> Minnie... (2 Replies)
Discussion started by: aveitas
2 Replies

10. Shell Programming and Scripting

Matching everything on a line using sed

I having a little trouble. I want to substitute whatever is on the first line no matter what is on it with the word BEGINNING. So on the first line, the only word on it is BEGINNING I tried the following: sed '1,1s/^ *.*$/BEGINNING/g' $FILE It works only if the 1st line starts with a... (2 Replies)
Discussion started by: quixoticking11
2 Replies
Login or Register to Ask a Question