user4993731
user4993731

Reputation:

How to remove new line from the string in awk?

data test.txt

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

Script

#!/bin/bash
a=test.txt

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

Output

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

It automatically break into new line.

Output should be like this

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

Upvotes: 2

Views: 3123

Answers (1)

anubhava
anubhava

Reputation: 784998

You need to use getline instead of system to capture command's output in a variable:

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

Upvotes: 5

Related Questions