header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
Converting a txt file to a table
Last Post 18 May 2011 12:17 PM by kas1pal. 10 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
HermanMUser is Offline
New Member
New Member
Posts:3
Avatar

--
03 Aug 2010 09:04 AM
    Hi, I’m new, I think powershell is great but I’ve got a problem I can seem to solve. Maybe you can help? I have a list of values in a text file I’m trying to convert into a table

    e.g.
    Name=Fred Address=Street1 Number=123
    Name=Janet Address=Street2 Number=345
    Etc…

    Name    Address   Number
    Fred      Street1    123
    Janet      Street2    345

    I’ve been playing around with things like:

    $o = New-Object object $o | Add-Member -Name "name" -Value "Jack" -MemberType NoteProperty $o | gm

    And

    "Name=Janet".split('=')[0]
    "Name=Janet".split('=')[1]

    But I can’t get the dam thing to work. Am I even going in the right direction?

    Thanks in advance.
    George HowarthUser is Offline
    Basic Member
    Basic Member
    Posts:360
    Avatar

    --
    03 Aug 2010 12:11 PM

    function ConvertTo-Object
    {
        param (
            [String]$Person
        )
       
        $objPerson = New-Object PSObject
       
        $Person.Trim() -split ' ' | ForEach-Object {
            $objPerson | Add-Member -MemberType NoteProperty -Name ($_ -split '=')[0] -Value ($_ -split '=')[1]
        }
       
        return $objPerson
    }

    Get-Content -Path "file.txt" | ForEach-Object { ConvertTo-Object -Person $_ } | Format-Table

    HermanMUser is Offline
    New Member
    New Member
    Posts:3
    Avatar

    --
    04 Aug 2010 12:37 AM
    Thanks, that works great for the information I gave you, unfortunatly that was wong! Sorry.

    The test file is in the format:
    Name=Fred
    Address=Street1
    Number=123
    Name=Janet
    Address=Street2
    Number=345

    I'll see if I can adapt the code.

    Again, sorry.
    George HowarthUser is Offline
    Basic Member
    Basic Member
    Posts:360
    Avatar

    --
    04 Aug 2010 01:11 AM

    $people = @()

    $data = Get-Content -Path "file.txt"

    for ($i = 0; $i -lt $data.Length; $i += 3)
    {
        $name = ($data[$i] -split '=')[1]
        $address = ($data[$i + 1] -split '=')[1]
        $number = ($data[$i + 2] -split '=')[1]
       
        $people += New-Object PSObject -Property @{
            Name = $name
            Address = $address
            Number = $number
        }
    }

    $people | Format-Table

    HermanMUser is Offline
    New Member
    New Member
    Posts:3
    Avatar

    --
    04 Aug 2010 02:43 AM
    You are a genius. Many thanks.
    Joel "Jaykul" BennettUser is Offline
    Basic Member
    Basic Member
    Posts:112
    Avatar

    --
    04 Aug 2010 08:43 AM
    There's a ConvertFrom-PropertyString function on PoshCode http://poshcode.org/1956 which is designed to handle this stuff. It can handle parsing ini files, unix property files, and even simple not-nested yaml -- because they're all very similar.

    You can just use a lookahead to split output objects whenever there's a new "Name=" line:
    ConvertFrom-PropertyString -InputFile file.txt -RecordSeparator "`n(?=Name=)"
    kas1palUser is Offline
    New Member
    New Member
    Posts:4
    Avatar

    --
    12 May 2011 03:20 PM
    hi,
    i have a code like
    #!/bin/sh
    export DATE=`date +%Y%m%d`
    cut -f 1,3,5,6,7 -d : /etc/passwd > $DATE.txt

    $DATE.txt file now contains data like

    sin444:3021:Richard :/home/sin444
    pas555:3454:mona :/home/pas555

    How to change this format in to table like this and store it in a file

    user name| ID | Full name |Home Director |
    sin444 | 3021 |Richard |/home/sin444|
    pas555| 3454 |mona |/home/pas555 |

    Please help
    Thanks in advance
    MattUser is Offline
    New Member
    New Member
    Posts:31
    Avatar

    --
    12 May 2011 09:50 PM
    Assuming you mean change the Date.txt file after this script has created it, there are a few ways you can get that information changed:


    #set up the header (doesn't need to be done if the header is in the file)$header="user name", "ID", "Full name", "Home Director"# import your file (called del.txt here)$file=Import-Csv-Delimiter":" .\del.txt-Header$header#from there you can do loads of stuff with it
    # convert to pipe delimited without double quotes$file | ConvertTo-Csv-Delimiter"|" | % {$_.Replace('"','')}
    #export to pipe delimtied $file | Export-Csv-Delimiter"|"out_del.txt# format as a table on screen$file | Format-Table
    Web: http://amonkeysden.tumblr.com
    Twitter: @aMonkeysDen
    Melissa DaisyUser is Offline
    New Member
    New Member
    Posts:2
    Avatar

    --
    15 May 2011 05:18 PM


    Well, great work! You have helped me to improve my knowledge about this field. Thank you so much for sharing.


    __________________
    [url=http://letswatchmovies.org]watch online movies[/url]
    kas1palUser is Offline
    New Member
    New Member
    Posts:4
    Avatar

    --
    17 May 2011 11:15 AM
    hi

    #!/bin/sh
    export DATE=`date +%Y%m%d`
    export HOST=`hostname`
    for i in `cat /etc/passwd`
    do
    uname=`echo $i | cut -f 1 -d : /etc/passwd`
    uid=`echo $i | cut -f3 -d : /etc/passwd`
    fullname=`echo $i | cut -f5 -d : /etc/passwd`
    homedirectory=` echo $i | cut -f6 -d : /etc/passwd`
    done
    echo hostname is $HOST. >> ${DATE}.txt
    echo $uname $uid $fullname $homedirectory >> ${DATE}.txt


    This code is producing a paragraph like this
    hostname is XXXX.com.
    root bin daemon adm lp sync shutdown halt mail news uucp operator games gopher ftp nobody nscd vcsa rpc mailnull smmsp apache oprofile pcap ntp dbus avahi hsqldb sshd rpcuser nfsnobody haldaemon avahi-autoipd xfs gdm sabayon sophosav kas1pal 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 99 28 69 32 47 51 48 16 77 38 81 70 96 74 29 65534 68 100 43 42 86 101 3454 root bin daemon adm lp sync shutdown halt mail news uucp operator games gopher FTP User Nobody NSCD Daemon virtual console memory owner Portmapper RPC user Apache Special user account to be used by OProfile System message bus Avahi daemon Privilege-separated SSH RPC Service User Anonymous NFS User HAL daemon avahi-autoipd X Font Server Sabayon user Sophos Anti-Virus (CR/RTC-NA) /root /bin /sbin /var/adm /var/spool/lpd /sbin /sbin /sbin /var/spool/mail /etc/news /var/spool/uucp /root /usr/games /var/gopher /var/ftp / / /dev / /var/spool/mqueue /var/spool/mqueue /var/www /home/oprofile /var/arpwatch /etc/ntp / / /var/lib/hsqldb /var/empty/sshd /var/lib/nfs /var/lib/nfs / /var/lib/avahi-autoipd /etc/X11/fs /var/gdm /home/sabayon /opt/sophos-av /home/kas1pal

    how can i get this in table format with uname uid fullname homedirectory as headings.
    kas1palUser is Offline
    New Member
    New Member
    Posts:4
    Avatar

    --
    18 May 2011 12:17 PM
    Thanks for the reply

    But the code which you have given is giving errors.


    $header="user name", "ID", "Full name", "Home Director"

    # import your file (called del.txt here)
    $file=Import-Csv-Delimiter":" ${DATE}.txt-Header$header

    #from there you can do loads of stuff with it
    # convert to pipe delimited without double quotes

    $file | ConvertTo-Csv-Delimiter"|" | % {$_.Replace('"','')}

    #export to pipe delimtied
    $file | Export-Csv-Delimiter"|"out_${DATE}.txt
    # format as a table on screen

    $file | Format-Table



    errors:

    table2.sh: line 6: =user name,: command not found
    table2.sh: line 9: =Import-Csv-Delimiter:: command not found
    table2.sh: line 14: syntax error near unexpected token `('
    table2.sh: line 14: `$file | ConvertTo-Csv-Delimiter"|" | % {$_.Replace('"','')}`

    I am running it on shell
    how can i solve this?
    Thank You



    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer