Convert Columns in Rows delimited by a strings


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Convert Columns in Rows delimited by a strings
# 1  
Old 02-18-2014
Convert Columns in Rows delimited by a strings

Hi Gurus,
I have a file that contain inventory information from someones computers:

Code:
UserName
domain\user1
DNSHostName
machine1
Caption
Microsoft Windows 7 Professional
OSArchitecture
64 bits
SerialNumber
XXX
Name
HP EliteBook Revolve 810 G1
NumberOfProcessors
1
Name
Intel(R) Core(TM) i5-3437U CPU @ 1.90GHz
TotalVisibleMemorySize
12443116
IPAddress
{"1.2.4.7", "fe80::e967:1d63:ebfa:8fa1"}
Description
Microsoft Office Professional Plus 2010
Microsoft Office OneNote MUI (Spanish) 2010
Microsoft Office Office 32-bit Components 2010
Microsoft Office Shared 32-bit MUI (Spanish) 2010
Microsoft Office InfoPath MUI (Spanish) 2010
Microsoft Office Access MUI (Spanish) 2010
Microsoft Office Excel MUI (Spanish) 2010
Microsoft Office PowerPoint MUI (Spanish) 2010
UserName
DNSHostName
machine2
Caption
Microsoft Windows Server 2008 R2 Enterprise
OSArchitecture
64-bit
SerialNumber
VMware-42 39 f1 5b 54 c0 fd 4d-eb 1e 23 4d bb a4 92 8c
Name
VMware Virtual Platform
NumberOfProcessors
4
Name
Intel(R) Xeon(R) CPU           X5670  @ 2.93GHz
Intel(R) Xeon(R) CPU           X5670  @ 2.93GHz
Intel(R) Xeon(R) CPU           X5670  @ 2.93GHz
Intel(R) Xeon(R) CPU           X5670  @ 2.93GHz
TotalVisibleMemorySize
8388152
IPAddress
{"1.2.4.8"}
Description
Tripwire Enterprise Agent
Microsoft Visual C++ 2005 Redistributable (x64)
Microsoft Visual C++ 2005 Redistributable
UserName
domain\user3
DNSHostName
machine3
Caption
Microsoft Windows 7 Professional
OSArchitecture
64 bits
SerialNumber
xxx
Name
HP EliteBook Revolve 810 G1
NumberOfProcessors
1
Name
Intel(R) Core(TM) i5-3437U CPU @ 1.90GHz
TotalVisibleMemorySize
12443116
IPAddress
{"1.2.4.9", "fe80::69ff:c1f1:1b19:38c7"}
{"192.1.1.40", "fe80::58b5:64a4:6582:6402"}
Description
Microsoft Lync Web App Plug-in
Microsoft Office Professional Plus 2010
Microsoft Office OneNote MUI (Spanish) 2010
Microsoft Office Office 32-bit Components 2010
Microsoft Office Shared 32-bit MUI (Spanish) 2010


I search something like this:

1.- in some case the UserName is not captured when this happens, the field should be left blank.

2.- for each machine, there are many programs installed, I need for each program generate new line containing the name of the program and machine(hostname).

3.- If any field not get information, need to insert blank.

4.- Field Separator needed is pipe character.


I search something like this:

Code:
UserName|DNSHostName|Caption|OSArchitecture|SerialNumber|Name|NumberOfProcessors|Name|TotalVisibleMemorySize|IPAddress|Description
domain\user1|machine1|Microsoft Windows 7 Professional	64 bits|XXX|HP EliteBook Revolve 810 G1	1|Intel(R) Core(TM) i5-3437U CPU @ 1.90GHz|12443116|1.2.4.7|Microsoft Office Professional Plus 2010
 |machine1| | | | | | | | |Microsoft Office OneNote MUI (Spanish) 2010
 |machine1| | | | | | | | |Microsoft Office Office 32-bit Components 2010
 |machine1| | | | | | | | |Microsoft Office Shared 32-bit MUI (Spanish) 2010
 |machine1| | | | | | | | |Microsoft Office Access MUI (Spanish) 2010
 |machine1| | | | | | | | |Microsoft Office Excel MUI (Spanish) 2010
 |machine1| | | | | | | | |Microsoft Office PowerPoint MUI (Spanish) 2010
 |machine1| | | | | | | | |Microsoft Office OneNote MUI (Spanish) 2010
 |machine1| | | | | | | | |Microsoft Office OneNote MUI (Spanish) 2010
 |machine2|Microsoft Windows Server 2008 R2 Enterprise|64-bit|VMware-42 39 f1 5b|VMware Virtual Platform|4|Intel(R) Xeon(R) CPU X5670  @ 2.93GHz,Intel(R) Xeon(R) CPU X5670,Intel(R) Xeon(R) CPU X5670,Intel(R) Xeon(R) CPU X5670,Intel(R) Xeon(R) CPU X5670,Intel(R) Xeon(R) CPU X5670,Intel(R) Xeon(R) CPU X5670,Intel(R) Xeon(R) CPU X5670,Intel(R) Xeon(R) CPU X5670,Intel(R) Xeon(R) CPU X5670|8388152|1.2.4.8|Tripwire Enterprise Agent
 |machine2| | | | | | | | |Microsoft Visual C++ 2005 Redistributable (x64)
 |machine2| | | | | | | | |Microsoft Visual C++ 2005 Redistributable
domain\user3|machine3|Microsoft Windows 7 Professional|64 bits|xxx|HP EliteBook Revolve 810 G1|1|Intel(R) Core(TM) i5-3437U CPU @ 1.90GHz|12443116|1.2.4.9|Microsoft Lync Web App Plug-in
 |machine3| | | | | | | | |Microsoft Office Professional Plus 2010
 |machine3| | | | | | | | |Microsoft Office OneNote MUI (Spanish) 2010
 |machine3| | | | | | | | |Microsoft Office Professional Plus 2010
 |machine3| | | | | | | | |Microsoft Office Professional Plus 2010



Is it possible to achieve this with awk?

I much appreciate any help

Regards MG.
# 2  
Old 02-18-2014
Here is something I wrote based on your input data:
Code:
awk '
        BEGIN {
         print "UserName|DNSHostName|Caption|OSArchitecture|SerialNumber|NumberOfProcessors|TotalVisibleMemorySize|IPAddress|Name|Name|Description"
         n = split ( "UserName:DNSHostName:Caption:OSArchitecture:SerialNumber:NumberOfProcessors:TotalVisibleMemorySize:IPAddress", V, ":" )
         while ( ++k <= n )
               H[V[k]]
        }
        /UserName/ {
                f = 0
                c = 0
                split ( "", R )
        }
        function getval(par, str)
        {
                if ( par == "IPAddress" )
                        gsub ( /\{\"|\".*/, X, str )
                return str
        }
        /^Name/ {
                getline
                N[++c] = $0
                next
        }
        $1 in H {
                i = $1
                getline
                if ( !( $1 in H ) )
                {
                        if ( $1 != "Name" )
                                R[i] = getval( i, $0 )
                }
                else
                {
                        i = $1
                        getline
                        if ( $1 != "Name" )
                                R[i] = getval( i, $0 )
                }
                next
        }
        /Description/ {
                f = 1
                next
        }
        f && !/Description/ {
                for ( j = 1; j <= n; j++ )
                        printf "%s|", R[V[j]]
                printf "%s|%s|", N[1], N[2]
                printf "%s\n", $0
        }
' OFS='|' file

Feel free to make changes as per your requirement.
# 3  
Old 02-18-2014
Yoda Wuaaau many thanks. Your code turned out successful for me.
After will post my own script.


Regards M.G.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Transpose comma delimited data in rows to columns

Hello, I have a bilingual database with the following structure a,b,c=d,e,f The right half is in a Left to right script and the second is in a Right to left script as the examples below show What I need is to separate out the database such that the first word on the left hand matches the first... (4 Replies)
Discussion started by: gimley
4 Replies

2. Shell Programming and Scripting

Convert rows to columns

hi folks, I have a sample data like what is shown below: 1,ID=1000 1,Org=CedarparkHospital 1,cn=john 1,sn=doe 1,uid=User001 2,uid=User002 2,ID=2000 2,cn=steve 2,sn=jobs 2,Org=Providence I would like to convert it into the below format: 1,1000,CedarparkHospital,john,doe,User001... (11 Replies)
Discussion started by: vskr72
11 Replies

3. Shell Programming and Scripting

Convert rows to columns

I am looking to print the data in columns and after every 3 words it should be a new row. cat example.out | awk 'END { for (i = 0; ++i < m;) print _;print _ }{ _ = _ x ? _ OFS $1 : $1}' m=1| grep -i INNER I am looking to print in a new line after every 3 words. ... (2 Replies)
Discussion started by: lazydev
2 Replies

4. Shell Programming and Scripting

Convert Rows to Columns

Hi Everyone, Could someone shed some lights on how to convert the records in rows form into column basis. 172.29.59.12 IBM,8255-E8B 102691P 8 65536 MB 6100-04-11-1140 172.29.59.15 IBM,8255-E8B 102698P 4 45056 MB 6100-04-11-1140 IP SYS MODEL ... (6 Replies)
Discussion started by: ckwan
6 Replies

5. Shell Programming and Scripting

Convert few columns to rows

Hi! Does anybody help me in converting following data: INPUT looks like this: 20. 100. 30 200. 40. 400. 50. 100. 60. 200. 70. 400. 80. 200. 150. 210. 30. 100. OUTPUT should look like this: 20. 100. 30 200. 40. 400. 50. 100. 60. 200. 70.... (5 Replies)
Discussion started by: lovelinux
5 Replies

6. Shell Programming and Scripting

convert rows to columns

hi, i have the file as below: abc def ghi jkl i want the output as abc,def,ghi,jki please reply, Thanks (4 Replies)
Discussion started by: namitai
4 Replies

7. Shell Programming and Scripting

Convert Rows to Space Delimited Line

Hello, I would like to convert a list of rows to a space separated line. Any ideas? Input file: "Administration Tools" "Design Suite" "Dial-up Networking Support" "Editors" desired output "Administration Tools" "Design Suite" "Dial-up Networking Support" "Editors" Thanks,... (4 Replies)
Discussion started by: jaysunn
4 Replies

8. Shell Programming and Scripting

convert columns into rows

hi, Apologies if this has been covered. I have requirement where i have to convert a single column into multiple column. My data will be like this - 2 3 4 5 6 Output required - 2 3 4 5 6 (1 Reply)
Discussion started by: Nishithinfy
1 Replies

9. Shell Programming and Scripting

So I converted columns to rows but I want it to be tab delimited and also I want.....

Hi, So my file looks like this: title number JR 2 JR 2 JR 4 JR 5 NM 5 NM 8 NM 2 NM 8 I used this line that I wrote to convert it to rows so it will look like this: awk -F"\t" '!/^$/{a=a" "$3} END {for ( i in a) {print i,a}}' occ_output.tab > test.txt JR 2 2 4 5 NM 5 8... (4 Replies)
Discussion started by: kylle345
4 Replies

10. Shell Programming and Scripting

how to convert columns to rows

Hi, I need a shell script for below requirement Input file P1 - 173310 P2 - 173476 P3 - 173230 P4 - 172737 P1 - 173546 P2 - 173765 P3 - 173876 P4 - 172989 Out put file P1 173310 173546 P2 173476 173765 P3 173230 173876 P4 172737 172989 Suresh (6 Replies)
Discussion started by: suresh3566
6 Replies
Login or Register to Ask a Question