-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?

  • 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` ? – Paul_Pedant Mar 10 '21 at 16:36
  • Might be there is an invisible character. So how can i exclude the invisible character here using the script? – DEEP MUKHERJEE Mar 10 '21 at 16:41
  • actually i am taking the line number of a file using LineNum variable with command grep -n command in a while loop. and after then i am using the sed command for replacing the tag in the line number – DEEP MUKHERJEE Mar 10 '21 at 16:45
  • Try running the script with `bash -x` and run `LC_ALL=C sed -n l < the-script` to spot invisible characters in it. – Stéphane Chazelas Mar 10 '21 at 16:53
  • 2
    See also: [Why is using a shell loop to process text considered bad practice?](//unix.stackexchange.com/q/169716) – Stéphane Chazelas Mar 10 '21 at 16:55
  • 2
    Please add clarifications into your question ([Edit](/posts/638602/edit) link under it), not in comments. – Stéphane Chazelas Mar 10 '21 at 16:57
  • I tried and there are no invisible characters. the problem is why i am still getting sed -e expression #1, char 7: unknown command `'. When i am giving the line number without a variable it is working fine. But there are multiple line number where i need to change. So for that i am using while loop to pickup multiple lines and assigning that LineNum varible inside sed command and then exception is happening. – DEEP MUKHERJEE Mar 10 '21 at 17:05
  • So, instead of describing, in comments, the code that you're _actually_ running, consider including that code in your question in place of the code that is working, even for you. – Kusalananda Mar 10 '21 at 17:12
  • I have modified my code – DEEP MUKHERJEE Mar 10 '21 at 17:17
  • 2
    You still have the same issue that I commented on in comments and in my answer to [your previous question](https://unix.stackexchange.com/questions/638546/getting-syntax-error-exception-for-expr). You get _multiple_ line numbers back. You can't use this as a single number. – Kusalananda Mar 10 '21 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. – Paul_Pedant Mar 11 '21 at 17:20

2 Answers2

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
Kusalananda
  • 320,670
  • 36
  • 633
  • 936
  • This printf '%s\n' 'g/Deep/-1 s/^/ at the 36 the line after the pattern matching at the end of the tag so the output looks like -->. 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. – DEEP MUKHERJEE Mar 11 '21 at 02:49
  • @DEEPMUKHERJEE You question only concerned adding ` – Kusalananda Mar 12 '21 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
hunter3740
  • 11
  • 3
  • sed "$LineNums#^# – DEEP MUKHERJEE Mar 10 '21 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. – Kusalananda Mar 10 '21 at 18:53