用awk将“替换为”

时间:2019-12-03 13:29:56

标签: awk

我有一个文件调用Input dirname() basename() /folder/subfolder/file.ext /folder/subfolder/ file.ext /folder/subfolder /folder/ subfolder /file.ext file.ext / file.ext file.ext null ,并且我想在var中以xxxx.txt开头的行,但要用“替换为'。

Sentencia

我正在使用awk,但我做不到。

$ cat xxxx.txt
>Ejecutor: Ezequiel
>Jira: 13226
>Origen (PROD/CUA/ACE/PROD6/DEV): PROD
>Destino (PROD/CUA/ACE/PROD6/DEV): CUA
>Sentencia: select * from pricing_user.prc_promotions where fec_baja is NULL and end_date 
between NOW() and adddate(NOW(), 30) and werks in ("6027", "0006", "0055", "6785")
>Tabla Destino: pricing_user.prc_promotions

3 个答案:

答案 0 :(得分:3)

防弹版本为:

awk 'match($0,/^Sentencia: */){gsub("\042","\047"/); print substr($0,RLENGTH+1)}'

我们使用octal notation,因为Posix支持POSIX standard(请参见doc的正则表达式)。

我避免使用FS=":",因为在相应的行中可能会有额外的:

答案 1 :(得分:0)

awk -F": " '{if ($1 == "Sentencia") {gsub("\042","\047",$2);print}}' $wkdir/xxxx.txt

我将双引号替换为\042,将单引号替换为\047(等效于八进制)

$ awk -F": " '{if ($1 == "Sentencia") {gsub("\042","\047",$2);print}}' $wkdir/MigrarDatosTablas.txt
Sentencia select * from pricing_user.prc_promotions where fec_baja is NULL and end_date between NOW() and adddate(NOW(), 30) and werks in ('6027', '0006', '0055', '6785')

答案 2 :(得分:0)

除了其他答案外,您还可以将程序编写到awk文件中,并且在不干扰外壳程序的情况下,按原样使用引号:

$ cat program.awk
BEGIN {
    FS=":"               # set the delimiter
}
/^>Sentencia:/ {         # match the beginning
    gsub(/"/,"'",$0)     # <- LOOK HERE
    print $2             
}
$ awk -f program.awk file
 select * from pricing_user.prc_promotions where fec_baja is NULL and end_date between NOW() and adddate(NOW(), 30) and werks in ('6027', '0006', '0055', '6785')

正如其他人已经指出的那样,我将忽略代码中的其他一些问题。