Remove the first match only in a file using sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove the first match only in a file using sed
# 1  
Old 08-27-2014
Remove the first match only in a file using sed

I'm trying to remove the first match only of 2Z694 from an xml file and replace with a blank

File Example:
Code:
</Phoenix_Response_Data>
<Bundle_Name_Primary>2Z694</Bundle_Name_Primary>
<Bundle_Name>2Z694</Bundle_Name>
</Phoenix_Response_Data>

tried using:
Code:
 sed -e 's/'2Z694'/''/1'

but this blanks out all instances of 2Z694.

Last edited by bartus11; 08-27-2014 at 07:25 PM.. Reason: Please use [code][/code] tags.
# 2  
Old 08-27-2014
Does it have to be sed?
Code:
perl -0pe 's/2Z694//' file.xml

# 3  
Old 08-28-2014
Code:
sed '/2Z694/,$ {1,1 s/2Z694/ /}' file
</Phoenix_Response_Data>
<Bundle_Name_Primary> </Bundle_Name_Primary>
<Bundle_Name>2Z694</Bundle_Name>
</Phoenix_Response_Data>

# 4  
Old 08-28-2014
Thanks for the reply Rudi but your suggestion returns an error
Code:
sed: 1: "/2Z694-00609/,$ {1,1 s/ ...": bad flag in substitute command: '}'

Below is the file and I'm trying to strip out the first and only match of 2Z694-00609

HTML Code:
<?xml version="1.0" encoding="UTF-8"?><SI_Response><SI_Response_Header><Command_Name>Comm.Get.BundleList</Command_Name><Supported>SUPPORTED</Supported><Responses_Sent>1</Responses_Sent><Responses_Expected>1</Responses_Expected><App_Name>phoenixd-7700</App_Name><Sequence_Number>1</Sequence_Number><Error_Code>0</Error_Code><Error_Msg></Error_Msg></SI_Response_Header><SI_Response_Data>
<Phoenix_Response>
<Phoenix_Response_Header version="1.0"></Phoenix_Response_Header>
<Phoenix_Response_Data>
<Bundle_Name>[B]2Z694-00609[/B]</Bundle_Name>
<Bundle_Name_Primary>2Z694-00609</Bundle_Name_Primary>
</Phoenix_Response_Data>
</Phoenix_Response>
</SI_Response_Data></SI_Response>
This is what I have tried ...
Code:
BundleName=`cat /Phoenix/Configuration/bundlelist.txt | grep -m 1 "2Z694" | awk -F "[>,<]" '{print$3}'`

echo Bundlelist found is $BundleName

cp -f /Phoenix/Configuration/bundlelist.txt /Phoenix/bundlelist1.txt

sed '/'2Z694-00609'/,$ {1,1 s/'$2Z694-00609'/ /}' /Phoenix/bundlelist1.txt > /Phoenix/Configuration/bundlelist.txt

Any other ideas, thanks again ...

Last edited by rbatte1; 08-28-2014 at 12:59 PM.. Reason: Added CODE & HTML tags
# 5  
Old 08-28-2014
With awk:
Code:
awk '!p{p=sub("2Z694-00609",x)}1' file

These 2 Users Gave Thanks to Franklin52 For This Post:
# 6  
Old 08-28-2014
Must be a different sed version:
Code:
sed '/2Z694-00609/,$ {1,1 s/2Z694-00609/ /}' file
<?xml version="1.0" encoding="UTF-8"?><SI_Response><SI_Response_Header><Command_Name>Comm.Get.BundleList</Command_Name><Supported>SUPPORTED</Supported><Responses_Sent>1</Responses_Sent><Responses_Expected>1</Responses_Expected><App_Name>phoenixd-7700</App_Name><Sequence_Number>1</Sequence_Number><Error_Code>0</Error_Code><Error_Msg></Error_Msg></SI_Response_Header><SI_Response_Data>
<Phoenix_Response>
<Phoenix_Response_Header version="1.0"></Phoenix_Response_Header>
<Phoenix_Response_Data>
<Bundle_Name> </Bundle_Name>
<Bundle_Name_Primary>2Z694-00609</Bundle_Name_Primary>
</Phoenix_Response_Data>
</Phoenix_Response>
</SI_Response_Data></SI_Response>

Code:
sed --version
sed (GNU sed) 4.2.2

# 7  
Old 08-28-2014
Thanks Franklin, awk works as expected. Rudi I'm running off Mac OS X 10.8 so see version different. Thanks again
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using awk to remove lines from file that match text

I am trying to remove each line in which $2 is FP or RFP. I believe the below will remove one instance but not both. Thank you :). file 12 123 FP 11 10 RFP awk awk -F'\t' ' $2 != "FP"' file desired output 12 11 (6 Replies)
Discussion started by: cmccabe
6 Replies

2. Shell Programming and Scripting

sed to remove newline chars based on pattern mis-match

Greetings Experts, I am in AIX; I have a file generated through awk after processing the input files. Now I need to replace or remove the new-line characters on all lines that doesn't have a ; which is the last character on the line. I tried to use sed 's/\n/ /g' After checking through the... (6 Replies)
Discussion started by: chill3chee
6 Replies

3. Shell Programming and Scripting

sed : replace Nth match in a file

I have a situation where a file "config.txt" looks like this Servername: OS: Serername: OS: Servername: OS: .... .... ... Servername: OS: looking for the sed syntax to replace the "Nth" occurrence of Servername (i would apply the same logic to OS as well), want to replace the Nth... (4 Replies)
Discussion started by: alldbest
4 Replies

4. Shell Programming and Scripting

Awk-sed help : to remove first and last line with pattern match:

awk , sed Experts, I want to remove first and last line after pattern match "vg" : I am trying : # sed '1d;$d' works fine , but where the last line is not having vg entry it is deleting one line of data. - So it should check for the pattern vg if present , then it should delete the line ,... (5 Replies)
Discussion started by: rveri
5 Replies

5. Shell Programming and Scripting

sed print from last occurrence match until the end of file

Hi, i have file f1.txt with data like: CHECK a b CHECK c d CHECK e f JOB_START .... I want to match the last occurrence of 'CHECK' until the end of the file. I can use awk: awk '/^CHECK/ { buf = "" } { buf = buf "\n" $0 } END { print buf }' f1.txt | tail +2Is there a cleaner way of... (2 Replies)
Discussion started by: ysrini
2 Replies

6. Shell Programming and Scripting

Insert few lines above a match using sed, and within a perl file.

Greetings all, I am trying to match a string, and after that insert a few lines above that match. The string is "Version 1.0.0". I need to insert a few lines ONLY above the first match (there are many Version numbers in the file). The rest of the matches must be ignored. The lines I need to... (2 Replies)
Discussion started by: nagaraj s
2 Replies

7. Shell Programming and Scripting

SED - Match a line from a File

Hi, I want to match a line which exists in a file. I have written a test script similar to below - The content of the file file.txt would be like this - /usr/bin/1234.xcf /usr/bin/3456.xcf /usr/bin/7897.xcf /usr/bin/2345.xcf out=`sed -n '\/usr\/bin\/7897.xcf/p' file.txt 2>&1`... (3 Replies)
Discussion started by: angshuman_ag
3 Replies

8. Shell Programming and Scripting

Insert text file only after the first match with SED

Hello, I'm new in Shell scripting but i should write a script, which inserts the license header out of a txt-File into the files in our Projekt. For the Java classes it runs without Problems but for XML files not. At xml-files i have to put the license Header after the xml-Header (?xml... (1 Reply)
Discussion started by: PhoenixONE
1 Replies

9. Shell Programming and Scripting

sed/awk help to match list of patterns and remove from org file

Hi, From the pattern mentioned below remove lines based on pattern range. Conditions 1 Look For all lines starting with ALTER TABLE and Ending with ; and contains the word MOVE.I wanto to remove these lines from the file sample below. Note : The above pattern list could be found in... (1 Reply)
Discussion started by: rajan_san
1 Replies

10. Shell Programming and Scripting

sed over writes my original file (using sed to remove leading spaces)

Hello and thx for reading this I'm using sed to remove only the leading spaces in a file bash-280R# cat foofile some text some text some text some text some text bash-280R# bash-280R# sed 's/^ *//' foofile > foofile.use bash-280R# cat foofile.use some text some text some text... (6 Replies)
Discussion started by: laser
6 Replies
Login or Register to Ask a Question