LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 10-21-2009, 03:39 AM   #1
srimal
LQ Newbie
 
Registered: Oct 2009
Posts: 5

Rep: Reputation: 0
Question 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 !!!!!!!!!!!!!!!
 
Old 10-21-2009, 05:29 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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.
 
Old 10-21-2009, 06:47 AM   #3
srimal
LQ Newbie
 
Registered: Oct 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Thumbs up Thank you

Thank you very much....for your great help.






Quote:
Originally Posted by druuna View Post
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.
 
Old 10-21-2009, 07:04 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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
 
Old 10-21-2009, 07:10 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
@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.
 
Old 10-21-2009, 07:41 AM   #6
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by druuna View Post
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.
 
  


Reply



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 read data from file to use in shell script? ozymandias Linux - Newbie 7 10-27-2006 01:19 PM
How to read a single line from a text file into a shell script. SkipHuffman Linux - Software 2 08-16-2006 02:10 PM
Read specific lines from a text file chobin Programming 8 06-14-2006 11:14 AM
bash script that can read lines of text palceksmuk Programming 1 12-25-2005 03:49 AM
How to filter lines from a text file Rascale Linux - Software 1 10-28-2003 12:46 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:16 PM.

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