file - 复制文本文件中的所有文本并将其插入每行的开头 sed
问题描述
我想复制一个文本文件中的所有文本,并在另一个文本文件中的每一行的开头添加。
我试过 sed,我可以在每行的开头放一个字符,但我不知道如何从另一个文件复制文本
我试过这个:
sed 's/^/#/' 2.txt > 3.txt
但这只会放置一个字符或字符串。
例子:
我在 2.txt 中有这样的话:
u
ubbia
ubbidiente
ubbidienza
ubbidire
ubertoso
在第二个 3.txt 我有相同的行但有解释:
u = explanation here
ubbia = explanation here
ubbidiente = explanation here
ubbidienza = explanation here
ubbidire = explanation here
ubertoso = explanation here
我想要这个结果:
u u = explanation here
ubbia ubbia = explanation here
ubbidiente ubbidiente = explanation here
ubbidienza ubbidienza = explanation here
ubbidire ubbidire = explanation here
ubertoso ubertoso = explanation here
解决方案
您能否尝试以下操作,此解决方案将根据第一列的最大长度的长度在输出中进行缩进(已为其编写了逻辑),并使用给定的样本进行了测试。
awk '
FNR==NR{
a[FNR]=$1
c[$1]
next
}
($1 in c){
b[++count]=$0
len=len>length($1)?len:length($1)
}
END{
for(i=1;i<=count;i++){
val=len<length(b[i])?len+(len-length(a[i])):len
printf("%s%"val"s%s\n",a[i],OFS,b[i])
}
}
' 2.txt 3.txt
输出如下。
u u = explanation here
ubbia ubbia = explanation here
ubbidiente ubbidiente = explanation here
ubbidienza ubbidienza = explanation here
ubbidire ubbidire = explanation here
ubertoso ubertoso = explanation here
推荐阅读
- java - Android Studio - 导航活动中的 2 个工具栏
- c - SIGSEGV 访问 C 中结构的参数
- javascript - html自定义元素使主机css改为应用于shadow dom子
- shell - 接收 URL 作为 shell 脚本的参数时,如何在不使用引号的情况下绕过与号 (&)?
- django - 聊天应用程序中的 Django Filters with Rest Framework Authentication 消息过滤策略/问题
- php - php解析对象层次结构和数组的JSON混合以读取值
- python - Mac在尝试运行代码时不断崩溃并重新启动
- java - 除了管理用户会话,Java SE/EE 中的 CDI 是什么?
- python - 为什么 imaplib.fetch RFC822 响应格式如此奇怪
- batch-file - 如何在 Windows 中通过命令行调用“声音设置”