Register
It is currently Sat Oct 25, 2014 5:08 am

Text file parsing.


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Sun Nov 01, 2009 12:19 am   

Joined: Sun Nov 01, 2009 12:00 am
Posts: 2
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
CA
Steve Baker
TX

Or if I wanted to format it like so..

Bob Jonson CA
Steve Baker TX

Thanks.


Top
 Profile  
 PostPosted: Mon Nov 02, 2009 3:21 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
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).
Code:
#!/bin/bash
# Path to file
file="/path/to/file"
# Creates a list containing only "name:" and "state:" fields.
list=$(cat $file | egrep "(name|state):"
x=0
# Inserts the list into an array for easier access later on.
for i in $list; do
   array[$x]=$i
   ((x++))
done

# Here's the magic, The loop will continue until the condition is met, meaning that a element is empty :)
x=0
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
   ((x++))   

   # 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
   ((x++))   

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


If you want just the name and the state on seperate rows then this is a easy fix :P
Code:
# 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


Top
 Profile  
 PostPosted: Tue Nov 03, 2009 12:51 pm   

Joined: Sun Nov 01, 2009 12:00 am
Posts: 2
fredrik,

Thanks for the detailed explanation!


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP