I need to insert varying lines (i.e. these lines are an output of another script) between lines starting with certain fields.
An example to make it more clear.
This is the file where I wanna insert lines:
(save it as "input.txt")
So, basically what I try to do is: I search for the lines starting with "texta" and check if the following line starts either with "textb" or - if there has been a line previously inserted (which can happen) - if this starts with "sec_buttonpress". If I found these lines then the "textins" should inserted.
Actually, an extension to cover all needs would be to check also if the second field in the file "input.txt" contains "1". If so then the line (only fields 2 and 3) to be inserted after should actually replace the fields 2 and 3 of this line.
For some reason, "dothis.awk" is not working: it inserts at the end of teh "input.txt" file always the same 2 fields: containing a 6digit number and a "0".
1.- Which file is untitled3.txt?
2.- Try comment a little bit your code to know what you want to do.
3.- Try one awk program instead using several 'sed' & 'awk' commands.
4.- Input file of 'dothis.awk' is 'input.txt' while in the 'awk' program you
search 'texta' and 'textb' fields which are from 'insert.txt' file. Is this
correct?
5.- Paste the output.txt you are looking for.
thanks a lot for your comments! Sorry, if it's not really clear what I meant. I'll try to improve that:
Sorry, my mistake: it should be called "insert.txt". I changed that in my first post.
Here the code commented:
#!/bin/bash
### ok, this is only for creating the example files and try to make it look like the original.
### My original code is much longer since I process also some other parts of the original input.
declare -a Arraytexta
declare -a Arraytextb
declare -a Arraytextins
nrLines=`awk 'END {print NR}' insert.txt`
nrLines=`expr ${nrLines} - 2`
for ((i=1; i<=$nrLines; i++));
do
countb=`expr ${i} + 1`
countins=`expr ${i} + 2`
### Following, I create the arrays that contain the informations that are needed for the "gawk" command:
### First, the pattern from the first line of the match.
Arraytexta=`sed -n "${i} p" insert.txt | awk 'BEGIN{FS=":"}{print $2}'`
### Second, the pattern with which the following line should start.
Arraytextb=`sed -n "${countb} p" insert.txt | awk 'BEGIN{FS=":"}{print $2}'`
### Third, the text that should be inserted.
Arraytextins=`sed -n "${countins} p" insert.txt | awk 'BEGIN{FS=":"}{print $2}'`
### Here, I'll try to pass on the informations from the arrays above into the "dothis.awk" file.
gawk -v textins="${Arraytextins}" -v texta="${Arraytexta}" -v textb="${Arraytextb}" -f dothis.awk input.txt > output.txt
done
Sorry, but I am a beginner with awk. So some things seem for me at this point easier or more familiar with sed. :o
The example you posted are very long and the lines does not correspond between input file, replacement file , and output file (it looks as if they were screwed).
I mean i don't see the logic that should be followed.
I think you should post a shorter an cleaner example of infile, insert file and output expected so we could
1) quickly understand the logic of formatting
2) provide you a more accurate answer
sorry, I thought the longer examples help to better understand the structure, because the input files are a mess, but they are given to me like that.
I shortened the examples in my former posts. I hope now it becomes more clear what I wanna do.
thanks a lot for your effort! The code seems quite complicated. Actually, I do not understand what is done. Can you explain it a little bit so that I learn something?
And one question: the reason why I explicitly marked not only "texta" but the following line as "textb" is that "texta" and also "textb" can be more than once in the original input file. But they will most likely not appear again in consecutive lines.
As far as I understand you code, it looks only for "texta" and ignores "textb". Is that correct?
thanks a lot for your effort!
But at which point can I insert the condition that the next line has to start with textb? Sorry for that question, but your code seems so complex to me that I don't know how to adapt it.
Does it work with something like:
Disable the "EXPN" and "VRFY" commands in your current version of the "sendmail" command. A
malicious user able to connect to a machine running sendmail may be able to acquire information
about user accounts on that system
I just tried your code on the entire input file: it doesn't work. For some reason it only works if I use the short example lines that you used. As soon as I add some more lines in the input and the insert files the the output file equals the input file. Nothing is replaced or added. How is that possible?