Scripting issue


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Scripting issue
# 1  
Old 09-18-2009
Scripting issue

Hello,

I have an ASCII file (many files of the same format, but different dates and numbers) in the format like below:

2008.01.02,08:00,1.46520,1.46520,1.46410,1.46440,70
2008.01.02,08:05,1.46450,1.46560,1.46440,1.46540,79
2008.01.02,08:10,1.46530,1.46540,1.46490,1.46500,46
2008.01.02,08:15,1.46490,1.46500,1.46470,1.46500,43
2008.01.02,08:20,1.46510,1.46600,1.46500,1.46580,44
. . .

Here 2008 - is a year. Other lines might start with year 1999 or 2004, etc.

Need to convert it to the following style:

2008-01-02 08:00 1,46520 1,46520 1,46410 1,46440 70
2008-01-02 08:05 1,46450 1,46560 1,46440 1,46540 79
2008-01-02 08:10 1,46530 1,46540 1,46490 1,46500 46
2008-01-02 08:15 1,46490 1,46500 1,46470 1,46500 43
2008-01-02 08:20 1,46510 1,46600 1,46500 1,46580 44
. . .


After that need to split the new file according to a year (and move each one to its own directory - "2008" or "1999", "2004" and so on).

I have Ubuntu installed, so perl, awk or whatever should be there (unfortunately I'm not familiar with how to programming them). Maybe anybody can help with this task.


Thank you!
# 2  
Old 09-19-2009
This is a start up
Code:
awk -F, '{gsub(/\./,"-",$1);gsub(/\./,",");print > substr($0,1,4)}' file

# 3  
Old 09-19-2009
Thank you, danmero!

The conversion is excellent, just all new files need to have the same =original)
filename, but each of them need to be located in directory according to a year (the directories will be created in the first run and following runs will use the same directories or we can assume that directories already exist).

For Example: filename.txt ===>>>
1999/filename.txt
2000/filename.txt
2001/filename.txt
2002/filename.txt
. . .

Maybe you know how to do this?


Thank you very much!




Quote:
Originally Posted by danmero
This is a start up
Code:
awk -F, '{gsub(/\./,"-",$1);gsub(/\./,",");print > substr($0,1,4)}' file

# 4  
Old 09-19-2009
I hope it helps...
Code:
 
cut -d"." -f1 < filename.txt | sort -u | while read year
do
mkdir ${year}
cd ${year}
awk -F, -v yr="${year}" '{if(yr==substr($0,1,4)){gsub(/\./,"-",$1);
gsub(/\./,",");print > "filename.txt"}}' /location/of/filename.txt
cd ..
done

# 5  
Old 09-19-2009
This works fine!
Thank you for your help!




Quote:
Originally Posted by malcomex999
I hope it helps...
Code:
 
cut -d"." -f1 < filename.txt | sort -u | while read year
do
mkdir ${year}
cd ${year}
awk -F, -v yr="${year}" '{if(yr==substr($0,1,4)){gsub(/\./,"-",$1);
gsub(/\./,",");print > "filename.txt"}}' /location/of/filename.txt
cd ..
done

[COLOR="#738fbf"]

---------- Post updated at 01:03 PM ---------- Previous update was at 12:58 PM ----------

One more question - I copied the previously mentioned code into a file.
- Is it possible to create a variable from "filename.txt", so each time (for every new file) I will need to change it only in one place? (currently it is mentioned 3 times)

Thank you!




Code:
 
cut -d"." -f1 < filename.txt | sort -u | while read year
do
mkdir ${year}
cd ${year}
awk -F, -v yr="${year}" '{if(yr==substr($0,1,4)){gsub(/\./,"-",$1);
gsub(/\./,",");print > "filename.txt"}}' /location/of/filename.txt
cd ..
done

# 6  
Old 09-20-2009
perl:

Code:
my %hash;
open FH,"<a.txt";
while(<FH>){
  chomp;
  my @tmp = split(",",$_);
  my @tmp1= split("[.]",$_,2);
  my $year=$tmp1[0];
  $tmp[0]=~s/[.]/-/g;
  map {s/[.]/,/;} @tmp[2..5];
  my $str=$tmp[0]." ".$tmp[1]." ".$tmp[2]." ".$tmp[3]." ".$tmp[4]." ".$tmp[5]." ".$tmp[6];
  push @{$hash{$year}}, $str;
}
foreach my $key(sort keys %hash){
  my $file="file_".$key.".txt";
  open TFH,">$file";
  local $,="\n";
  print TFH @{$hash{$key}};
  close TFH;
}

# 7  
Old 09-21-2009
Hello summer_cherry,

The files are generated correctly, just need to give them the name of the
original file and create them in a "year"-directory. For Eample:

Quote:
ABCD.txt ->
1999/ABCD.txt
2003/ABCD.txt
. . .
Also, is it possible to specify the name of the file only when running the script? - i.e.

Quote:
> perl_script.pl ABCD.txt

Thank you!




Quote:
Originally Posted by summer_cherry
perl:

Code:
my %hash;
open FH,"<a.txt";
while(<FH>){
  chomp;
  my @tmp = split(",",$_);
  my @tmp1= split("[.]",$_,2);
  my $year=$tmp1[0];
  $tmp[0]=~s/[.]/-/g;
  map {s/[.]/,/;} @tmp[2..5];
  my $str=$tmp[0]." ".$tmp[1]." ".$tmp[2]." ".$tmp[3]." ".$tmp[4]." ".$tmp[5]." ".$tmp[6];
  push @{$hash{$year}}, $str;
}
foreach my $key(sort keys %hash){
  my $file="file_".$key.".txt";
  open TFH,">$file";
  local $,="\n";
  print TFH @{$hash{$key}};
  close TFH;
}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Scripting issue

Hi Experts, I'm stuck with a single liner bash script. Need your help to fix it. Let me brief you that what is my expectations from this script. I need the interfaces configured for a list of servers, their respective IP address and FQDN in a output file. The output should be in the same... (3 Replies)
Discussion started by: arun_adm
3 Replies

2. Shell Programming and Scripting

Scripting Issue

Hi All, I am facing a problem when i am trying to run shell script more than 5 times. I have shell script(.sh file) which ran perfectly fine in the early attempts(1,2,3,4 runs). But if i try to run the script more number of times, i am facing the below error message. Too many ('s I do... (3 Replies)
Discussion started by: am24
3 Replies

3. Shell Programming and Scripting

Scripting issue

I am having a problem. which I have described below>> I have to run a script with the format : <File_name><Start_date><End_date> abcd.sh 19-JAN-2015 01-May-2014 problem is I need to compare these two dates and throw an error as start date must be less than or equal to end date. But... (13 Replies)
Discussion started by: Chandan_Bose
13 Replies

4. Shell Programming and Scripting

Scripting Issue

needing this script to shut down 1 IceS and start up another, close the 2nd one after 12 seconds and then reboot. here is what i have so far #!/bin/bash ShutDown() { echo "Shutdown in progress." wall <<ENDOFWALL CI Shutdown has been intiated!!!! Shutdown will occur in 30 seconds...... (1 Reply)
Discussion started by: Zaine
1 Replies

5. Shell Programming and Scripting

Scripting Issue

I am having an issue with a script that I created today, my first attempt at this, and was wondering if anyone can give me insight to what changes need to be made. Below is a copy of the script that I have written. WEe are trying to monitor whether or not a services is running. I do have a cron... (1 Reply)
Discussion started by: lsudubee
1 Replies

6. Shell Programming and Scripting

scripting issue

Hello, Guys I am having a sql script file which contains some sql statements including inserting values, One column is of the data type date. Now i am having a KSH script for inserting values via this script into the database. The problem I am facing that when I am inserting value in the... (1 Reply)
Discussion started by: mraghunandanan
1 Replies

7. Shell Programming and Scripting

IP-Scripting Issue

Hi Guys, I am quite new to Shell Scripting... I need ur help.. This is very urgent. The thing is like, I need to match a IP address (ex 192.168.200.56) i.e, xxx.xxx.xxx.xx inside a KSH script,but if we enter in different format other than the specified format (ex jjj.ksj., 1.0...), it should... (3 Replies)
Discussion started by: mraghunandanan
3 Replies

8. Shell Programming and Scripting

Scripting issue..

Hi guys... I am newbie to Shell Scripting... I am querying the Oracle database.I want a Shell script for fetching some data from datasets in Oracle database and displaying in an excel sheet.This is my requirement.. Can u plz help me guys.. Regards, Mahesh... (4 Replies)
Discussion started by: mraghunandanan
4 Replies

9. Shell Programming and Scripting

Scripting issue..

Hi guys... I am a newbie to scripting... I have a small requirement... I dont whether u r clear with my requirement... But plz try to help me... Like, tell me some kind of scripts that can help me in retreiving the data from the datasets. Regards, Mahesh... (1 Reply)
Discussion started by: mraghunandanan
1 Replies

10. Shell Programming and Scripting

scripting issue

folks; i'm trying to write a shell script to do the following: 1. i have a file with 39 fields, i'm trying to add 10 more fields with (!) as a field separator. with the following requirement: if field number 20 has a value, then field number 40 will show as (!M!), and if the field number 20 does... (2 Replies)
Discussion started by: moe2266
2 Replies
Login or Register to Ask a Question