Score:-4

How to convert text to csv from linux?

cn flag
azureuser@puppetagent-ubuntu1:/tmp$ cat /bin/user_activity.sh
#!/bin/bash
echo `last` > /tmp/login_history.txt

I'm tracking user data in linux through the "last" command so I'm appending data to .txt and .csv files but still data is not coming as format as i'm expecting.

cat out.txt:

azureuse pts/1 157.47.38.139 Mon Sep 20 14:30 still logged in 
azureuse pts/0 157.47.56.225 Mon Sep 20 12:33 - 14:34 (02:00) azureuse pts/0 157.47.56.225 Mon Sep 20 10:29 - 12:33 (02:03) azureuse pts/0 157.47.56.225 Mon Sep 20 09:53 - 10:28 (00:35) azureuse pts/0 157.47.37.28 Mon Sep 20 09:09 - 09:53 (00:43) azureuse pts/1 157.47.40.253 Mon Sep 20 05:30 - 08:51 (03:20) azureuse pts/0 157.47.40.253 Mon Sep 20 05:28 - 08:51 (03:22) azureuse pts/0 157.48.201.134 Fri Sep 17 12:15 - 16:10 (03:54) azureuse pts/0 157.48.201.134 Fri Sep 17 08:47 - 11:38 (02:50) azureuse pts/0 157.48.201.134 Fri Sep 17 06:07 - 08:17 (02:09) azureuse pts/0 157.48.139.114 Thu Sep 16 14:53 - 14:57 (00:04) azureuse pts/0 157.48.139.114 Thu Sep 16 14:48 - 14:48 (00:00) azureuse pts/0 157.48.139.114 Thu Sep 16 13:49 - 14:47 (00:58) reboot system boot 5.8.0-1041-azure Thu Sep 16 13:29 still running wtmp begins Thu Sep 16 13:29:48 2021

im expecting like below

testvm:/etc/puppetlabs/code/environments/production/manifests$ last
 azureuse pts/0        104.46.119.134   Mon Sep 20 14:29   still logged in
 azureuse pts/1        104.46.119.134   Mon Sep 20 09:52 - 14:35  (04: 42)
 azureuse pts/0        104.46.119.134   Mon Sep 20 09:10 - 09:53  (00:42)
in flag
Apologies, but could you [edit] your question to fix the formatting? I tried to do it for you, but the first bit does not match the third bit, and the blockquote in the middle seems copy/pasted from 3 different sources. Be sure to also include the version of Ubuntu that you are running, as this makes it easier to provide specific suggestions.
pLumo avatar
in flag
"*not coming as format as i'm expecting*". What format do you expect? Please add example output. And also tell us what you tried.
hariraj avatar
cn flag
expecting comma separated values like as csv
cn flag
Ray
I can see why you would like it, but the format provided by `last` is just normal text with a space character between each field. So, in your example, there is a space character between `134` and `Mon` and between `Mon` and `Sep`. If you need it separated, then you have to write a program to parse it yourself....
hariraj avatar
cn flag
It is normal text only but at the same time I want to put the same text into the file when appending into the file all the data gets fixed. So I don't want that .Is there a way to achieve the same.?
Score:0
cn flag

You can use utmpdump to dump /var/log/wtmp:

utmpdump < /var/log/wtmp | IFS=\] mapfile -d \[; a=("${MAPFILE[@]%% *}"); IFS=,; for ((i=0; i<${#a[@]}; i+=8)); do echo "${a[*]:$i:8}"; done
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.