LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 05-05-2009, 09:10 AM   #1
briana.paige
LQ Newbie
 
Registered: May 2009
Location: Canada
Posts: 14

Rep: Reputation: 0
AWK/Perl for extracting data from txt file to numerous other files


Hi,

I'm new to both Perl and AWK. I have a text file with tens of thousands of lines arranged by year 1953-2005.

The first line, first field is 'CAN14A2005010101'.

If I use:

awk '/CAN14A2005/' ottawa.txt > ottawa2005.txt

in my cygwin bash window,it performs the operation that I want by copying all the data entries for 2005 and writing them to a new file named appropriately.

I want to avoid having to type that for each year, whats the best way to write a script that will use the awk command and do it for each year? Should I write it in a perl script and use awk commands within that?

If I need to clarify anything, please let me know. Any help would be greatly appreciated!

Briana
 
Old 05-05-2009, 09:18 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
is the year always at the same position?
Code:
awk '{print $0 > "ottawa"substr($1,7,4)".txt"}' file
 
Old 05-05-2009, 09:53 AM   #3
briana.paige
LQ Newbie
 
Registered: May 2009
Location: Canada
Posts: 14

Original Poster
Rep: Reputation: 0
Hi,

That worked perfectly, exactly what I needed. Thank you so much!

Briana

Last edited by briana.paige; 05-05-2009 at 10:15 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Extracting chunks of data based on variables stored in another file (Perl?) mchriste Linux - Software 2 03-12-2009 12:44 PM
awk/perl script for extract data mruknown1 Programming 3 09-11-2007 09:19 AM
awk/perl script for extract data mruknown1 Linux - Newbie 2 09-11-2007 04:42 AM
extracting data from html files into one text file adityavpratap Slackware 9 05-10-2007 10:30 AM
How exclude | from txt.file using awk or sed? sarajevo Programming 2 08-21-2006 07:26 AM


All times are GMT -5. The time now is 11:35 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration