inserting and replacing lines with awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting inserting and replacing lines with awk
# 8  
Old 06-12-2011
Yes it is correct : my code currently ignore textb.
I am not sure to understand your needs so i just gave an attempt based on the given example.
Feel free to adapt it.

---------- Post updated at 12:41 AM ---------- Previous update was at 12:11 AM ----------

BEGIN{i=1}initialize the index that will later be used to scan file1
NR==FNR{p[NR]=$1;a[NR]=$0;next}from the first file, build 2 "line number indexed" array one holding $1 and one holding the entire line
/^texta/{x=$2}while scanning the second file, if texta is found, keep $2 value in variable x
x&&/^textins/{sub(".*"$3,$3);y=$0}!y{next}if x is set find the next textins line and erase everything before $3 and save the line in y variable
{do {still while scanning the second file, we start scanning the arrays holding file1 that have previously been build
if(p[i]!=x){while $1 from file 1 is different from the currently retainded scanned value of file2
print a[i]just print the line of file1
}else{ otherwise , substitution (as per what i have understood of your requirments)
split(a[i],b)the initial line is split in an array b so field can be extracted easier
print p[i]" \t "y"\t"b[4]" \t "b[5]print the desired field inserting those extracted from file2
x=y=z;delete b;i++;nextreset x and y to empty, delete array b increment i to start next scan of file1 at the next line than the one that have been processed, and reprocess from beginning to populate x and y again with next value from file 2
} delete a[i];i++delete the printed line from the array an prepare to scan next line
}while(length(a[i])) Loop for scanning file1 while line not empty
}END{do{print a[i];i++}while(length(a[i]))} since the scan of file 1 only start when x and y have been populated from file2, when the last substitiution has been made (when scanning file2 is finished) it requires an extra loop to finish the display the rest of file1

Last edited by ctsgnb; 06-12-2011 at 08:19 PM..
# 9  
Old 06-13-2011
Hi ctsgnb,

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:
Code:
BEGIN{i=1;flag=0}
NR==FNR{p[NR]=$1;a[NR]=$0;next}
/^texta/{flag=1}
if(/^textb/&&flag==1)
/^texta/{x=$2}
x&&/^textins/{sub(".*"$3,$3);y=$0}!y{next}
{do {
        if(p[i]!=x){
                print a[i]
        }else{
                split(a[i],b)
                print p[i]" \t "y"\t"b[4]" \t "b[5]
                x=y=z;delete b;i++;next
        }
        delete a[i];i++
}while(length(a[i]))
}END{do{print a[i];i++;flag=0}while(length(a[i]))}

Thanks a lot for you patience!

tempestas

Last edited by tempestas; 06-13-2011 at 06:29 AM..
# 10  
Old 06-13-2011
i have to do the below tasl using script.

please help

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
# 11  
Old 06-13-2011
Hi ctsgnb,

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?

Thanks!

tempestas
# 12  
Old 06-13-2011
Could you please :

1) post a short example (initial input, replacement file, and expected output) that demonstrate when and how should textb considered.

2) upload your big files, input and replacement files.

Thx
# 13  
Old 06-15-2011
give a try to this :

Code:
# cat ext.awk
BEGIN{
FS="\t"
}
$3~/Response/{
if (!v&&!r) { if ($4==1) {r++;R[0]=$5} }
else {if ($4==2||$4==3) {K[(v":"r)]=$4;R[(v":"r)]=$5;r++}}
;next}
$3~/Video/{S[++s]=$1;r=1;v++;V[v]=$4;St[v]=$5;next}
$1~/Document/{ Et[++e]=$7-R[0];next}
END {
k=1;do{
t=1;do{x=k":"t
y=(y?y OFS:z) (K[x]?K[x]:0) OFS (R[x]?R[x]-St[k]:0)
} while ((k":"(++t)) in K)
print S[k],V[k],St[k]-R[0],Et[k],y;y=z} while (k++<v)
}

and then run

awk -f ext.awk OFS="\t\t" OrigFile.log

or chose any OFS (Output Field Separator) that fit with your expectations instead of the double tabulation proposed aboved.

Last edited by ctsgnb; 06-17-2011 at 10:27 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Merging multiple lines to columns with awk, while inserting commas for missing lines

Hello all, I have a large csv file where there are four types of rows I need to merge into one row per person, where there is a column for each possible code / type of row, even if that code/row isn't there for that person. In the csv, a person may be listed from one to four times... (9 Replies)
Discussion started by: RalphNY
9 Replies

2. Shell Programming and Scripting

Replacing lines matching a multi-line pattern (sed/perl/awk)

Dear Unix Forums, I am hoping you can help me with a pattern matching problem. What am I trying to do? I want to replace multiple lines of a text file (that match a multi-line pattern) with a single line of text. These patterns can span several lines and do not always have the same number of... (10 Replies)
Discussion started by: thefang
10 Replies

3. Shell Programming and Scripting

Summing over specific lines and replacing the lines with the sum using sed, awk

Hi friends, This is sed & awk type question. I have a text file which has numbers spread all over the file. I want to sum the series of numbers whenever i find it and produce an output file with the sum. For example ###start of input text file #### abc def ghi 1 2 3 4 kjld random... (3 Replies)
Discussion started by: kaaliakahn
3 Replies

4. UNIX for Dummies Questions & Answers

Finding lines with a regular expression, replacing them with blank lines

So the tag for this forum says all newbies welcome... All I want to do is go through my file and find lines which contain a given string of characters then replace these with a blank line. I really tried to find a simple command to do this but failed. Here's what I did come up with though: ... (2 Replies)
Discussion started by: Golpette
2 Replies

5. Shell Programming and Scripting

Help in replacing two blank lines with two lines of diff data

Hi.. I'm facing a trouble in replacing two blank lines in a file using shell script... I used sed to search a line and insert two blank lines after the searchd line using the following sed command. sed "/data/{G;G;}/" filename . In the file, after data tag, two lines got inserted blank lines..... (4 Replies)
Discussion started by: arjun_arippa
4 Replies

6. Shell Programming and Scripting

inserting multiple lines with awk

awk '/<login-module code="com.nlayers.seneca.security.LdapLogin" flag="sufficient">/{p++} /<login-module code="com.nlayers.seneca.security.LdapLogin" flag="sufficient">/ && p==1 {$0="Mulitple lines here\n"$0}1' login-config.xml In the above awk code inside shell script, i am having problems when... (1 Reply)
Discussion started by: sunrexstar
1 Replies

7. Shell Programming and Scripting

Replacing lines between two files with awk

Hello Masters, I have two subtitles file with different language like below First file : 1 00:00:41,136 --> 00:00:43,900 2 00:00:55,383 --> 00:00:58,477 <i> Ladies and gentlemen,</i> <i>this is Simon Barsinister,</i> 3 00:00:58,553 --> 00:01:00,521 <i>the wickedest man in the... (8 Replies)
Discussion started by: rk4k
8 Replies

8. Shell Programming and Scripting

inserting lines

Hi I would like to add lines to a file at specific locations. eg: If I have a file containing data... ABC DEF GHIJKLKMNOP RSTUVWXYZ and I have a requirement to insert a lines a) "LINE_FIRST" before first line in the file b) "LINE_X" before third line in the file c) "LINE_LAST"... (2 Replies)
Discussion started by: tostay2003
2 Replies

9. Shell Programming and Scripting

inserting a lines in a file

Hi folks, i need to insert the same set of lines between each line input lines 111111 aaaaaa 333333 output should be 111111 1 2 3 aaaaaa 1 2 3 333333 1 (2 Replies)
Discussion started by: Balaji Sukumara
2 Replies

10. UNIX for Dummies Questions & Answers

Inserting specific lines in vi

How can I insert, say lines 500 - 700 from another file into the current file on the current line (cursor) that I am editing while in vi (AIX). I know how to insert the entire file but how do you do it when you only need certain lines from a huge file? I've referenced my Unix Unleash book but... (2 Replies)
Discussion started by: giannicello
2 Replies
Login or Register to Ask a Question