LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 07-16-2014, 08:47 AM   #1
davidcummings
LQ Newbie
 
Registered: Jul 2014
Posts: 3

Rep: Reputation: Disabled
Script help required


Hi,
I am new to the Linux scene and I am using a raspberry Pi to log air traffic radar. I was wondering if someone could help me with a bit of script code?
Code:
 
#!/bin/bash
/bin/mv /root/dump1090/radar.txt /root/logs/in/radar.txt
/bin/touch /root/dump1090/radar.txt
/bin/awk '!_[$1]++' /root/logs/in/radar.txt > /root/logs/in/processed.txt
/bin/rm /root/logs/in/radar.txt
/bin/mv /root/logs/in/processed.txt /root/logs/out/"radar-`date +%Y%m%d`.txt"
/sbin/restart
my problem is here :
Code:
/bin/awk '!_[$1]++' /root/logs/in/radar.txt > /root/logs/in/processed.txt
what I want it to do is go through the radar.txt file, remove any duplicates and save out the result as processed.txt

before i try and run it is the above syntax correct?

I would also like the file to have a date in the format of yyyy-mm-dd added to the end of each line after a comma:

current:
gf621a
83hfs8

wanted:
gf621a,2014-07-16
83hfs8,2014-07-16

and the file saved out as CSV. is this possible?

Last edited by davidcummings; 07-16-2014 at 08:51 AM. Reason: extra detail
 
Old 07-16-2014, 03:13 PM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by davidcummings View Post
I am new to the Linux scene and I am using a raspberry Pi to log air traffic radar.
Welcome to LQ! You may wish to ask that this thread be moved to the Programming forum for some better exposure; however moderators may also make that determination on their own. Either case, people will still see it so don't be too overly concerned.

Quote:
Originally Posted by davidcummings View Post
Code:
/bin/awk '!_[$1]++' /root/logs/in/radar.txt > /root/logs/in/processed.txt
what I want it to do is go through the radar.txt file, remove any duplicates and save out the result as processed.txt

before i try and run it is the above syntax correct?
Correct by my reckoning. I'm not too familiar with awk; however I created a text file containing the following:
Code:
abc
abc
123
456
I then ran your awk command against it and the result was one of the duplicate "abc" lines was removed. Quick Edit Update: To go further, I thought since me not knowing awk too well that I'd also add a second duplicate, and therefore added another line "123" and it removed both duplicates. So I feel the command as you have it scripted will work properly.

Quote:
Originally Posted by davidcummings View Post
I would also like the file to have a date in the format of yyyy-mm-dd added to the end of each line after a comma:
I believe the format you chose is fine, you may also use:
Code:
date +%F
And if you wish a COMMA versus a DASH, then you need to change "radar-" to say "radar,". Personally I prefer the DASH because I think COMMA in a file name is not too great.

Quote:
Originally Posted by davidcummings View Post
and the file saved out as CSV. is this possible?
The files are in ... some form of record type? You have performed an awk call to remove duplicate entries, similarly you can use awk, sed, or something to edit entries to place them in CSV form. I would then instead of using a .txt extension, use a .csv extension. The matter there is how difficult it is, but as far as processing a record, you can do this. If you show an illustration of a record and the desired way you'd change it to be comma separated, then people can suggest a command form which will do this.

Last edited by rtmistler; 07-16-2014 at 03:16 PM.
 
Old 07-16-2014, 05:29 PM   #3
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
One suggestion, from a maintainability point of view, is to drop all of the hard coded path names and use variables instead. For example:
Code:
#!/bin/bash
/bin/mv /root/dump1090/radar.txt /root/logs/in/radar.txt
/bin/touch /root/dump1090/radar.txt
/bin/awk '!_[$1]++' /root/logs/in/radar.txt > /root/logs/in/processed.txt
/bin/rm /root/logs/in/radar.txt
/bin/mv /root/logs/in/processed.txt /root/logs/out/"radar-`date +%Y%m%d`.txt"
/sbin/restart
becomes
Code:
#!/bin/bash
start_dir=/root/dump1090
in_dir=/root/logs/in
out_dir=/root/logs/out

/bin/mv ${start_dir}/radar.txt ${in_dir}/radar.txt
/bin/touch ${start_dir}/radar.txt
/bin/awk '!_[$1]++' ${in_dir}/radar.txt > ${in_dir}/processed.txt
/bin/rm ${in_dir}/radar.txt
/bin/mv ${in_dir}/processed.txt ${out_dir}/"radar-`date +%Y%m%d`.txt"
/sbin/restart
That way in the future, when you want to move something (and you probably will), you can do it in one place instead of five.

Also, you're using a lot of intermediate files, and I'm not really seeing why. Couldn't you just do:
Code:
#!/bin/bash
start_dir=/root/dump1090
out_dir=/root/logs/out

/bin/awk '!_[$1]++' ${start_dir}/radar.txt > ${out_dir}/"radar-`date +%Y%m%d`.txt"
/bin/rm ${start_dir}/radar.txt
/bin/touch ${start_dir}/radar.txt
/sbin/restart
Also, you're using a lot of absolute path names to standard executables. Are you planning on running this script through a cron job? Even then, /bin is usually in the default cron PATH and can be left off. Or if you're worried about it, you can modify the PATH directly in the script:
Code:
#!/bin/bash
export PATH=$PATH:/bin:/sbin
start_dir=/root/dump1090
out_dir=/root/logs/out

awk '!_[$1]++' ${start_dir}/radar.txt > ${out_dir}/"radar-`date +%Y%m%d`.txt"
rm ${start_dir}/radar.txt
touch ${start_dir}/radar.txt
restart

Last edited by suicidaleggroll; 07-16-2014 at 05:31 PM.
 
Old 07-17-2014, 02:02 AM   #4
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,475

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
Also take a look at
Code:
man uniq
for dropping duplicates in files.
Using this as a test file:
Code:
abc
abc
123
456
abc
Code:
uniq test.txt

abc
123
456
abc
Code:
uniq -u test.txt

123
456
abc
However the more you learn about awk the better!
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Required Script or steps dhirendrs Programming 3 11-03-2010 04:12 AM
Script required dhirendrs Programming 4 08-11-2010 09:54 AM
Monitoring Script required nimish Linux - Software 0 11-05-2004 03:25 AM
basic script help required jimmorrison Linux - General 5 10-26-2004 11:31 PM
New Firewall Script Required smurf Linux - Networking 1 06-10-2004 07:29 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:51 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