LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-01-2010, 05:13 PM   #1
cam34
Member
 
Registered: Aug 2003
Distribution: Fedora 22, Debian 8, Centos 6/7 for servers
Posts: 101

Rep: Reputation: 16
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
 
Old 07-01-2010, 06:24 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,236

Rep: Reputation: 4150Reputation: 4150Reputation: 4150Reputation: 4150Reputation: 4150Reputation: 4150Reputation: 4150Reputation: 4150Reputation: 4150Reputation: 4150Reputation: 4150
"awk '/:/ {name=$1} ; /lastu/ {print name $3}' input.file" should get you started (assumes well-formed data). dealing with the colon should be trivial.
 
Old 07-01-2010, 06:36 PM   #3
KenJackson
Member
 
Registered: Jul 2006
Location: Maryland, USA
Distribution: Fedora and others
Posts: 757

Rep: Reputation: 145Reputation: 145
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

Last edited by KenJackson; 07-01-2010 at 06:40 PM.
 
Old 07-01-2010, 11:51 PM   #4
everToulouse
LQ Newbie
 
Registered: Apr 2010
Posts: 18

Rep: Reputation: 5
Code:
awk 'BEGIN { RS = "" }
   { name = substr($1,1,length($1)-1);
   if ( systime()-604800 <= $7 ) print name }
' inputFile
 
Old 07-02-2010, 03:10 AM   #5
cam34
Member
 
Registered: Aug 2003
Distribution: Fedora 22, Debian 8, Centos 6/7 for servers
Posts: 101

Original Poster
Rep: Reputation: 16
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!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use grep, cut, or awk to get an IP from a file chudster Linux - General 4 02-03-2010 07:06 PM
Sed/awk/grep search for number string of variable length in text file Alexr Linux - Newbie 10 01-19-2010 01:34 PM
How to use command grep,cut,awk to cut a data from a file? hocheetiong Linux - Newbie 7 09-11-2008 07:16 PM
sed/awk/grep for multiple line data hotrodmacman Programming 8 10-18-2007 11:06 AM
awk/sed to grep the text ahpin Linux - Software 3 10-17-2007 12:34 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration