The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
mutliple files in the same directory epi8 Shell Programming and Scripting 8 05-12-2008 11:04 PM
process mutliple files in the same directory epi8 UNIX for Dummies Questions & Answers 1 05-12-2008 11:43 AM
How to split a field into two fields? vbrown Shell Programming and Scripting 4 02-21-2008 02:50 AM
can Awk split my field on the . oly_r Shell Programming and Scripting 6 10-26-2007 04:16 PM
Split a field in awk script CamTu Shell Programming and Scripting 4 03-21-2005 12:03 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-16-2008
Registered User
 

Join Date: Apr 2008
Posts: 14
Question Please Help:Need to Split the file into mutliple files depends on the KEY field value

Hi Gurus,

I am new to UNIX(HP). my requirmnet is File needs to needs to be split into multiple files dependa on one key value.
example
1 abc 333 us
2 bbc 444 ch
5 nnn 333 kk
7 hhh 555 ll
3 hhh 333 hh

now the requirment is line with 333 should be copied into test1.txt and the rest to test2.txt.

normally i will recieve 2 files name a1.txt and a2.txt once a daily to one of my server . Depends on the condition i need to split the files and send it to 2different servers.

appreciate your help on the above.

Thanks
Arun
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 04-16-2008
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 3,883
Code:
awk '{ if ($3 == "333") {print $0 > test1.txt}
         else { print $0 > test2.txt} 
       } ' inputfile
Reply With Quote
  #3 (permalink)  
Old 04-16-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
Or try

Code:
grep 333 inputfile >test1.txt
grep -v 333 inputfile >test2.txt
If you know the value 333 is always in the third field then by all means use the awk script. (On the other hand, it's certainly possible to construct a regular expression for grep to express the same constraint.)
Reply With Quote
  #4 (permalink)  
Old 04-16-2008
Registered User
 

Join Date: Apr 2008
Posts: 14
Thanks Jim,

but when i appy the same code i am getting error like below. i don;t know why.

awk ' { if ($3 == "RD018414292") {print $0 > test1.txt} else { print $0 > test2.txt} } ' arigmrec03_2007_02_02_15_06_59_505.success
syntax error The source line is 1.
The error context is
{ if ($3 == "RD018414292") {print $0 > >>> test1. <<< txt} else { print $0 > test2.txt} }
awk: The statement cannot be correctly parsed.
The source line is 1.



is that $3 in if statment refers to 3rd column? but my actual file cotains may fileds; i have just copied one line below.

200804NA441200119000000000000000200804083727708DELPHI DELCO ELECTRONICS EFTRD002136823A8020570218671R08098 -00000000131541012900015979340420080327GM 56741 0000000000000NN DELPHI DELCO ELECTRONICS EFTOSYNDCN052777597 200804042304 SPON USD-000000001315410USD 000001000000000 20080502


in that above line i need to capture a field value(RD002136823) starting at the position 78 and ending at 89 into another file.

also can i give multiple files as input to code?

waiting for your response
Arun
Reply With Quote
  #5 (permalink)  
Old 04-16-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
Try the grep above (we seem to have posted at exactly the same time). You can give multiple files as input to most Unix commands.

Code:
grep    RD018414292 file1 file2 file3 >have
grep -v RD018414292 file1 file2 file3 >have.not
Reply With Quote
  #6 (permalink)  
Old 04-16-2008
Registered User
 

Join Date: Apr 2008
Posts: 14
Hi Era,

Thanks for the help. but requirment is little bit different. you have given example for a single value search, but i want to search multiple values in given files and put result into single file.

waiting for your response.

Thanks
Arun
Reply With Quote
  #7 (permalink)  
Old 04-16-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
Different values in different files, or multiple values in multiple files, or different sets of values in different sets of files?

Code:
egrep 'first|second|third' file1 file2 file3 >have
egrep -v 'first|second|third' file1 file2 file3 >have.not
If the value is different for each file, you need a loop.

Code:
>have; >have.not  # make sure they're empty before we start
while read file value; do
  grep    "$value" "$file" >>have
  grep -v "$value" "$file" >>have.not
done <<HERE
file1  first
file2  second.*rege[xz]magic$
file3  how sweet I roam from field to field
HERE
This is to illustrate that you can have fairly complex "value" fields in the here document. You probably don't need that flexibility for this particular problem, but if you do, it's there. (And if you need to search for stuff which has special meaning in regular expressions, such as asterisks or dollar signs, you need to escape them, or something.)
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
regex, regular expressions

Thread Tools
Display Modes




All times are GMT -7. The time now is 09:17 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0