The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Spliting file based on condition Raamc Shell Programming and Scripting 2 05-15-2008 08:51 AM
How to split the String based on condition? sankar reddy Shell Programming and Scripting 2 03-19-2008 04:48 AM
Moving file to directory based on condition. ramanagh Shell Programming and Scripting 2 02-02-2008 08:41 AM
Read file based on condition sbasetty Shell Programming and Scripting 5 01-31-2007 11:54 PM
Splitting a file based on some condition and naming them srivsn Shell Programming and Scripting 1 12-07-2005 08:27 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 06-13-2005
Registered User
 

Join Date: Jun 2005
Posts: 26
awk script to split a file based on the condition

I have the file with the records like


4234234 US phone
3244234 US cup
2342342 CA phone
8947234 US phone
2389472 CA cup
2348972 US maps
3894234 CA phone

I want the records with (US,phone) as record to be in one file, (Us, cup) in another file and (CA,cup) to be in another
I mean all records with the last two records forming unique pair in the one file itself
Is it possible in awk?
Reply With Quote
Forum Sponsor
  #2  
Old 06-13-2005
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,683
How about this ?

Code:
awk '{ print $0 >> $2$3.txt }' input.txt
where all US cup combination will go into the file UScup.txt, likewise with US phone into USphone.txt et al.

Vino
Reply With Quote
  #3  
Old 06-13-2005
Registered User
 

Join Date: Jun 2005
Posts: 26
Thanks vino, it worked
I have one more question
If the file is like this
WSRTK10000000000000067839904809787489959595924667889USMNC
WSRTK10000893479900006783990480978748995959592466673CNATT
WSRTK10000893472387462342349899000067839904809787455USAPT
I know that the last 5 characters in each line is my search pattern and my problem remains the same
I want lines containing patterns like USMNC to go to US_MNC etc
Can I extract last few characters of each line in awk?
Reply With Quote
  #4  
Old 06-13-2005
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,683
Ah. The plot thickens !

Is it always the last 5 characters ?

Vino
Reply With Quote
  #5  
Old 06-13-2005
Registered User
 

Join Date: Jun 2005
Posts: 26
Yes, and each line has equal number of bits, say the pattern starts always after 300 bits in each line and pattern is of 5 bits only
Reply With Quote
  #6  
Old 06-13-2005
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,683
How about this ?

Code:
sed -e 's/\(.*\)\([A-Z][A-Z]\)\([A-Z][A-Z][A-Z]\)/\1 \2 \3/p' list.txt | awk '{ printf $0 >> $2_$3.txt }'
In this case, your output file will contain each line delimited by spaces between the first long stretch of characters then US and then the last 3 characters.

Not 100% right, but close. Have to figure out why..

Vino

Last edited by vino; 06-13-2005 at 10:58 PM.
Reply With Quote
  #7  
Old 06-13-2005
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,683
Here this works fine.
Code:
#! /bin/sh

while read line
do
name=`echo $line | sed -n -e 's/\(.*\)\([A-Z][A-Z]\)\([A-Z][A-Z][A-Z]\)/\2_\3/p'`
echo "$line" >> $name.txt
done < list.txt
Where $name will have the value like US_APT et al.. and list.txt is your input file.

Vino
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 01:50 AM.


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 Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0