Score:1

bash: How to create a key,value pairs from .txt file in linux

cn flag
w | awk 'NR==1 {print $1}' >file.txt
cat file
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
lava1    pts/0    157.48.149.102   05:03   31.00s  0.31s  0.31s -bash
azureuse pts/1    157.48.149.102   07:26    0.00s  0.07s  0.05s w

From the above text file I want to create key,value pairs like json format as below is the expected output:

{
 "USER" : "lava1",
 "TTY" : "pts/1",
 "FROM" : "157.47.49.254",
 "LOGIN" : "12:32",
 "IDLE" : "5.00s"
}
cn flag
In bash? see https://stackoverflow.com/questions/48470049/build-a-json-string-with-bash-variables
Andrej Podzimek avatar
cn flag
The `awk` command in your question makes no sense. First, it writes into a different file, not into the one you `cat` below. Second, `file.txt` will contain the current time (from `w` output), not the lines that `file` contains.
Andrej Podzimek avatar
cn flag
`PROCPS_USERLEN=32 PROCPS_FROMLEN=32 w | tail -n+2 | while read -r user tty from login idle discard; do echo -e '{\n "USER" : "'"$user"'",\n "TTY" : "'"$tty"'",\n "FROM" : "'"$from"'",\n "LOGIN" : "'"$login"'",\n "IDLE" : "'"$idle"'"\n}'; done` Anyway, the two environment variables show why parsing the output from `w` is simply a bad idea. Not to mention that you may need to also export (e.g.) `LC_ALL=en_US.UTF-8` to get consistent output. Otherwise the login date will be spelled out in the current locale etc.
Score:2
hr flag

I'd suggest Miller for something like this - specifically, convert from "pretty print" to JSON:

$ mlr --ipprint --ojson cat file
{ "USER": "lava1", "TTY": "pts/0", "FROM": "157.48.149.102", "LOGIN@": "05:03", "IDLE": "31.00s", "JCPU": "0.31s", "PCPU": "0.31s", "WHAT": "-bash" }
{ "USER": "azureuse", "TTY": "pts/1", "FROM": "157.48.149.102", "LOGIN@": "07:26", "IDLE": "0.00s", "JCPU": "0.07s", "PCPU": "0.05s", "WHAT": "w" }

Selecting specific fields with cut and renaming the LOGIN@ field:

$ mlr --ipprint --ojson cut -f USER,TTY,FROM,LOGIN@,IDLE then rename LOGIN@,LOGIN file
{ "USER": "lava1", "TTY": "pts/0", "FROM": "157.48.149.102", "LOGIN": "05:03", "IDLE": "31.00s" }
{ "USER": "azureuse", "TTY": "pts/1", "FROM": "157.48.149.102", "LOGIN": "07:26", "IDLE": "0.00s" }
Score:0
ro flag

I saw this issue and tried to figure it out. I got close, then hit a couple of walls. Ultimately though, your answer is here:

All I ask for is some credit for taking up the charge on this one. Upvotes!

Score:0
cn flag

Since you want to produce JSON, let's use

w \
| tail -n +3 \
| jq -R '. | split("\\s+"; "g") | {USER:.[0], TTY:.[1], FROM:.[2], LOGIN:.[3], IDLE:.[4]}'

I'm using tail to skip the first 2 lines of w output.

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.