Text file parsing.
Page 1 of 1

Author:  millerdc [ Sun Nov 01, 2009 12:19 am ]
Post subject:  Text file parsing.

Can someone show me an example or point me to example of how to parse a text file and pull out info to put into another text file? For example lets say I have a text file with the following info.

name: Bob Johnson
age: 47
state: CA
job: Fork lift operator

name: Steve Baker
age: 33
state: TX
job: Sales


How would I pull just the persons name and state and put them into another text file in the same order so it would look like the following.

Bob Jonson
Steve Baker

Or if I wanted to format it like so..

Bob Jonson CA
Steve Baker TX


Author:  fredrik.eriksson [ Mon Nov 02, 2009 3:21 am ]
Post subject:  Re: Text file parsing.

There are ofcourse multiple ways of doing this.

I'm going to assume that the file is consecutive and that all the "entrys" contains name and state (this will minimize the size of the scripts).
# Path to file
# Creates a list containing only "name:" and "state:" fields.
list=$(cat $file | egrep "(name|state):"
# Inserts the list into an array for easier access later on.
for i in $list; do

# Here's the magic, The loop will continue until the condition is met, meaning that a element is empty :)
until [ -z $array[$x] ]; do
   # Chops out the "name: " part of the string
   name=$(echo ${array[$x]} | sed -e "s/^[^:]\+: //")
   # Increases the positioning variable so we can get the next row

   # Chops out the "state: " part of the string
   state=$(echo ${array[$x]} | sed -e "s/^[^:]\+: //")
   # Increases the positioning variable so it's at the right place next itteration

   # Outputs the name and state on the same row :)
   echo "$name $state"

If you want just the name and the state on seperate rows then this is a easy fix :P
# cat file.txt | egrep "(name|state):"

If the file isn't consecutive then this script is useless without modification... The issue which it has is there is no checks at all if the next line is the one which should be there.

If 1 entry is missing the state field then all other will get out of alignment. This can be solved by adding if-conditions to check if the line is a "state:" line or not. Also it will probably make some funny results if the sed doesn't match the line :P

ps. none of the examples or suggestion I've given has been tested :P ds.

Best regards
Fredrik Eriksson

Author:  millerdc [ Tue Nov 03, 2009 12:51 pm ]
Post subject:  Re: Text file parsing.


Thanks for the detailed explanation!

Page 1 of 1 All times are UTC - 6 hours
© 2000, 2002, 2005, 2007 phpBB Group •