944,174 Members | Top Members by Rank

Ad:
Oct 21st, 2009
0

Shell script to read lines in a text file and filter user data

Expand Post »
hi all,

I have this file [myfile.txt] with some user data.

example:
$cat myfile.txt
FName|LName|Gender|Company|Branch|Bday|Salary|Age
aaaa|bbbb|male|cccc|dddd|19900814|15000|20|
eeee|asdg|male|gggg|ksgu|19911216|||
aara|bdbm|male|kkkk|acke|19931018||23|
asad|kfjg|male|kkkc|gkgg|19921213|14000|24|
aera|bprb|male|cccc|pppp||15000|20|
.
.
. // and so on


So what I want to do is to take out (to a file) the missing fields as following format:

<FName> <LName> <Company> Missing Field/s:<> <>

example output:

eeee asdg gggg Missing Field/s: Salary Age
aara bdbm kkkk Missing Field/s: Salary

CAN ANYONE HELP ME PLEASE !!!!!!!!!!!!!!!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
e04047 is offline Offline
2 posts
since Aug 2008
Oct 21st, 2009
1
Re: Shell script to read lines in a text file and filter user data
Here is a start to your solution.
Shell Scripting Syntax (Toggle Plain Text)
  1. #!/bin/bash
  2.  
  3. filename="/home/sk/daniweb/scripts/data.txt"
  4. exec<${filename}
  5. value=0
  6. declare -a arr
  7. while read line
  8. do
  9. varray=(`echo $line | awk '{ split($0, ulist, "|"); for(i=1; i<=11; i++) printf ulist[i] " "; }'`)
  10. echo "Array Length: " ${#varray[@]}
  11. done

That will read the input from your file in to an array inside of bash. Now you need to find the empty values and pull the header information.
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Oct 21st, 2009
0

FYI

hi all,
I found the answer.Go through the link
==========================================
http://www.linuxquestions.org/questi...r-data-763384/
===========================================
Click to Expand / Collapse  Quote originally posted by e04047 ...
hi all,

I have this file [myfile.txt] with some user data.

example:
$cat myfile.txt
FName|LName|Gender|Company|Branch|Bday|Salary|Age
aaaa|bbbb|male|cccc|dddd|19900814|15000|20|
eeee|asdg|male|gggg|ksgu|19911216|||
aara|bdbm|male|kkkk|acke|19931018||23|
asad|kfjg|male|kkkc|gkgg|19921213|14000|24|
aera|bprb|male|cccc|pppp||15000|20|
.
.
. // and so on


So what I want to do is to take out (to a file) the missing fields as following format:

<FName> <LName> <Company> Missing Field/s:<> <>

example output:

eeee asdg gggg Missing Field/s: Salary Age
aara bdbm kkkk Missing Field/s: Salary

CAN ANYONE HELP ME PLEASE !!!!!!!!!!!!!!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
e04047 is offline Offline
2 posts
since Aug 2008
Oct 21st, 2009
0
Re: Shell script to read lines in a text file and filter user data
Another crafty solution

Please mark this thread as solved as you have found an answer to your question and good luck!
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Oct 30th, 2009
-2
Re: Shell script to read lines in a text file and filter user data
thanks for sharing this information with us. its really very useful for me and all of us here.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
natufor is offline Offline
1 posts
since Oct 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: Awk Problems
Next Thread in Shell Scripting Forum Timeline: Counter





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC