-2

I have code which will replace the xml tag at a particular line number

 LineNum=`grep -n "Deep" file.xml | cut -d: -f 1 | awk -F '[\t]' '{$NF = $NF -1;}1'`
 sed "${LineNum}s#^<Deep>#<!--<Deep>#" file.xml

While executing the command i am getting below exception

sed -e expression #1, char 7: unknown command `'

Can anyone provide me the solution for this?

asked Mar 10, 2021 at 16:21
11
  • Works for me in sed (GNU sed) 4.2.2. No exception, and does the edit (obviously I changed LineNum to 3 for my test). Is it possible there is an invisible character (like '\r') after the ...205 ? Commented Mar 10, 2021 at 16:36
  • 2
    See also: Why is using a shell loop to process text considered bad practice? Commented Mar 10, 2021 at 16:55
  • 2
    Please add clarifications into your question (Edit link under it), not in comments. Commented Mar 10, 2021 at 16:57
  • 2
    You still have the same issue that I commented on in comments and in my answer to your previous question. You get multiple line numbers back. You can't use this as a single number. Commented Mar 10, 2021 at 17:21
  • 1
    "actually i am taking the line number of a file using LineNum". Yes, that is exactly the problem. Why not echo the sed command that is failing? and then you will understand why it throws an exception. Commented Mar 11, 2021 at 17:20

2 Answers 2

0

Ignoring the fact that you are using line-oriented tools for editing data that is clearly not oriented in lines but as XML...

To insert the string <!-- in front of any line that contains the string Deep, use sed like so:

sed '/Deep/ s/^/<!--/' file.xml >newfile.xml

There is no need to first calculate the line numbers with grep or any other tools, as far as I can see.

Would you want to insert the <!-- string at the start of the line above whatever lines contain Deep, then use

sed -e '1 { h; d; }' -e '/Deep/ { x; s/^/<!--/; x; }' -e x -e '$ { p; x; }' file.xml >newfile.xml

Or, if the file will fit easily in memory, script the ed editor (this may actually be the most flexible approach):

printf '%s\n' 'g/Deep/-1 s/^/<!--/' 'w newfile.xml' 'q' | ed -s file.xml
answered Mar 10, 2021 at 21:38
2
  • This printf '%s\n' 'g/Deep/-1 s/^/<!--/' 'w newfile.xml' 'q' | ed -s file.xml works perfectly if i want to insert the <!-- string at the start of the line above whatever lines contain Deep. However i also need to add --> at the 36 the line after the pattern matching at the end of the tag so the output looks like </ReconSummary>-->. I am using the code printf '%s\n' 'g/Deep/+36 s/$/-->/' 'w newfile.xml' 'q' | ed -s file.xml. But it is not giving the desired output. Commented Mar 11, 2021 at 2:49
  • @DEEPMUKHERJEE You question only concerned adding <!--. Conisder updating your question if you have other requirements. Commented Mar 12, 2021 at 20:23
-2

does this help?

LineNum=$(grep -n "Deep" file.xml | cut -d: -f 1 | awk -F '[\t]' '{$NF = $NF -1;}1')

then run the sed line (or maybe also adjust that one):

sed $LineNum 's#^<Deep>#<!--<Deep>#g' > file.xml
answered Mar 10, 2021 at 17:27
2
  • sed "$LineNums#^<Deep>#<!--<Deep>#" file.xml -> this is not changing anything. The variable name is LineNum Commented Mar 10, 2021 at 17:33
  • Note that you use an unset variable called LineNums in your sed call. Also, it's unclear what you have changed in the code that that the user posted, apart from using $(...) in place of a backticked command substitution. When you correct the unset variable's name, this still has the same issue as the original code in that it will fail if there are multiple matches in the XML document. Commented Mar 10, 2021 at 18:53

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.