LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Get data from multi lined text file using awk, sed or perl - grep & cut not upto par (https://www.linuxquestions.org/questions/programming-9/get-data-from-multi-lined-text-file-using-awk-sed-or-perl-grep-and-cut-not-upto-par-817506/)

cam34 07-01-2010 05:13 PM

Get data from multi lined text file using awk, sed or perl - grep & cut not upto par
 
I need a loop that pulls out the user name into a variable and then pulls out the LastUpdate field into another variable so I can then perform a comparison against the last update field.

Requirements are AIX tools including AWK, SED and Perl

I am writing a script to check AIX users password expiration dates and if they are within the alerting period (ie. 7 days etc) it will email the user.
I will release the full script into the public domain once completed.

The text file I want to parse is formatted like:
Code:

colettel:
        password = XSON0m4SdIQDw
        lastupdate = 1260829398

andrewwa:
        password = ktttjY3Q1ay22
        lastupdate = 1260829665

davidm:
        password = cb5vaQT9NOP0Y
        lastupdate = 1260830289

peterr:
        password = l40lbIXyrvRHw
        lastupdate = 1260996018
        flags = ADMCHG

polarisb:
        password = emGciT1C/VPJw
        lastupdate = 1274912595

Usually I would grep and cut fields but thats not really going to happen for me with these multi line files - Thanks in advance

*Cam

syg00 07-01-2010 06:24 PM

"awk '/:/ {name=$1} ; /lastu/ {print name $3}' input.file" should get you started (assumes well-formed data). dealing with the colon should be trivial.

KenJackson 07-01-2010 06:36 PM

Cant' we just use a shell script?
Code:

#!/bin/sh
test -f "$1"  ||  { echo "Can't find file \"$1\""; exit 1; }
name= password= lastupdate= flags=

while read first ignore data otherjunk; do

    if [ -z "$first" ]; then
        if [ -n "$name" ]; then

            # Do your processing here
            echo "$name, $password, $lastupdate, $flags"

            name= password= lastupdate= flags=
        fi
    elif [ -z "$ignore" ]; then
        name="${first%:*}"
    elif [ "$first" = password ]; then
        password="$data"
    elif [ "$first" = lastupdate ]; then
        lastupdate="$data"
    elif [ "$first" = flags ]; then
        flags="$data"
    else
        echo "ERROR \"$first\" \"$ignore\" \"$data\" \"$otherjunk\""
    fi
done < "$1"

# May need to do the last user here

if [ -n "$name" ]; then
    echo "$name, $password, $lastupdate, $flags"
fi


everToulouse 07-01-2010 11:51 PM

Code:

awk 'BEGIN { RS = "" }
  { name = substr($1,1,length($1)-1);
  if ( systime()-604800 <= $7 ) print name }
' inputFile


cam34 07-02-2010 03:10 AM

Thanks Syg00 hit the nail on the head. I can now parse that output to extract the two variables I require - Username and LastUpdate time!

Thanks Heaps.

Thanks to everyone else who gave their input also!


All times are GMT -5. The time now is 10:54 PM.