Question

script to create tilde seperate output file and the count

Asked by: yerruhari

I have an input file which contains the following data...

sno: 10
id: 15000
firstname: abcd
mail: abcd@mail.com
secondname: efgh
mail: efgh@mail.com

sno: 11
id: 15001
firstname: ijkl
mail: ijkl@mail.com
secondname: mnop
mail: mnop@mail.com

sno: 12
id: 15002
firstname: qrst
mail: qrst@mail.com
secondname:  uvwx
mail: uvwx@mail.com

sno: 13
id: 15003
firstname: mike
secondname: shane
mail: shane@mail.com

sno:14
id: 15004
secondname: donald
mail: donald@mail.com

sno: 15
id: 15001
firstname: abcd
mail: abcd@mail.com
secondname: efgh
mail: efgh@mail.com

sno: 16
id: 15001
firstname: ijkl
mail: ijkl@mail.com
secondname: mnop
mail: mnop@mail.com

sno: 17
id: 15002
firstname: abcd
mail: abcd@mail.com
secondname: efgh
mail: efgh@mail.com

sno: 18
id: 15003
firstname: ijkl
mail: ijkl@mail.com
secondname: mnop
mail: mnop@mail.com

sno: 19
id: 15004
firstname: abcd
mail: abcd@mail.com
secondname: efgh
mail: efgh@mail.com

sno: 20
id: 15006
firstname: ijkl
mail: ijkl@mail.com
secondname: mnop
mail: mnop@mail.com

sno: 21
id: 15004
firstname: abcd
mail: abcd@mail.com
secondname: efgh
mail: efgh@mail.com

sno: 22
id: 15004
firstname: abcd
mail: abcd@mail.com
secondname: efgh
mail: efgh@mail.com


I need an tilde seperated output file as follows....

10~15000~1~abcd~abcd@mail.com~efgh~efgh@mail.com
11~15001~3~ijkl~ijkl@mail.com~mnop~mnop@mail.com
12~15002~2~qrst~qrst@mail.com~uvwx~uvwx@mail.com
13~15003~2~mike~ ~shane~shane@mail.com
14~15004~4~ ~ ~don~don@mail.com
20~15006~1~ijkl~ijkl@mail.com~mnop~mnop@mail.com

Please note: If there is a firstname's mail id missing, as in the case of sno: 13, it should leave a blank space as shown in the output file.

Also in sno: 14 both the firstname and firstname's mail id are missing....so it should leave a blank spaces for both as shown in the output.

I don't have the option of distinguishing the firstname's mail id and secondname's mail id.........since both of them start with xmail:   so if the firstname's mail id is missing there is a chance that it takes second name's mail id in the output file.

Please try to provide me with a unix script for the above. I am using sun solaris. This is just sample data....i have data from sno: 1 to sno: 5000 in my input file. The firstname will be followed by firstname;s mail id and secondi name will be followed by secondname's maild id. Please make sure there will be no problems if there is any missing data like... firstname missing........secondname missing......secondname's mail id missing.....firstname's mail id missing......sno's missing and so on..............it should leave a blank space in the above cases.

Also i should get a count of haw many times the id is repeating as shown in the output file.for example, If the id is repeating 4 times as in the case of 15004. I should get that count and the remaining three entries can be left out. No matter what the other fileds (sno,firstname,mail,secondname,mail) may be. It should be uniqed by the id only and i should get that count.

Please do make it as soon as you can as it is pretty urgent

This question is in progress. Our experts are working on an answer right now.
Sign up for immediate access to the solution once it becomes available.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-11-07 at 13:10:24ID24880859
Topics

Shell Scripting

,

Scripting Languages

,

Unix Systems Programming

Participating Experts
2
Points
500
Comments
40

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. sftp bash shell script
    I am attempting to write a cron job that will move two files onto an sftp server, then create a directory with the date as its name, and move the files into the directory. So far I have the code below, but I am not familar with linux, cron jobs, or sftp so I am stuck. The cod...
  2. bash: read file into array
    Hi Experts, I need some help, I want to read a file and store each line as an item in the array. Thanks Hussain
  3. Robocopy exit codes
    I am automating the use of robocopy from the WIndows Server 2003 Resource Kit. I have been unable to find any information on the exit codes. From what I can tell, anything less than 8 is a success anything above is a failure. Does anyone have any more detailed information?
  4. Unattended Uninstall Script - command line equiv to Add/…
    Hey guys Got a problem. I need to create a script that automatically uninstalls ICS Viewer 6.0. I need help on how to tackle this. After some research, turns out PureEdge does not support quiet uninstall string via MSIEXEC (which is what I normally use - thru ConfigureSoft)...
  5. BASH shell script with Expect
    Hello all! I have been trying to put together a BASH script which uses Expect within the script. A parameter of another script gets passed to this BASH script, which then passes to the Expect script within. The problem is its not working. The parameter I beleive would get pas...
  6. PowerShell  Scripting - trying to eject a usb device
    Hello, I am new to Powershell Scripting - I have a server that uses a usb hard drive to back it up. Everyday I have to terminal to this device and go through the safely remove hardware process and then switch hard drives. I am trying to create a script that will do this fo...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: vinit_kainPosted on 2009-11-07 at 15:06:26ID: 25768511

Hi,

Change 'DataFile' to your actual filename and run this script

Vinit

#!/bin/ksh
awk -F\: '{
if($1~/sno/)
{
if(I>0)
{
print sno"~"id"~"firstname"~"mail"~"secondname"~"mail2
}
sno=$2
X=0
id=" "
firstname=" "
mail=" "
secondname=" "
mail2=" "
}
if($1~/id/) id=$2
if($1~/firstname/) firstname=$2
if(($1~/mail/)&&(X==0)) {mail=$2;X=1}
if($1~/secondname/) {secondname=$2;X=1}
if(($1~/mail/)&&(X==1))mail2=$2
I=1
}END { print sno"~"id"~"firstname"~"mail"~"secondname"~"mail2 }' DataFile|sort -n  +1 -t\~ |awk -F\~ ' BEGIN {I=1} {
if( F2 == $2 )
{
I=I+1
}
else
{
if(NR>1)
{
print F1"~"F2"~"I"~"F3"~"F4"~"F5"~"F6
}
F1=$1; F2=$2; F3=$3; F4=$4; F5=$5; F6=$6
I=1
}
} END { print F1"~"F2"~"I"~"F3"~"F4"~"F5"~"F6 }'
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:

Select allOpen in new window

 

by: yerruhariPosted on 2009-11-07 at 18:29:42ID: 25769176

Vinit can u please specify how to run the script.....

I created a file DataFile with the input data as shown above and ran the script as follows

./script DataFile

it's giving me the following errors

sort: open failed: +1: No such file or directory
~~1~~~~

can you please check on this

Thanks in advance

 

by: vinit_kainPosted on 2009-11-08 at 06:00:24ID: 25770474

Sorry about that. Can you please attached your script here(not the data file)

Regards
Vinit

 

by: yerruhariPosted on 2009-11-08 at 08:10:16ID: 25770852

I just copied your code into a file by name script and executed the script vinit. I got the above mentioned error. Any idea on y I got that error

 

by: vinit_kainPosted on 2009-11-08 at 08:24:33ID: 25770892

mv this file to scrv.doc to scrv.ksh and try to run

regards
vinit

 

by: yerruhariPosted on 2009-11-08 at 08:51:35ID: 25770970

Vinit i am getting the same error. Also i tried to keep the code until DataFile and tried executing the script. I am not getting the desired output. If second name's mail id is missing its taking firstname's mail id. Vinit is there any other means of communication i can reach you at as i don't have much time left to report this.

 

by: vinit_kainPosted on 2009-11-08 at 09:10:40ID: 25771017

as you already run upto first part, can you please copy few lines of output, which you think is incorrect.

save the outout and run
cat SavedFile|sort -n  +1 -t\~

let me know if you get another error on sort

I am here, and waiting for your reply
Vinit

 

by: yerruhariPosted on 2009-11-08 at 09:16:08ID: 25771039

sort: open failed: +1: No such file or directory

I am getting this error vinit.....

 

by: vinit_kainPosted on 2009-11-08 at 09:19:17ID: 25771050

I fixed the problem with second mail issue

check the code below

#!/bin/ksh
awk -F\: '{
if($1~/sno/)
{
if(I>0)
{
print sno"~"id"~"firstname"~"mail"~"secondname"~"mail2
}
sno=$2
X=0
id=" "
firstname=" "
mail=" "
secondname=" "
mail2=" "
}
if($1~/id/) id=$2
if($1~/firstname/) firstname=$2
if(($1~/mail/)&&(X==0)) {mail=$2;X=1;YY=NR}
if($1~/secondname/) {secondname=$2;X=1}
if(($1~/mail/)&&(X==1)&&(NR != YY))mail2=$2
I=1
}END { print sno"~"id"~"firstname"~"mail"~"secondname"~"mail2 }' DataFile
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:

Select allOpen in new window

 

by: yerruhariPosted on 2009-11-08 at 09:27:37ID: 25771080

vinit.........if the sno is missing then its not printing the whole line........for example if sno's 12 and 13 are missing its not printing the whole line......in such case the data will be missing

 

by: vinit_kainPosted on 2009-11-08 at 09:28:01ID: 25771085


change the sort command to 'sort -t\~ -k2 -n' and check if you still get the error

 

by: yerruhariPosted on 2009-11-08 at 09:34:24ID: 25771113

cat save|sort -t\~ -k2 -n

when i did this on saved file....its not giving any errors....

 

by: vinit_kainPosted on 2009-11-08 at 09:35:18ID: 25771118

instead of sno, I changed the mandatory field as id now. Please verify

#!/bin/ksh
awk -F\: '{
if($1~/sno/) newsno=$2
if($1~/id/)
{
if(I>0)
{
print sno"~"id"~"firstname"~"mail"~"secondname"~"mail2
X=0
id=" "
firstname=" "
mail=" "
secondname=" "
mail2=" "
}
id=$2
sno=newsno
}
if($1~/firstname/) firstname=$2
if(($1~/mail/)&&(X==0)) {mail=$2;X=1;YY=NR}
if($1~/secondname/) {secondname=$2;X=1}
if(($1~/mail/)&&(X==1)&&(NR != YY))mail2=$2
I=1
}END { print sno"~"id"~"firstname"~"mail"~"secondname"~"mail2 }' DataFile
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:

Select allOpen in new window

 

by: vinit_kainPosted on 2009-11-08 at 09:37:28ID: 25771127

That is good,

Check the complete script below

Vinit

#!/bin/ksh
awk -F\: '{
if($1~/sno/) newsno=$2
if($1~/id/)
{
if(I>0)
{
print sno"~"id"~"firstname"~"mail"~"secondname"~"mail2
X=0
id=" "
firstname=" "
mail=" "
secondname=" "
mail2=" "
}
id=$2
sno=newsno
}
if($1~/firstname/) firstname=$2
if(($1~/mail/)&&(X==0)) {mail=$2;X=1;YY=NR}
if($1~/secondname/) {secondname=$2;X=1}
if(($1~/mail/)&&(X==1)&&(NR != YY))mail2=$2
I=1
}END { print sno"~"id"~"firstname"~"mail"~"secondname"~"mail2 }' DataFile |sort -t\~ -k2 -n |awk -F\~ ' BEGIN {I=1} {
if( F2 == $2 )
{
I=I+1
}
else
{
if(NR>1)
{
print F1"~"F2"~"I"~"F3"~"F4"~"F5"~"F6
}
F1=$1; F2=$2; F3=$3; F4=$4; F5=$5; F6=$6
I=1
}
} END { print F1"~"F2"~"I"~"F3"~"F4"~"F5"~"F6 }'
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:

Select allOpen in new window

 

by: yerruhariPosted on 2009-11-08 at 09:48:29ID: 25771169

vinit if a sno is missing it's taking the previous sno now...........when there are sno's 11 to 15 missing.......for all of them its taking 10 as the sno.....can you check this and in the output file i am getting this as my first line

~~2~~~~

is it taking count for spaces or what?

 

by: yerruhariPosted on 2009-11-08 at 09:50:41ID: 25771179

can we do the code without keeping anything mandatory........because there are more than 5000 records.....what if id goes missing somewhere .....we will not know what is missing......

 

by: vinit_kainPosted on 2009-11-08 at 09:54:15ID: 25771191

Old serial no. fixed.

about that first line, can you run the script upto sort and let me know the top three  lines

Vinit

#!/bin/ksh
awk -F\: '{
if($1~/sno/) newsno=$2
if($1~/id/)
{
if(I>0)
{
print sno"~"id"~"firstname"~"mail"~"secondname"~"mail2
X=0
sno=" "
id=" "
firstname=" "
mail=" "
secondname=" "
mail2=" "
}
id=$2
sno=newsno
}
if($1~/firstname/) firstname=$2
if(($1~/mail/)&&(X==0)) {mail=$2;X=1;YY=NR}
if($1~/secondname/) {secondname=$2;X=1}
if(($1~/mail/)&&(X==1)&&(NR != YY))mail2=$2
I=1
}END { print sno"~"id"~"firstname"~"mail"~"secondname"~"mail2 }' DataFile |sort -t\~ -k2 -n |awk -F\~ ' BEGIN {I=1} {
if( F2 == $2 )
{
I=I+1
}
else
{
if(NR>1)
{
print F1"~"F2"~"I"~"F3"~"F4"~"F5"~"F6
}
F1=$1; F2=$2; F3=$3; F4=$4; F5=$5; F6=$6
I=1
}
} END { print F1"~"F2"~"I"~"F3"~"F4"~"F5"~"F6 }'
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:

Select allOpen in new window

 

by: vinit_kainPosted on 2009-11-08 at 09:58:18ID: 25771202

I do not know about the data. How can I assume that new record started if there is no field is mandatory.

can I assume blank row as record separator?

Vinit

 

by: yerruhariPosted on 2009-11-08 at 10:00:15ID: 25771206

This is the first line i am getting
~~~~~


 

by: yerruhariPosted on 2009-11-08 at 10:02:02ID: 25771209

yes vinit we can assume blank row as a record seperator...........as there will definitely be a blank row after each record in my data.

 

by: vinit_kainPosted on 2009-11-08 at 10:03:54ID: 25771217

can you please remove blank lines from the top(not all). The first record shouldn't be blank

 

by: yerruhariPosted on 2009-11-08 at 10:08:23ID: 25771229

remove the blank lines from the top.....i did not get you vinit.......can you make that clear........thank you very much for your time vinit...........i really appreciate it.......r u from USA or?

 

by: vinit_kainPosted on 2009-11-08 at 10:19:49ID: 25771269

Hi,

Changed the code to blank line as data seperator
 Fix the first line issue.

Check now
Vinit

#!/bin/ksh
awk -F\: '{
if(NF==0)
{
if(I>0)
{
print sno"~"id"~"firstname"~"mail"~"secondname"~"mail2
}
X=0
sno=" "
id=" "
firstname=" "
mail=" "
secondname=" "
mail2=" "
I=0
}
if($1~/sno/){sno=$2;I=1}
if($1~/id/){id=$2;I=1}
if($1~/firstname/){firstname=$2;I=1}
if(($1~/mail/)&&(X==0)){mail=$2;X=1;YY=NR;I=1}
if($1~/secondname/) {secondname=$2;X=1;I=1}
if(($1~/mail/)&&(X==1)&&(NR != YY)){mail2=$2;I=1}
}END { print sno"~"id"~"firstname"~"mail"~"secondname"~"mail2 }' DataFile |sort -t\~ -k2 -n |awk -F\~ ' BEGIN {I=1} {
if( F2 == $2 )
{
I=I+1
}
else
{
if(NR>1)
{
print F1"~"F2"~"I"~"F3"~"F4"~"F5"~"F6
}
F1=$1; F2=$2; F3=$3; F4=$4; F5=$5; F6=$6
I=1
}
} END { print F1"~"F2"~"I"~"F3"~"F4"~"F5"~"F6 }'
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:

Select allOpen in new window

 

by: yerruhariPosted on 2009-11-08 at 10:34:14ID: 25771314

working great vinit.....vinit if i want to insert this code into another script can i do that.......I will be getting the DataFile output when i run a script...if i can insert how to insert it......

 

by: vinit_kainPosted on 2009-11-08 at 10:42:22ID: 25771341

Yes you can insert it any script.
 add all lines except (#!/bin/ksh) in other script.

assume 'cat xfile' command generating the output same as DataFile file


then change your script as below, remove name of the file from awk and piped the output of previous command to awk.

Vinit

'cat xfile|awk -F\: '{
if(NF==0)
{
if(I>0)
{
print sno"~"id"~"firstname"~"mail"~"secondname"~"mail2
}
X=0
sno=" "
id=" "
firstname=" "
mail=" "
secondname=" "
mail2=" "
I=0
}
if($1~/sno/){sno=$2;I=1}
if($1~/id/){id=$2;I=1}
if($1~/firstname/){firstname=$2;I=1}
if(($1~/mail/)&&(X==0)){mail=$2;X=1;YY=NR;I=1}
if($1~/secondname/) {secondname=$2;X=1;I=1}
if(($1~/mail/)&&(X==1)&&(NR != YY)){mail2=$2;I=1}
}END { print sno"~"id"~"firstname"~"mail"~"secondname"~"mail2 }' |sort -t\~ -k2 -n |awk -F\~ ' BEGIN {I=1} {
if( F2 == $2 )
{
I=I+1
}
else
{
if(NR>1)
{
print F1"~"F2"~"I"~"F3"~"F4"~"F5"~"F6
}
F1=$1; F2=$2; F3=$3; F4=$4; F5=$5; F6=$6
I=1
}
} END { print F1"~"F2"~"I"~"F3"~"F4"~"F5"~"F6 }'

 

by: yerruhariPosted on 2009-11-08 at 10:59:34ID: 25771424

Thanks a lot dude........vinit i need to automate this script.........i need to run this script probably every night and i need to run this against four different files generated that day.......for every four hours the files will be rotated.........so  i need to concatenate these four files together........doing so.........there will be some duplicates......for example

id 15001 is in all the four files ......in the first file it came twice ......second file it came thrice........third one four times and fourth file one time...then the count should become 10. i asked a question on this before.....u gave the answer also.

can you write me a loop for that....

it should go into a directory ......search for that files generated that day and the script should run against each file......the output from all the four files must be clubbed together into one file and i should get the count.........can we inculde that in this script or should it be another one????

 

by: vinit_kainPosted on 2009-11-08 at 11:13:28ID: 25771470

Use file and cat as below and attach the script

Replace the Directory and file name in below script. it will run on all the four files.

I can give you right 'find', if you give me the name of data files

Vinit


find /FullDirectoryPath -name "DataFile*" -mtime -1 -exec cat {} |awk -F\: '{
if(NF==0)
{
if(I>0)
{
print sno"~"id"~"firstname"~"mail"~"secondname"~"mail2
}
X=0
sno=" "
id=" "
firstname=" "
mail=" "
secondname=" "
mail2=" "
I=0
}
if($1~/sno/){sno=$2;I=1}
if($1~/id/){id=$2;I=1}
if($1~/firstname/){firstname=$2;I=1}
if(($1~/mail/)&&(X==0)){mail=$2;X=1;YY=NR;I=1}
if($1~/secondname/) {secondname=$2;X=1;I=1}
if(($1~/mail/)&&(X==1)&&(NR != YY)){mail2=$2;I=1}
}END { print sno"~"id"~"firstname"~"mail"~"secondname"~"mail2 }' |sort -t\~ -k2 -n |awk -F\~ ' BEGIN {I=1} {
if( F2 == $2 )
{
I=I+1
}
else
{
if(NR>1)
{
print F1"~"F2"~"I"~"F3"~"F4"~"F5"~"F6
}
F1=$1; F2=$2; F3=$3; F4=$4; F5=$5; F6=$6
I=1
}
} END { print F1"~"F2"~"I"~"F3"~"F4"~"F5"~"F6 }'

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:

Select allOpen in new window

 

by: yerruhariPosted on 2009-11-08 at 11:25:49ID: 25771506

Vinit the output for DataFile is obatained by running another script against the four files for that day.......so can do need to call out the four files in another script?

 

by: yerruhariPosted on 2009-11-08 at 11:36:35ID: 25771555

The output for the DataFile will be obatined by running it against one of the four files for that day..........so can we do it this way.........write another script to find the four files from the directory and run agianst each file.....concatenate the data and get the count as mentioned. do we need to write a loop of just use find exec command?

 

by: vinit_kainPosted on 2009-11-08 at 11:37:45ID: 25771559

what I understood

You have four files

one script generate the output as DataFile for each file.

You can change the above script, call four times (run in a loop )

for file in `File1 File2 File3 File 4`
do
{
Script1
}
done|  Script2 ( What I created (without find))

Vinit

 

by: vinit_kainPosted on 2009-11-08 at 11:43:56ID: 25771586

If you get 4 DataFile ( data as attached in question)
then concatenate  this four files and run script. no need to loop the script.

Vinit

 

by: WanderJahrePosted on 2009-11-08 at 12:21:16ID: 25771718

Hi yerruhari

The following bash script should meet your requirements. Just run it from the command line. For example:

bash ./script_for_tilde_separated_output.sh < data_for_tilde_separated_output_file.txt

Note that there is a slight discrepancy in the output from my script since I am using your input data where sno:14 has a second name of 'donald' not 'don' and also has a correspondingly shortened email. Otherwise my output matches your sample output.

Hope this helps.

#!/bin/bash
fields=(sno id firstname firstmail secondname secondmail)
declare -a arry idlist idcount
reset_keys () {
  for r in ${fields[*]}; do
    eval "$r=\ "
  done
}
add_record () {
  if [[ "${idlist[$id]}" ]]; then
    idcount[$id]=$((${idcount[$id]} + 1))
  else
    idlist[$id]=1
    idcount[$id]=1
    arry[${#arry[*]}]="$id~$sno~$id~COUNT~$firstname~$firstmail~$secondname~$secondmail" 
  fi
  reset_keys
}
reset_keys
while read rec; do
  key=${rec%:*}
  val=${rec#*:}
  val=${val// /}
  if [[ "$rec" =~ ^[:blank:]*$ ]]; then
    [[ "${#id}" -gt 1 ]] && add_record
  else
    if [[ "$key" == "mail" ]]; then
      if [[ "${#secondname}" -gt 1 ]]; then
        key=secondmail
      else
        key=firstmail
      fi
    fi
    eval "$key='$val'"
  fi
done 
[[ "${#id}" -gt 1 ]] && add_record
for ((i=0; i<${#arry[*]};i++)); do
  line=${arry[i]}
  id=${line%%~*}
  line=${line#*~}
  count=${idcount[$id]}
  line=${line/COUNT/$count}
  echo $line
done

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:

Select allOpen in new window

 

by: yerruhariPosted on 2009-11-08 at 12:45:59ID: 25771795

Vinit.........

I have a total of 5 servers for which i need to run the script

for 3 servers then files are at /home/data/Datafiles

for other two servers the files are at different location /home/records/Datafiles

can you write a loop for this dude with the above mentioned requirement.......

 

by: yerruhariPosted on 2009-11-08 at 12:53:40ID: 25771835

vinit........For the code which you wrote..........can you write explanation comments if you don't mind...........

 

by: vinit_kainPosted on 2009-11-10 at 01:27:34ID: 25783720

Yerruhari,

Sorry for late reply, I was away.

You have 5 servers, does it means 5 files or 5*4 files?

Vinit

 

by: yerruhariPosted on 2009-11-10 at 19:17:54ID: 25792080

Vinit,

I need to have something like this

for server in `01 02 03 04 05`
do
   for file in `file1 file2 file3 file4`
   do
   script1
done
done

But i have some concerns here.....

for server 01,02,03 the files are at /home/data/Datafiles

for server 04 and 05 the files are at /home/records/Datafiles

I need to automate the script so that it gets only the files within last 24 hours. so first i need to search for the files in the directories concerned to their respective server and run the script1 for each and every file and redirect the whole output to a file. can you give me the correct code to do this. Also note that the script1 will be at following path /home/scripts/script1.

Thanks in advance....can u do that as soon as you can....if possible...can u look at my other question too....I really appreciate your help vinit...

 

by: yerruhariPosted on 2009-11-10 at 19:20:40ID: 25792090

Also can you explain the previous script you wrote with comments if possible......Thanks a lot in advance

 

by: vinit_kainPosted on 2009-11-10 at 19:56:44ID: 25792203

To connect to different servers you need to add login information as well

for server in "01 02 03 04 05"
do
{
if [ server=="01" -o server=="02" -o server=="03" ]
then
{
for file in `find /home/data/Datafiles -name "file[1-4]" -type f -mtime -1`
do
{

script to run

}
done
}
else
{
for file in `find /home/records/Datafiles -name "file[1-4]" -type f -mtime -1`
do
{

script to run

}
done
}
fi
}
done

 

by: yerruhariPosted on 2009-11-10 at 23:37:44ID: 25792964

Vinit,

Also please do take into consideration that i need to run the script against all the four files...and not a single file......concatenate the data into one file and from this file i should get the count based on the field 3 and field 4 with the following code you provided. can you let me know where in this script i should include this and give me some final code.

awk -F\, '{ if(F3==$3) { F4=F4+$4 } else { if(NR>1) { print F1","F2","F3","F4","F5","F6 } F1=$1;F2=$2;F3=$3;F4=$4;F5=$5;F6=$6 }}  END {print F1","F2","F3","F4","F5","F6 }' File1

File1 here is the concatenated output file mentioned above

 

by: vinit_kainPosted on 2009-11-11 at 00:15:18ID: 25793101



I have attached the script with loop for all the servers and all the files.

Vinit

for server in "01 02 03 04 05"
do
{
if [ server=="01" -o server=="02" -o server=="03" ]
then
{
find /home/data/Datafiles -name "file[1-4]" -type f -mtime -1 -exec cat {} \;|awk -F\: '{
if(NF==0)
{
if(I>0)
{
print sno"~"id"~"firstname"~"mail"~"secondname"~"mail2
}
X=0
sno=" "
id=" "
firstname=" "
mail=" "
secondname=" "
mail2=" "
I=0
}
if($1~/sno/){sno=$2;I=1}
if($1~/id/){id=$2;I=1}
if($1~/firstname/){firstname=$2;I=1}
if(($1~/mail/)&&(X==0)){mail=$2;X=1;YY=NR;I=1}
if($1~/secondname/) {secondname=$2;X=1;I=1}
if(($1~/mail/)&&(X==1)&&(NR != YY)){mail2=$2;I=1}
}END { print sno"~"id"~"firstname"~"mail"~"secondname"~"mail2 }' |sort -t\~ -k2 -n |awk -F\~ ' BEGIN {I=1} {
if( F2 == $2 )
{
I=I+1
}
else
{
if(NR>1)
{
print F1"~"F2"~"I"~"F3"~"F4"~"F5"~"F6
}
F1=$1; F2=$2; F3=$3; F4=$4; F5=$5; F6=$6
I=1
}
} END { print F1"~"F2"~"I"~"F3"~"F4"~"F5"~"F6 }'
 
}
else
{
find /home/records/Datafiles -name "file[1-4]" -type f -mtime -1 -exec cat {} \;|awk -F\: '{
if(NF==0)
{
if(I>0)
{
print sno"~"id"~"firstname"~"mail"~"secondname"~"mail2
}
X=0
sno=" "
id=" "
firstname=" "
mail=" "
secondname=" "
mail2=" "
I=0
}
if($1~/sno/){sno=$2;I=1}
if($1~/id/){id=$2;I=1}
if($1~/firstname/){firstname=$2;I=1}
if(($1~/mail/)&&(X==0)){mail=$2;X=1;YY=NR;I=1}
if($1~/secondname/) {secondname=$2;X=1;I=1}
if(($1~/mail/)&&(X==1)&&(NR != YY)){mail2=$2;I=1}
}END { print sno"~"id"~"firstname"~"mail"~"secondname"~"mail2 }' |sort -t\~ -k2 -n |awk -F\~ ' BEGIN {I=1} {
if( F2 == $2 )
{
I=I+1
}
else
{
if(NR>1)
{
print F1"~"F2"~"I"~"F3"~"F4"~"F5"~"F6
}
F1=$1; F2=$2; F3=$3; F4=$4; F5=$5; F6=$6
I=1
}
} END { print F1"~"F2"~"I"~"F3"~"F4"~"F5"~"F6 }'
 
}
fi
}
done

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:

Select allOpen in new window

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...