如何将文件名附加到每个 [英] How to append the filename to every
本文介绍了如何将文件名附加到每个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
文件中有32个单元格,因此我需要读取文件名并将文件名附加在每一行的开头
I have 32 cells in a file so i need to read the name of the file and append the file name at the starting of every line how can i do it
示例
MACRO XYZ
PIN AB
DIRECTION INPUT ;
PIN BC
DIRECTION INPUT ;
MACRO GEN
PIN DECSEL_STG2[0]
DIRECTION INPUT ;
PIN DECSEL_STG1[0]
DIRECTION INPUT ;
我需要什么作为输出
MACRO XYZ
XYZ PIN AB
XYZ DIRECTION INPUT ;
XYZ PIN BC
XYZ DIRECTION INPUT ;
MACRO GEN
GEN PIN DECSEL_STG2[0]
GEN DIRECTION INPUT ;
GEN PIN DECSEL_STG1[0]
GEN DIRECTION INPUT ;
推荐答案
这可以通过相对简单的awk
脚本来完成,如下所示:
This can be done with a relatively simple awk
script, as per the following transcript:
pax$ printf "MACRO XYZ\nline 2\nline3\n\nMACRO GEN\nline 6\n\n\n" | awk '
...$ $1=="MACRO"{mac=$2;print;next}$NF>0{print mac" "$0;next}{print}'
MACRO XYZ
XYZ line 2
XYZ line3
.
MACRO GEN
GEN line 6
.
.
(我添加了那些.
个字符只是为了显示空白行).
(those .
characters are added by me just to show where the blank lines are).
扩展脚本:
$1 == "MACRO" { # If first word is macro:
mac=$2; # store second word,
print; # print line,
next # and go get next line.
}
$NF > 0 { # Otherwise, if line has some fields:
print mac" "$0; # print stored macro name before line,
next # and go get next line.
}
{ # Otherwise (lines with no fields):
print # just print it as is.
}
这篇关于如何将文件名附加到每个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文