The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 11:51 AM
How to split the String based on condition? sankar reddy Shell Programming and Scripting 2 03-19-2008 07:48 AM
Moving file to directory based on condition. ramanagh Shell Programming and Scripting 2 02-02-2008 11:41 AM
Read file based on condition sbasetty Shell Programming and Scripting 5 02-01-2007 02:54 AM
Splitting a file based on some condition and naming them srivsn Shell Programming and Scripting 1 12-07-2005 11:27 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-13-2005
superprogrammer superprogrammer is offline
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?
  #2 (permalink)  
Old 06-13-2005
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
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
  #3 (permalink)  
Old 06-14-2005
superprogrammer superprogrammer is offline
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?
  #4 (permalink)  
Old 06-14-2005
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

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

Is it always the last 5 characters ?

Vino
  #5 (permalink)  
Old 06-14-2005
superprogrammer superprogrammer is offline
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
  #6 (permalink)  
Old 06-14-2005
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
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-14-2005 at 01:58 AM..
  #7 (permalink)  
Old 06-14-2005
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
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
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 04:53 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0