0

I am trying to parse a file in AIX server. I could do the name in Ubuntu.

As the -i option did not work, i used a temperory file and renamed that to the original file. Please let me know where I am missing something.

I have to find a pattern in the file and add a line above or below the pattern.

sed '/ServiceNow (TAM):/i myline1' filename > temp_file && mv temp_file filename

Actual commands used:

sed '/ServiceNow (TAM)/i servicenow.test..ersngrouplist.0=Test' /itim/data/testfile > temp_properties && mv temp_properties /itim/data/testfile

Error: sed: Function /ServiceNow (TAM)/i servicenow.test..ersngrouplist.0=Test cannot be parsed.

Please help how i can change this command to get

My Expectation:

myline1
ServiceNow (TAM): //pattern
myline2

  • Sorry, but that is not enough information. We would need some *actual* example data, the *actual* commands you typed, and the *actual* error / output. Ideally as short and concise as possible ([mcve]). Posting pseudocode ("pattern" / "new_line" and saying "it failed" does not tell us anything, and we cannot help you that way. – DevSolar Dec 10 '20 at 08:11
  • Thanks for your suggestions. I have updated with actual commands. Will this helps now – Siva Subramanian Dec 10 '20 at 08:16
  • Does this answer your question? [Insert newline using sed](https://stackoverflow.com/questions/46082397/). – DevSolar Dec 10 '20 at 08:38
  • Just a thought... when working with the *"awkward"* Unix platforms (without wishing to shame anyone, but I mean HP-UX, AIX and Tru64) I generally found that **Perl** was far, far more consistent across them all and resorted time and again to **Perl** to get solutions that worked across the board. Tim Maher's excellent book *"Minimal Perl"* discusses all the variants of `awk`, `sed` and `grep` and shows how to overcome the differences with Perl. – Mark Setchell Dec 10 '20 at 09:25

1 Answers1

1

The problem is that POSIX versions of sed don't accept the i command with additional text on the same line. You need to put a \ after i and to put the text for insertion on the next line.

sed '/ServiceNow (TAM)/i\
servicenow.test..ersngrouplist.0=Test'

The error message from GNU sed is far more helpful than AIX's!

$ sed --posix '/ServiceNow (TAM)/i servicenow.test..ersngrouplist.0=Test'
sed: -e expression #1, char 21: expected \ after `a', `c' or `i'
Jon
  • 3,573
  • 2
  • 17
  • 24