awk script to split a file based on the condition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk script to split a file based on the condition
# 1  
Old 06-13-2005
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  
Old 06-13-2005
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  
Old 06-14-2005
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  
Old 06-14-2005
Ah. The plot thickens !

Is it always the last 5 characters ?

Vino
# 5  
Old 06-14-2005
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  
Old 06-14-2005
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 02:58 AM..
# 7  
Old 06-14-2005
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk do not split if condition is meet

Trying to use awk to format the input based on the filed count being 5. Most lines are fine using the awk below, except the first two lines. I know the reason is the -1 in green and -2 in blue. But can not figure out how to not split on the - if it is followed by a digit then letter. Thank you :).... (1 Reply)
Discussion started by: cmccabe
1 Replies

2. UNIX for Beginners Questions & Answers

awk to add +1 to value based on condition in input

In the awk below I am trying to add a | that will adjust $2 in the ouput by adding +1 if the original value from file that was used in $3 had a - in it. Line 3 of file is an example of this. In my current awk I just subtract one but I am not sure how to only apply this to those values without a -.... (5 Replies)
Discussion started by: cmccabe
5 Replies

3. Shell Programming and Scripting

awk to change contents of field based on condition in same file

In the awk below I am trying to copy the entire contents of $6 there may be multiple values seperated by a ;, to $8, if $8 is . (lines 1 and 3 are examples). If that condition $8 is not . (line2 is an example) then that line is skipped and printed as is. The awk does execute but prints the output... (3 Replies)
Discussion started by: cmccabe
3 Replies

4. Shell Programming and Scripting

How to Split File to 2 depending on condition?

Hi , cat myfile.txt ! 3100.2.0.5 ! 3100.2.22.4 ! 3100.2.30.33 ! 3100.2.4.1 ! ! 3100.2.0.5 ! 3100.2.22.4 ! 3100.2.22.11 ! 3100.2.4.1 ! ! 3100.2.0.5 ! 3100.2.2.50 ! 3100.2.22.11 ! 3100.2.4.1 ! ! 3100.2.0.5 ! 3100.2.22.4 ! 3100.2.30.33 ! 3100.2.4.1 ! ! 3100.2.0.5 ! 3100.2.22.4 !... (6 Replies)
Discussion started by: OTNA
6 Replies

5. Shell Programming and Scripting

awk script to split file into multiple files based on many columns

So I have a space delimited file that I'd like to split into multiple files based on multiple column values. This is what my data looks like 1bc9A02 1 10 1000 FTDLNLVQALRQFLWSFRLPGEAQKIDRMMEAFAQRYCQCNNGVFQSTDTCYVLSFAIIMLNTSLHNPNVKDKPTVERFIAMNRGINDGGDLPEELLRNLYESIKNEPFKIPELEHHHHHH 1ku1A02 1 10... (9 Replies)
Discussion started by: viored
9 Replies

6. Shell Programming and Scripting

How to split file into multiple files using awk based on 1 field in the file?

Good day all I need some helps, say that I have data like below, each field separated by a tab DATE NAME ADDRESS 15/7/2012 LX a.b.c 15/7/2012 LX1 a.b.c 16/7/2012 AB a.b.c 16/7/2012 AB2 a.b.c 15/7/2012 LX2 a.b.c... (2 Replies)
Discussion started by: alexyyw
2 Replies

7. Shell Programming and Scripting

Split file based on file size in Korn script

I need to split a file if it is over 2GB in size (or any size), preferably split on the lines. I have figured out how to get the file size using awk, and I can split the file based on the number of lines (which I got with wc -l) but I can't figure out how to connect them together in the script. ... (6 Replies)
Discussion started by: ssemple2000
6 Replies

8. Shell Programming and Scripting

split file with condition

$ cat file H1:12:90 k:12:b n:22:i k:54:b k:42:b s:48:s a:41:b t:18:n c:77:a I am trying to split above file based on $2 such that if $2 is rounded to nearest 10's multiple (e.g. 10,20,30 etc), each sub file should contain 3 multiples and so on (also I want to keep header i.e. NR==1, in... (6 Replies)
Discussion started by: uwork72
6 Replies

9. Shell Programming and Scripting

Split a file based on pattern in awk, grep, sed or perl

Hi All, Can someone please help me write a script for the following requirement in awk, grep, sed or perl. Buuuu xxx bbb Kmmmm rrr ssss uuuu Kwwww zzzz ccc Roooowwww eeee Bxxxx jjjj dddd Kuuuu eeeee nnnn Rpppp cccc vvvv cccc Rhhhhhhyyyy tttt Lhhhh rrrrrssssss Bffff mmmm iiiii Ktttt... (5 Replies)
Discussion started by: kumarn
5 Replies

10. Shell Programming and Scripting

How to split the String based on condition?

hi , I have a String str="/opt/ibm/lotus/ibw/latest" or ="/opt/lotus/ibw/latest" this value is dynamic..I want to split this string into 2 strings 1. /opt/ibm/lotus(/opt/lotus) this string must ends with "lotus" 2./ibw/latest can any body help me on this? Regards, sankar (2 Replies)
Discussion started by: sankar reddy
2 Replies
Login or Register to Ask a Question