Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 07-01-2010, 05:13 PM   #1
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:
        password = XSON0m4SdIQDw
        lastupdate = 1260829398

        password = ktttjY3Q1ay22
        lastupdate = 1260829665

        password = cb5vaQT9NOP0Y
        lastupdate = 1260830289

        password = l40lbIXyrvRHw
        lastupdate = 1260996018
        flags = ADMCHG

        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

Old 07-01-2010, 06:24 PM   #2
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 13,480

Rep: Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347Reputation: 1347
"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
Registered: Jul 2006
Location: Maryland, USA
Distribution: Fedora, PCLinuxOS
Posts: 580

Rep: Reputation: 65
Cant' we just use a shell script?
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=
    elif [ -z "$ignore" ]; then
    elif [ "$first" = password ]; then
    elif [ "$first" = lastupdate ]; then
    elif [ "$first" = flags ]; then
        echo "ERROR \"$first\" \"$ignore\" \"$data\" \"$otherjunk\""
done < "$1"

# May need to do the last user here

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

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

Rep: Reputation: 5
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
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!


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

All times are GMT -5. The time now is 02:36 PM.

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