1

I have multiple lines in a file names.txt, let's say

my name is Jim your name please
what is good Harry potter
how is he Jhony and me

and so on , more than 500 lines.

I use awk command to search/ print only names awk "{ print $4; }" names.txt all names are on 4th position on each line

following command outputs

Jim
Harry
Jhony

and so on, more than 500 names.

I use sed command to print lines before each line which matches pattern. list.txt already have many lines. let's say

hero is always good and have alpha name
jungle is also called forest and beta owner
carrots are sweet have gama solution

sed command search word have and add line before pattern match line

sed -i "/have/ s/^/oslo good owner="Jim" and too good\n/" list.txt

and its works, but write all line with same name.

I want to use awk command result in sed, e.g require output in list.txt file should be

olso good owner="Jim" and too good
hero is always good and have alpha name
jungle is also called forest and beta owner
olso good owner="Harry" and too good
carrots are sweet have gama solution

and so on, till list.txt file ends.

working in windows-10 environment command prompt

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

0

You must log in to answer this question.

Browse other questions tagged .