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
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.awk
fails. If yoursed
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.sed for beginners
orawk for beginners
. Even for specific things, like adding a line before a pattern withsed
- 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.awk
, then use that variable instead of "Jim" in yoursed
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