Latest LQ Deal: Latest LQ Deals
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 05-05-2009, 10:10 AM   #1
LQ Newbie
Registered: May 2009
Location: Canada
Posts: 14

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


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!

Old 05-05-2009, 10:18 AM   #2
Senior Member
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

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

Original Poster
Rep: Reputation: 0

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


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


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 01:44 PM
awk/perl script for extract data mruknown1 Programming 3 09-11-2007 10:19 AM
awk/perl script for extract data mruknown1 Linux - Newbie 2 09-11-2007 05:42 AM
extracting data from html files into one text file adityavpratap Slackware 9 05-10-2007 11:30 AM
How exclude | from txt.file using awk or sed? sarajevo Programming 2 08-21-2006 08:26 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:23 AM.

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