awk/sed find then find again from that point

grep regex
awk examples
awk print characters after match
awk tutorial
awk regex
man sed
sed command
sed options

I have a file where I want to find and replace a very specific line. I have provided a simplified file below which I'll call test.in. If I were to do this manually I would find "log 3" then I would find "old string" from that point and replace it with "new string". In practice the files are large with lots of information between LOG number and old string, but these are the unique identifiers.

*LOG 1

old string
old string

*LOG 2

old string
old string
old string

*LOG 3

old string [Want to replace this with "new string"]


*LOG 4

old string
old string

I thought about using the following:

  grep -A 700 "*LOG 3" test.in | sed '0,/old string/{s/old string/new string/}'

But this doesn't work for me as I need to correct the entire file. I also came across the following on these pages:

awk '/old string/{count++;if(count==3){sub("old string","new string")}}1' test.in

However this also doesn't work as it assumes I know how many "old string" there are before I get to the one I need to replace - and in practice I don't as there are many different files.

I cannot simply find LOG 3 with a line space and old string and replace it that way, as there is a ton of different information between LOG 3 and old string.

Is there a simple way I can find something then search again from that new position, and then use sed/awk and save the entire file.

Many thanks :)

Simply 1 sed line
sed '/^\*LOG 3/,/old string/s/old string/new string/' test.in

Translated:

From *LOG 3 to next line containing *old string*, replace old string by new string

Return only the portion of a line after a matching pattern, Here, you could write stalled: again in the replacement part; this feature is useful Sometimes you'll want to remove the portion of the line after the match. of the expression, so not the entire line which is - of course - normally done by grep. I wanted to point out that grep doesn't recognize the \n as a newline, so your first� Awk and Sed are brilliant text processors, and as you learn more ways to use them the less you’re going to find yourself using a word processor. Word processors, in my sometimes-humble opinion, are great lumbering things all full of buttons and menus, and good luck finding what you want — or if it even exists.

With awk you could do the same as in the (sed solution from F. Hauri](https://stackoverflow.com/a/59582010/8344060)

awk '/^[*]LOG 3$/,/old_string/{sub(/old_string/,"new_string")}1' file

This is, however, not really flexible if you want to start doing other things with your awk. The flag method is, therefore, the easiest:

awk '/^[*]LOG 3$/{f=1};f{f=!(sub(/old_string/,"new_string"))}; 1' file

[PDF] grep, awk and sed – three VERY useful command-line utilities Matt , In the simplest terms, grep (global regular expression print) will search input files for all of the lines that do not match the search string, rather than printing the The next input record is read and processing starts over with the first pattern in example, you can specify an integer of given width, or a floating point number or � 20 awk examples. Many utility tools exist in the Linux operating system to search and generate a report from text data or file. The user can easily perform many types of searching, replacing and report generating tasks by using awk, grep and sed commands. awk is not just a command.

This is probably what you want:

$ awk '/^\*LOG/{f=($NF==3)} f{sub(/old string/,"new string")} 1' file
*LOG 1

old string
old string

*LOG 2

old string
old string
old string

*LOG 3

new string [Want to replace this with "new string"]


*LOG 4

old string
old string

Note that although the text you're searching for and replacing has the word "string" in it, the above and any sed solution or any other awk solution posted so far is NOT doing a literal string search/replace. Instead it's searching for a regexp and replacing it with a backreference-enabled string so it'll misbehave given various metacharacters. If you REALLY want to do a literal string search/replace then it's:

$ awk '
    BEGIN { old="old string"; new="new string" }
    /^\*LOG/ { f=($NF==3) }
    f && s=index($0,old) { $0=substr($0,1,s-1) new substr($0,s+length(old)) }
1' file
*LOG 1

old string
old string

*LOG 2

old string
old string
old string

*LOG 3

new string [Want to replace this with "new string"]


*LOG 4

old string
old string

Matching Patterns and Processing Information with awk, This chapter describes the awk command, a tool with the ability to match lines of text in awk reads the next input record and repeats the program with that record . Compare the operation of awk with the very similar operation of the sed editor, numeric (floating-point) values or string values depending on their use in the� Rather sed scans the input file, line by line, and applies a list of instructions (called a sed script) to each line in the input file. The script, which is usually a separate file, can be included in the sed command line if it is a one-line command. The sed utility has three useful options. Option –n suppresses the automatic output.

Not sure how it's done with sed, but with vim (ex/vi too?) you can simply paste regexps one after another:

vim '+/^\*LOG 3//^old string/s/.*/new string' '+wq' test.txt

Sed - An Introduction and Tutorial, This is the Grymoire's UNIX/Linux SED editor. Sed will match the first string, and make it as greedy as possible. I'll cover that later. If you The next line is read from the input file and places it in the pattern space. If the end of� The sed is simple and limited while awk is more complex and versatile. Conclusion. The difference between sed and awk is that sed is a command utility that works with streams of characters for searching, filtering and text processing while awk more powerful and robust than sed with sophisticated programming constructs such as if/else, while, do

Sed & Awk: Amazon.co.uk: Arnold Dougherty, Dale, Robbins , This edition covers features of sed and awk that are mandated by the POSIX standard. Next, the book describes how to write sed scripts. 109,624 in Books (See Top 100 in Books) use AWK I reach for this book, spend an hour trying to find something, try a few test programs which the book infers will hang together, and� Straight to the point, I'm wondering how to use grep/find/sed/awk to match a certain string (that ends with a number) and increment that number by 1. The closest I've come is to concatenate a 1 to the end (which works well enough) because the main point is to simply change the value.

UNIX Shell Programming : grep,sed and awk, A flowchart for the grep utility is given on the left and two points are to be 8. grep cannot select a line based on the contents of the previous or the next line. In the example below, we use grep to find all the lines that end in the word the and� Hey murphy, Thnks for the reply firstof all. Below is my script : where i am using your given code..and somehow its not giving desired o/p. can you please tell me what is wrong in this

KSH script BASICS, Trivial Calculations Numerical Calculations using "bc" "grep" "sed" "awk" "perl" The shell from which you are starting the script will find this line and and hand the One can continue commands over more than one line with a "\" immediately arguments so that one can call it with different values from different places in the � Awk command in Unix / Linux is a powerful command for processing text. Administrator & developer often need to look for the pattern in the files and then process that text as per there requirement. Awk command comes quite handy for these types of task. It is very helpful for processing table i.e (rows and column) type of data in the files.

Comments
  • Do you only want to act on the first old string that comes between *LOG 3 and *LOG 4 or all of them (assuming there can be more than one)?