简体   繁体   中英

Using Grep Sed or Awk to search and replace lines in EDL file

Essentially, I'm trying to use sed, grep or awk to go from this:

001  AX       V     C        00:00:32:09 00:00:40:19 00:00:00:00 00:00:08:10
* FROM CLIP NAME: 001_M04-1008rev.mov

002  AX       V     C        00:00:20:04 00:00:30:11 00:00:08:10 00:00:18:17
* FROM CLIP NAME: 001_M04-1011rev.mov

003  AX       V     C        00:00:15:00 00:00:26:05 00:00:18:17 00:00:29:22
* FROM CLIP NAME: 001_M04-1012rev.mov

004  AX       V     C        00:00:20:01 00:00:30:13 00:00:29:22 00:00:40:10
* FROM CLIP NAME: 001_M04-1027rev.mov

005  AX       V     C        00:00:24:22 00:00:30:09 00:00:40:10 00:00:45:21
* FROM CLIP NAME: 001_M04-1028rev.mov

006  AX       V     C        00:00:38:04 00:00:49:03 00:00:45:21 00:00:56:20
* FROM CLIP NAME: 001_M04-1029rev.mov

To this:

001  1008rev       V     C        00:00:32:09 00:00:40:19 00:00:00:00 00:00:08:10
* FROM CLIP NAME: 001_M04-1008rev.mov

002  1011rev       V     C        00:00:20:04 00:00:30:11 00:00:08:10 00:00:18:17
* FROM CLIP NAME: 001_M04-1011rev.mov

003  1012rev       V     C        00:00:15:00 00:00:26:05 00:00:18:17 00:00:29:22
* FROM CLIP NAME: 001_M04-1012rev.mov

004  1027rev       V     C        00:00:20:01 00:00:30:13 00:00:29:22 00:00:40:10
* FROM CLIP NAME: 001_M04-1027rev.mov

005  1028rev       V     C        00:00:24:22 00:00:30:09 00:00:40:10 00:00:45:21
* FROM CLIP NAME: 001_M04-1028rev.mov

006  1029rev       V     C        00:00:38:04 00:00:49:03 00:00:45:21 00:00:56:20
* FROM CLIP NAME: 001_M04-1029rev.mov

Any suggestions? Basically I am taking the 7 last characters before ".mov" and move those characters in place of the "AX" that comes before it on the previous line.

Here's one way using awk :

awk '{ sub($2, substr($NF, length($NF) - 10, 7)) }1' RS= ORS="\n\n" file

Here's one way using perl :

perl -00 -pae 's/$F[1]/substr($F[-1], -11, 7)/e' file

Note that that AWK solution will give you a trailing newline, whereas the Perl solution will not. Whether or not that's even relevant is another thing.

Here's a potential awk solution:

awk 'BEGIN { buf=""; } /AX/ { buf=$0; } /.mov$/ { match($0, /\-([^\-\.]+).mov/, n); gsub("AX", n[1], buf); print buf,"\n",$0,"\n"; }' < test.txt

Which outputs the following:

001  1008rev       V     C        00:00:32:09 00:00:40:19 00:00:00:00 00:00:08:10
 * FROM CLIP NAME: 001_M04-1008rev.mov

002  1011rev       V     C        00:00:20:04 00:00:30:11 00:00:08:10 00:00:18:17
 * FROM CLIP NAME: 001_M04-1011rev.mov

003  1012rev       V     C        00:00:15:00 00:00:26:05 00:00:18:17 00:00:29:22
 * FROM CLIP NAME: 001_M04-1012rev.mov

004  1027rev       V     C        00:00:20:01 00:00:30:13 00:00:29:22 00:00:40:10
 * FROM CLIP NAME: 001_M04-1027rev.mov

005  1028rev       V     C        00:00:24:22 00:00:30:09 00:00:40:10 00:00:45:21
 * FROM CLIP NAME: 001_M04-1028rev.mov

006  1029rev       V     C        00:00:38:04 00:00:49:03 00:00:45:21 00:00:56:20
 * FROM CLIP NAME: 001_M04-1029rev.mov

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