简体   繁体   中英

Bash insert multiple lines into a text file

I have a list of parameters:

dwm.normfgcolor:
dwm.normbgcolor:
dwm.normbordercolor:
dwm.selfgcolor:
dwm.selbgcolor:
dwm.selbordercolor:

and a list of hex values:

#e6e4e3
#352231
#a19f9e
#e6e4e3
#FEE798
#e6e4e3

What i want to do is 'merge' them in a way that the first line in the first string corresponds to the first line in the second string and so forth. It should look like that

dwm.normfgcolor: #e6e4e3
dwm.normbgcolor: #352231

I thought of using

cat > output.txt<< EOF
dwm.normfgcolor: $variables?
dwm.normbgcolor:
dwm.normbordercolor:
dwm.selfgcolor:
dwm.selbgcolor:
dwm.selbordercolor:
EOF

with variables but I have no idea how.

答案 - 使用 paste -d 和进程替换。

Using paste

paste -d " " file1 file2 
paste -d " " <(command to get input1) <(command to get input2)
paste -d " " file1 <(command to get output2)

Using awk

awk 'NR==FNR{a[FNR]=$0;next}{print a[FNR],$0}' file1 file2
awk 'NR==FNR{a[FNR]=$0;next}{print a[FNR],$0}' <(command1) <(command2)
awk 'NR==FNR{a[FNR]=$0;next}{print a[FNR],$0}' file1 <(command2)

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