Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
10-21-2009, 03:39 AM
|
#1
|
LQ Newbie
Registered: Oct 2009
Posts: 5
Rep:
|
Shell script to read lines in a text file and filter user data
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 !!!!!!!!!!!!!!!
|
|
|
10-21-2009, 05:29 AM
|
#2
|
LQ Veteran
Registered: Sep 2003
Posts: 10,532
|
Hi,
I do have to assume things to solve this:
1) fields 1, 2 and 4 (FName, LName and Company) are always present,
2) rest of the fields could be missing.
I came up with this:
Code:
#!/bin/bash
awk '
BEGIN { FS = "|"
misFields = ""
}
{
if ( $3 == "" ) { misFields=misFields" Gender" }
if ( $5 == "" ) { misFields=misFields" Branch" }
if ( $6 == "" ) { misFields=misFields" Bday" }
if ( $7 == "" ) { misFields=misFields" Salary" }
if ( $8 == "" ) { misFields=misFields" Age" }
if ( misFields != "" ) { print $1, $2, $4, "Missing Field/s:" misFields }
misFields=""
}
' myfile.txt
Test run with the data given in first post:
Quote:
$ 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|
$
$
$ ./blaat
eeee asdg gggg Missing Field/s: Salary Age
aara bdbm kkkk Missing Field/s: Salary
aera bprb cccc Missing Field/s: Bday
|
Hope this helps.
|
|
|
10-21-2009, 06:47 AM
|
#3
|
LQ Newbie
Registered: Oct 2009
Posts: 5
Original Poster
Rep:
|
Thank you
Thank you very much....for your great help.
Quote:
Originally Posted by druuna
Hi,
I do have to assume things to solve this:
1) fields 1, 2 and 4 (FName, LName and Company) are always present,
2) rest of the fields could be missing.
I came up with this:
Code:
#!/bin/bash
awk '
BEGIN { FS = "|"
misFields = ""
}
{
if ( $3 == "" ) { misFields=misFields" Gender" }
if ( $5 == "" ) { misFields=misFields" Branch" }
if ( $6 == "" ) { misFields=misFields" Bday" }
if ( $7 == "" ) { misFields=misFields" Salary" }
if ( $8 == "" ) { misFields=misFields" Age" }
if ( misFields != "" ) { print $1, $2, $4, "Missing Field/s:" misFields }
misFields=""
}
' myfile.txt
Test run with the data given in first post:
Hope this helps.
|
|
|
|
10-21-2009, 07:04 AM
|
#4
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
Code:
awk 'BEGIN{FS="|"}
NR==1{
for(o=1;o<=NF;o++){
a[o]=$o
}
next
}
{
missing="";f=0
for (i=1;i<=NF;i++){
if (!$i){
missing=missing" "a[i]
f=1
}
}
if(f){ print $1,$2,$4,missing}
}' file
|
|
|
10-21-2009, 07:10 AM
|
#5
|
LQ Veteran
Registered: Sep 2003
Posts: 10,532
|
@ghostdog74:
Nice solution, but......
1) It prints all the lines, not just the faulty ones,
2) The "Missing Field/s:" part is missing in your output.
|
|
|
10-21-2009, 07:41 AM
|
#6
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
Quote:
Originally Posted by druuna
1) It prints all the lines, not just the faulty ones,
2) The "Missing Field/s:" part is missing in your output.
|
1) is trivial to solve by rearranging where the print statement is.
2) i left the output un-formatted for the OP to do if he desires. What is important is the logic
Last edited by ghostdog74; 10-21-2009 at 07:43 AM.
|
|
|
All times are GMT -5. The time now is 10:58 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|