Skip to main content
The 2024 Developer Survey results are live! See the results

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

7
  • 1
    Googling sed add line before phrase returns for example fabianlee.org/2018/10/28/… among a lot others. Expressions like "not successful" and "command not working" aren't helpful. Instead consider "When I do X happens Y" with exact commands and results - thats a LOT easier to troubleshoot than "doesn't work". Also more descriptive question titles help understand what exactly you want to achieve. Commented Apr 2, 2023 at 19:13
  • Thanks I edited my question, and tried to follow your instructions as much as I can.
    – Rizwan.A
    Commented Apr 2, 2023 at 19:44
  • I'm not convinced that removing the information that something is failing actually helps. What I meant was instead of saying "awk doesn't work" explain exactly how awk fails. If your sed command contains only the name "Jim" you will get "Jim" every time. You need to figure out how to use a variable. I'd suggest checking GNU Bash Reference, TLDP Advanced Bash Scripting, GNU Sed Manual etc... a lot of resources are easily found. Commented Apr 2, 2023 at 19:57
  • 1
    I'm just pointing out that there are any number of tutorials easily found with simple internet searches, for different levels starting from 0 - sed for beginners or awk for beginners. Even for specific things, like adding a line before a pattern with sed - did you check that link? If you're able to understand Cisco manuals, any scripting tutorial is a piece of cake. Speaking from personal experience. You do need to put in some effort. Members of the community will help if you run into a wall, but nobody will write a script for you. Again speaking from personal experience. Commented Apr 2, 2023 at 20:56
  • 1
    As you're describing this, you don't need to. You need a loop that reads value to a variable from the text file you produced with awk, then use that variable instead of "Jim" in your sed line. Again, easily searchable, for example here's just the first result of many when googling how to read variable from file: tecmint.com/different-ways-to-read-file-in-bash-script Commented Apr 2, 2023 at 21:38