简体   繁体   中英

To replace one word in first line with another in 2nd line in linux with sed or awk

I have a log with the following texts. I need to replace GX81CBCCONVCH10 with the string GX4APDOCUGRLK10 / GX4DODOCGRPGT30 /.. in next line. And then delete the next two lines. Can anyone help with any sed or awk command to do that. Or any python scripts.

I was replacing in vim with the following command but it does not work in sed. :s/\(^.*\)GX81CBCCONVCH10\(.*\n\)\(.*EXT;\)\(.*;.*;.*;\)\(.*\)\(;.*;.*\n.*RTC.*\)/\1\5\2/g

Log:

20220812 090258078;1;2727;SVC;GX81CBCCONVCH10;;10.115.48.238;1.015;0.023;0.996;0.014;4096;820;1;0.995;3;0.009
20220812 090258078;1;2727;EXT;SwitchOAIntf;0.995;0.000;GX4APDOCUGRLK10;P;RIDD08B825RQ11DC
20220812 090258078;1;2727;RTC;2;0;0;0;0;2;0;0;1;0;0.00060;0.00000

20220812 090303106;1;2727;SVC;GX81CBCCONVCH10;;10.115.48.238;1.119;0.028;1.106;0.018;4096;1084;1;1.105;3;0.003
20220812 090303106;1;2727;EXT;SwitchOAIntf;1.105;0.000;GX4DODOCGRPGT30;P;RIDO2PCD35RQ11DC
20220812 090303106;1;2727;RTC;2;0;0;0;0;2;0;0;1;0;0.00065;0.00000

20220812 090308239;1;2727;SVC;GX81CBCCONVCH10;;10.115.48.238;0.896;0.031;0.883;0.021;4096;1084;1;0.881;3;0.003
20220812 090308239;1;2727;EXT;SwitchOAIntf;0.881;0.000;GX4DODOCGRPGT50;P;RIDWNKFI35RQ11DC
20220812 090308239;1;2727;RTC;2;0;0;0;0;2;0;0;1;0;0.00060;0.00000

20220812 090309137;1;2727;SVC;GX81CBCCONVCH10;;10.115.48.238;0.878;0.022;0.868;0.015;4096;820;1;0.867;3;0.003
20220812 090309137;1;2727;EXT;SwitchOAIntf;0.867;0.000;GX4APDOCUGRLK10;P;RIDZACDJ35RQ11DC
20220812 090309137;1;2727;RTC;2;0;0;0;0;2;0;0;1;0;0.00060;0.00000

The output should be like:

20220812 090258078;1;2727;SVC;GX4APDOCUGRLK10;;10.115.48.238;1.015;0.023;0.996;0.014;4096;820;1;0.995;3;0.009

20220812 090303106;1;2727;SVC;GX4DODOCGRPGT30;;10.115.48.238;1.119;0.028;1.106;0.018;4096;1084;1;1.105;3;0.003

20220812 090308239;1;2727;SVC;GX4DODOCGRPGT50;;10.115.48.238;0.896;0.031;0.883;0.021;4096;1084;1;0.881;3;0.003

20220812 090309137;1;2727;SVC;GX4APDOCUGRLK10;;10.115.48.238;0.878;0.022;0.868;0.015;4096;820;1;0.867;3;0.003

Using any awk:

$ awk 'BEGIN{FS=OFS=";"} (NR%4)==2{rep=$8; $0=prev; $5=rep; print} {prev=$0}' file
20220812 090258078;1;2727;SVC;GX4APDOCUGRLK10;;10.115.48.238;1.015;0.023;0.996;0.014;4096;820;1;0.995;3;0.009
20220812 090303106;1;2727;SVC;GX4DODOCGRPGT30;;10.115.48.238;1.119;0.028;1.106;0.018;4096;1084;1;1.105;3;0.003
20220812 090308239;1;2727;SVC;GX4DODOCGRPGT50;;10.115.48.238;0.896;0.031;0.883;0.021;4096;1084;1;0.881;3;0.003
20220812 090309137;1;2727;SVC;GX4APDOCUGRLK10;;10.115.48.238;0.878;0.022;0.868;0.015;4096;820;1;0.867;3;0.003

or if you really do want the output lines separated by blank lines then either this which will also produce a blank line at the end of the output:

$ awk 'BEGIN{FS=OFS=";"; ORS="\n\n"} (NR%4)==2{rep=$8; $0=prev; $5=rep; print} {prev=$0}' file
20220812 090258078;1;2727;SVC;GX4APDOCUGRLK10;;10.115.48.238;1.015;0.023;0.996;0.014;4096;820;1;0.995;3;0.009

20220812 090303106;1;2727;SVC;GX4DODOCGRPGT30;;10.115.48.238;1.119;0.028;1.106;0.018;4096;1084;1;1.105;3;0.003

20220812 090308239;1;2727;SVC;GX4DODOCGRPGT50;;10.115.48.238;0.896;0.031;0.883;0.021;4096;1084;1;0.881;3;0.003

20220812 090309137;1;2727;SVC;GX4APDOCUGRLK10;;10.115.48.238;0.878;0.022;0.868;0.015;4096;820;1;0.867;3;0.003

or this which won't:

$ awk 'BEGIN{FS=OFS=";"} (NR%4)==2{rep=$8; $0=prev; $5=rep; print sep $0; sep=ORS} {prev=$0}' file
20220812 090258078;1;2727;SVC;GX4APDOCUGRLK10;;10.115.48.238;1.015;0.023;0.996;0.014;4096;820;1;0.995;3;0.009

20220812 090303106;1;2727;SVC;GX4DODOCGRPGT30;;10.115.48.238;1.119;0.028;1.106;0.018;4096;1084;1;1.105;3;0.003

20220812 090308239;1;2727;SVC;GX4DODOCGRPGT50;;10.115.48.238;0.896;0.031;0.883;0.021;4096;1084;1;0.881;3;0.003

20220812 090309137;1;2727;SVC;GX4APDOCUGRLK10;;10.115.48.238;0.878;0.022;0.868;0.015;4096;820;1;0.867;3;0.003

This might work for you (GNU sed):

sed -E '/(GX81CBCCONVCH)10/{s//\130/;N;N;P;d}' file

Match GX81CBCCONVCH10 and replace 10 with 30 , append the next two lines and print the first only.

Edit: Misunderstood the question, perhaps:

sed -E '/SVC/{N;N;s/GX81CBCCONVCH10(.*)\n.*EXT(;[^;]*){3};([^;]*).*\n.*RTC.*/\3\1/}' file

Using GNU sed

$ sed -Ez 's/([^G]*)GX81CBCCONVCH10([^\n]*)\n[^G]*(GX[^;]*)[^\n]*\n[^\n]*/\1\3\2/g' input_file
20220812 090258078;1;2727;SVC;GX4APDOCUGRLK10;;10.115.48.238;1.015;0.023;0.996;0.014;4096;820;1;0.995;3;0.009

20220812 090303106;1;2727;SVC;GX4DODOCGRPGT30;;10.115.48.238;1.119;0.028;1.106;0.018;4096;1084;1;1.105;3;0.003

20220812 090308239;1;2727;SVC;GX4DODOCGRPGT50;;10.115.48.238;0.896;0.031;0.883;0.021;4096;1084;1;0.881;3;0.003

20220812 090309137;1;2727;SVC;GX4APDOCUGRLK10;;10.115.48.238;0.878;0.022;0.868;0.015;4096;820;1;0.867;3;0.003

You may try this sed command (that should work in any sed ):

sed '/^$/d; N; s/\(\([^;]*;\)\{4\}\)[^;]*\(;.*\)\n\([^;]*;\)\{7\}\([^;]*\).*/\1\5\3/p; N; d' file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM