简体   繁体   English

awk将输出打印为变量/数组,未保持格式

[英]awk print output as variable/array not staying formatted

My aim is to take the output from an awk print command (while keeping the formatting) and use that to replace certain text in a file. 我的目标是从awk打印命令中获取输出(同时保持格式),并使用该命令替换文件中的某些文本。
Currently im using: 目前即时通讯正在使用:

declare -a mach=($(awk -F',' 'NR>1{print NR-1, $8}' mag_grad.csv)) 

which gives: 这使:

1 0.77492
2 0.18022
3 0.29596
4 0.5229

etc... 等等...

I'm trying to use sed to find and replace text in a file with the array $mach, with the same formatting of 2 columns. 我正在尝试使用sed查找和替换数组$ mach的文件中的文本,其格式为2列。

sed -i s/"mach data"/"${mach[*]}"/g nodedata.txt

However the result of this is to replace mach data with 但是,这样做的结果是用

1 0.77492 2 0.18022 3 0.29596 4 0.5229 .......

Any ideas on how I could preserve the formatting? 关于如何保存格式的任何想法?

Cheers 干杯

try: 尝试:

mach=$(awk -F',' 'NR>1{print NR-1, $8}' mag_grad.csv)
sed -i "s/\x22mach data\x22/${mach//$'\n'/\\n}/" nodedata.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM