shell - 如何从awk中的字符串中删除新行?

标签 shell awk

数据测试.txt

a,b,c
1,2,3
a,s,f

脚本

#!/bin/bash
a=test.txt

cat $a | awk -F , '{ print system("uuidgen"),$1,$3 }' > output.txt

输出

772c2c75-fbd6-42db-80eb-839a4817178a
a,c
c1293427-4ec1-4b28-ac47-3a22dc30037b
1,3
f8da702d-2411-4e9c-9c28-123793a9f44b
a,f

它会自动换行。

输出应该是这样的

772c2c75-fbd6-42db-80eb-839a4817178a,a,c
c1293427-4ec1-4b28-ac47-3a22dc30037b,1,3
f8da702d-2411-4e9c-9c28-123793a9f44b,a,f    

最佳答案

您需要使用 getline 而不是 system 来捕获变量中的命令输出:

awk 'BEGIN{FS=OFS=","} ("uuidgen" |& getline out) > 0 {
       print "\"" out "\"", $1, $3; close("uuidgen")}' test.txt
29F8159A-F3F0-41B4-AE8B-6B86562FE58B,a,c
29F8159A-F3F0-41B4-AE8B-6B86562FE58B,1,3
29F8159A-F3F0-41B4-AE8B-6B86562FE58B,a,f

关于shell - 如何从awk中的字符串中删除新行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31099674/

相关文章:

linux - 简单的 shell 。混合管道和输出

bash - 如何对文本文件中的一行数字求和——Bash Shell Scripting

linux - -bash : syntax error near unexpected token `done' in script

linux - BASH - csv 文件中列和行的条件总和

linux - 在 Linux 上将一个文件的内容插入到另一个文件

bash - 如何通过awk命令计算csv文件中的 "|"?

linux - 无法为用户将 key 添加到远程计算机

Linux shell脚本编程基础 : making a command-line tool that executes a command to execute several

c++ - 有没有 C++ 方法来调用 linux shell 命令?

awk - 使用 awk 打印文件而不添加尾随换行符