LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-03-2010, 07:52 AM   #1
vandana.parwani
LQ Newbie
 
Registered: Mar 2010
Posts: 1

Rep: Reputation: 0
Post need to create awk sript for generating two files from a file checking a condition


Hi,

I have a csv file like, having HDR segment and multiple LIN01 segment in each line:like

HDR01;INSDES;XOSN;HEL;39;0;LWS21
LIN01;0017064576;000010;R;8000043703;008R12989;1,000;20090727;110504;PORVOON OFFS' ETPAINO OY;14 YRITTÄJÄ' NKATU;;;;PORVOO;06150;Jyrki;Toivola;;2001269538;;LR;;

LIN01;0017064783;000010;R;8000043706;013R00623;1,000;20090727;110507;AUTO-KILTA TRUCKS OY;27 TERMINAALINKATU;;;;LAPPEENRANTA;53420;Tea;Hirvonen;;2002604609;;LF;;

LIN01;0017065490;000010;R;8000043717;641S00098;1,000;20090727;110515;ETELÄ- SAVO'N SAIRAANHOITOPIIRI;35-37 PORRASSALME' NKATU;;;;MIKKELI;50100;sauli;put konen;;2001236683;;LR;;

here i want to split file in two, one having LIN01 segment which have LR at end, and one which have LF at end in LIN01 segment.

is it possible to created awk or shell script for this?

regards,
vandy.
 
Old 03-03-2010, 08:18 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
to clarify: Remove the section beginning with "LIN01" and ending with "LF;;" and write that section to a new file.

Easy to do with SED----
Use an address range---eg:
sed '/start/,/stop/ <action>' filename

It could be 2 passes--one to filter out that section and write to a new file, the second to delete the section from the original file. Once you have the basic logic, it may be possible to combine into one statement.

Good SED tutorial here:
http://www.grymoire.com/Unix/Sed.html
 
Old 03-03-2010, 08:38 AM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
awk '/LIN01.*LR;;/{print $0 > "LIN01LR.txt"}/LIN01.*LF;;/ {print $0 > "LIN01LF.txt"}' file
 
Old 03-03-2010, 11:15 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Code:
sed -i -e '/^LIN01/,/LF;;$/ w newfile' -e '/^LIN01/,/LF;;$/ d' oldfile
I would have thought the one could combine the two commands inside brackets({}), but that is eluding me...
 
  


Reply

Tags
awk



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
[SOLVED] bash command or sript to list files sepi Linux - Software 7 08-25-2009 04:15 PM
If condition for checking patterns in multiple files rattlesnakejoe Programming 5 05-07-2009 02:52 AM
checking for intltool >= 0.35.0... awk: cmd. line:1: fatal: cannot open file `./intlt chytraeus Linux - Software 2 12-25-2008 05:08 AM
Thank every one. PATHCHANGER sript file Add/Remove directory to path! icecoolcorey Programming 1 06-13-2008 01:42 AM
Stuck on sript to scp a file nazs Programming 10 04-24-2007 06:19 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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