rearrange info of file in a "table"


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting rearrange info of file in a "table"
# 1  
Old 06-12-2008
rearrange info of file in a "table"

Please I need to rearrange data acquired by USB port from a sensor network. The information is mixed and I need to convert it into a kind of table. This is my input file:

Node 4D5A joined
Temperature: 27,5
Humidity: 40
Dew Point: 23
No motion detected
LUX: 389
Temperature: 28
Humidity: 41
Dew Point: 21
Node 58DF joined
Temperature: 28,5
Humidity: 41
Dew Point: 24
LUX: 384
Node 4D5A joined
Temperature: 21,5
Humidity: 43
Dew Point: 20
No motion detected

Realize that every time a node send info, first is sent its ID and later different data without any specific pattern. My idea is to identify every line between Node and Node and arrange them into other file. Any pattern is posible... but I don't have any idea on how to implement it. I have been trying with awk, sed, grep... but with no result.

A possible solution could be like this:

Node:data:value
Node:data:value

or

Node
Data:value
Node
Data:value

please give some ideas or solutions Smilie

Thanks a lot!
# 2  
Old 06-12-2008
You may need to change it a bit if you want to remove blanks for values or filter out unwanted lines (maybe you don't need the message "No motion detected", I don't know):

Code:
awk '
   BEGIN { OFS=":" }
   {
      if ($1 == "Node") {
         n=$2
      } else {
         split($0, x, ":");
         print n, x[1], x[2];
      }
   }
' input_file.txt

# 3  
Old 06-12-2008
Great! Now I have easier line identifier, but actually my final target is to get something like this:

4D5A:Temperature: 27,5 / 28 / 21,5
4D5A:Humidity: 40 / 41 / 43
4D5ASmilieew Point: 23 / 21 / 20
4D5A:Motion: 0 / 0
4D5A:LUX: 389
58DF:Temperature: 28,5
58DF:Humidity: 41
58DFSmilieew Point: 24
58DF:Motion: 1
58DF:LUX: 384

Is it posible by awk as well?

Thanks again
# 4  
Old 06-12-2008
This should work. By the way, I didn't understand the sorting "algorithm" you've used for your expected output... Is it a random sort? Smilie

Code:
awk '
   BEGIN { OFS=":" }
   {
      if ($1 == "Node") {
         n=$2
      } else {
         split($0, x, ":");
         key=n ":" x[1];
         val=x[2]?x[2]:" 0";
         y[key]=y[key] (y[key]?" /":"") val;
      }
   }
   END {
      for (node_data in y) print(node_data, y[node_data]);
   }
' input_file.txt | sort

# 5  
Old 06-13-2008
Ahhh... you mean fot Temperature and everything, hehe... sorting alphabetically is right. Thank you very much.

Can you explain me every step of the program?
# 6  
Old 06-13-2008
CPU & Memory

Basically, we first need to know which node every information in the input file is related to and then, given the node name and the measurement name, populate an associative array (hash) which keys are the concatenation between "node_name:measurement_name". For each element, the associated value will be the union of all the consecutive values found, separated by "/".

We once tell awk that the Output Field Separator for print statements will be a colon:
Code:
   BEGIN { OFS=":" }

For each row of the file, we execute the code between curly braces. Here, when we notice that the first space-separated field of the file corresponds to the word "Node", we assign to the "n" variable the node name (=2nd field, $2):
Code:
   {
      if ($1 == "Node") {
         n=$2

If that's not the case, we elaborate the measurement information. By the means of split function, we create an array called "x" which will contain the two colon-separated fields: x[1] is the measurement name (eg. "Temperature"), x[2] is the measurement value (eg. " 27,5"):
Code:
      } else {
         split($0, x, ":");

Now we compose a key for our hash, concatenating "n" (eg. "4D5A"), a colon ":" and the measurement name.
For example, in the second loop, our key will be "4D5A:Temperature", in the 3rd "4D5A:Humidity" and so on.
Code:
         key=n ":" x[1];

The corresponding value for the key just found will be the measurement value, x[2]. Here I've used a ternary operator because you want to display "0" even if the measurement name hasn't an associated value, for example in the case of "No motion detected": if x[2] is not null, val=x[2], else val=" 0".
Code:
         val=x[2]?x[2]:" 0";

Now that we have defined the key and the value, we populate an associative array called "y".
There's another ternary operator: if an element associated with the key is already present, the value will be the actual non-null value (y[key]) followed by a slash " /" and the measurement value (val); if it is the first time we insert a value for the key "key", we don't put the slash.
Code:
         y[key]=y[key] (y[key]?" /":"") val;
      }
   }

Finally, when all the hash is populated with the values got from every record of the input file, we simply print all the key/value pairs. The print output is OFS delimited.
Code:
   END {
      for (node_data in y) print(node_data, y[node_data]);
   }

The awk result is piped through the "sort" command to beautify and make readable the output Smilie

Hope it's more clear now Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

3. UNIX for Advanced & Expert Users

AIX - io info get from "libperfstat" not match "iostat"

Hi, everyone. I need to write a program to get io info based on libperfstat. But the "write time" of a disk is just half of the value get from iostat. I'm confused and can't explain. Help please. How I calculate "write service time per sec": In iostat: write service... (0 Replies)
Discussion started by: jackliang
0 Replies

4. Shell Programming and Scripting

finding the strings beween 2 characters "/" & "/" in .txt file

Hi all. I have a .txt file that I need to sort it My file is like: 1- 88 chain0 MASTER (FF-TE) FFFF 1962510 /TCK T FD2TQHVTT1 /jtagc/jtag_instreg/updateinstr_reg_1 dff1 (TI,SO) 2- ... (10 Replies)
Discussion started by: Behrouzx77
10 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. HP-UX

Need to identify the process/app which is triggering the error "vmunix: file: table is full"

Hi, I'm seeing the error vmunix: file: table is full in syslog.log. Although changing the value of the kernel parameter nfile would make this error go away, how would I identify which process/application in the server is triggering this error? The server is a HP-UX B.11.11. Thanks in advance! (1 Reply)
Discussion started by: enchogas
1 Replies

7. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

8. UNIX for Advanced & Expert Users

Help, filtering some info. from a file under "()"

I HAVE A FILE "TEXT.TXT" WHICH CONTAINS FOLLOWING : CHANNEL(SYSTEM.DEF.CLUSRCVR) CHLTYPE(CLUSRCVR) CHANNEL(TO.XYZ) CHLTYPE(CLUSRCVR) NOW I WANT THE NAMES WRITTEN BETWEEN THE BRACES () UNDER COLUMN ONE, THAT IS, TO.XYZ and SYSTEM.DEF.CLUSRCVR.... (10 Replies)
Discussion started by: varungupta
10 Replies

9. UNIX for Dummies Questions & Answers

how do I dump "info ls" to a file?

how do I dump "info ls" to a file? I did $info ls >a <ENTER> but it didn't dump the whole thing. It only dumped chapter 10. (4 Replies)
Discussion started by: james hanley
4 Replies
Login or Register to Ask a Question