I created a script that logs if an email is sent if it satisfies a condition.
I get the output below using AWK to get specific columns.
[AUTORESTART] Mar 17 21:21:32 GMT 2022
I want to convert them to csv so I use below command
cat logchkmail_all.txt | awk '{print $4,$6,$7,$8,$9,$10}' | awk 'NR>1{$1=$1}1' OFS="," > sample.csv
...which works, but it returns
[AUTORESTART],Mar,17,21:21:32,GMT,2022
as it should. But what I want is to be able to separate the [AUTORESTART] and then combine columns 6-10 so that it would just have a total of 2 columns, one with [AUTORESTART] and the other "Mar 17 21:21:32 GMT 2022" as one column.
Is there a way to do this?
CodePudding user response:
You can put in string literal inside awk
print command.
Here's an example:
$ cat a
1 2 3 [AUTORESTART] Mar 17 21:21:32 GMT 2022
$ cat a | awk '{print $4 "," $6 " " $7 " " $8 " " $9 " " $10}'
[AUTORESTART],17 21:21:32 GMT 2022
You can see that I print 4th column, then a literal comma, then 6th column, then literal space, and so on until 10th column
You can then redirect it to a csv file
$ cat a | awk '{print $4 "," $6 " " $7 " " $8 " " $9 " " $10}' > mycsv.csv
CodePudding user response:
Would something like this work? At least if you don't need it all in one piped command:
newstring=`echo $mystring | cut -d' ' -f1`
newstring="$newstring,`echo $mystring | cut -d' ' -f2-`"
echo $newstring # prints out [AUTORESTART],Mar 17 21:21:32 GMT 2022