How can i segregate?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How can i segregate?
# 1  
Old 01-02-2013
How can i segregate?

I have this file which contains


Code:
Code:
91886,000,MiniC2-00,1.9.12,aML,en
91886,000,MiniC2-00,1.9.12,aML,en
91886,000,MiniC2-00,1.9.12,aML,en
91886,000,MiniC2-00,1.9.12,aML,en
91886,000,MiniC2-00,1.9.12,aML,en
91886,000,MiniC2-00,1.9.12,aML,en
91886,000,MiniC2-00,3.0,aML,en
91886,000,MiniC2-00,1.9.12,aML,en
91886,000,MiniC2-00,1.9.12,aML,en
91886,000,MiniC2-00,1.9.12,aML,en
91886,000,MiniC2-00,1.9.12,aML,en
91886,000,MiniC2-00,4.9,aML,en
91886,000,MiniC2-00,1.0,aML,en
91886,000,MiniC2-00,1.9.12,aML,en
91886,000,MiniC2-00,1.9.12,aML,en
91886,000,MiniC2-00,1.9.12,aML,en


I want to seggregate based on the 4th column i.e. if it is 1.0, 2.0, 3.0, 4.0 & 5.0 den i need to add a column with value S30 in between 3rd and 4th i.e. as shown below

91886,000,MiniC2-00,S30,1.0,aML,en

else add a column with value S40 in between 3rd and 4th i.e. as shown below
91886,000,MiniC2-00,S40,1.9.12,aML,en
# 2  
Old 01-02-2013
Try

Code:
awk -F, '{if($4 ~ /^1\.0|2\.0|3\.0|4\.0|5\.0$/){sub($3,$3",S30")}else{sub($3,$3",S40")}}1' file

OR

Code:
awk -F, '{if($4 ~ /^1\.0|2\.0|3\.0|4\.0|5\.0$/){$3=$3",S30"}else{$3=$3",S40"}}1' OFS="," file

# 3  
Old 01-02-2013
EDIT: Misread the question

Last edited by Franklin52; 01-02-2013 at 07:41 AM..
# 4  
Old 01-02-2013
i did a mistake ...

I wanted the 3rd colum to be replaced....

sorry....


required o/p

Code:
Code:
91886,000,S40,1.9.12,aML,en


dont want this one

Code:
Code:
91886,000,MiniC2-00,S40,1.9.12,aML,en

---------- Post updated at 05:06 PM ---------- Previous update was at 05:03 PM ----------

Code:
set -x
>proper_file
while read line
do
echo $line
var=`echo $line|awk -F, '{print $4}'`
model=`echo $line|awk -F, '{print $3}'`
if [ "$var" = "1.0" -o "$var" = "2.0" -o "$var" = "3.0" -o "$var" = "4.0" -o "$var" = "5.0" ]
then
sed "s/$model/S30/g" >> proper_file
else
sed "s/$model/S40/g" >> proper_file
fi
done < normal.csv
~



thanks PAMU, frank & MYSELF Smilie

Last edited by Scott; 01-02-2013 at 08:41 AM.. Reason: Code tags
# 5  
Old 01-02-2013
Try this one since it is one line
Code:
 awk -F, '{if($4 ~/1\.0|2\.0|3\.0|4\.0|5\.0/){$3="S30"}else{$3="S40"}1} OFS=","' file_name

# 6  
Old 01-02-2013
Quote:
Originally Posted by nikhil jain
i did a mistake ...

I wanted the 3rd colum to be replaced....

sorry....


required o/p

Code:
Code:
91886,000,S40,1.9.12,aML,en


dont want this one

Code:
Code:
91886,000,MiniC2-00,S40,1.9.12,aML,en

---------- Post updated at 05:06 PM ---------- Previous update was at 05:03 PM ----------

Code:
set -x
>proper_file
IFS=','
while read col1 col2 var model other
do
if [ "$var" = "1.0" -o "$var" = "2.0" -o "$var" = "3.0" -o "$var" = "4.0" -o "$var" = "5.0" ]
then
sed "s/$model/S30/g" >> proper_file
else
sed "s/$model/S40/g" >> proper_file
fi
done < normal.csv
~



thanks PAMU, frank & MYSELF Smilie
Use shell built-in whenever possible since it will reduce processing time.

Cheers,
RangaSmilie

---------- Post updated at 09:07 AM ---------- Previous update was at 08:55 AM ----------

Quote:
Originally Posted by senthilkumark
Try this one since it is one line
Code:
 awk -F, '{if($4 ~/1\.0|2\.0|3\.0|4\.0|5\.0/){$3="S30"}else{$3="S40"}1} OFS=","' file_name

$4 - will match even '11.0' or '1.03' something like that. So if you want to be more specific match or more accurate. You should use like below,

Code:
 awk -F, '{if($4 ~/^1\.0$|^2\.0$|^3\.0$|^4\.0$|^5\.0$/)......

Cheers,
RangaSmilie
# 7  
Old 01-03-2013
Code:
awk '{$3=($4~/^[1-5]\.0/)?"S30":"$40"}1' FS=, OFS=, infile


Last edited by rdcwayx; 01-03-2013 at 10:17 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to segregate a section from big file?

Hello, I need to know all IP range (ip_prefix), associated with us-west-2 region only from this link - https://ip-ranges.amazonaws.com/ip-ranges.json (it can be opened in wordpad for better visibility) Please suggest, how would I do it. If vi, awk or sed is needed, I have downloaded it on my... (7 Replies)
Discussion started by: solaris_1977
7 Replies

2. Shell Programming and Scripting

Need help in awk: running a loop with one column and segregate data 4 each uniq value in that field

Hi All, I have a file like this(having 2 column). Column 1: like a,b,c.... Column 2: having numbers. I want to segregate those numbers based on column 1. Example: file. a 5 b 9 b 620 a 710 b 230 a 330 b 1910 (4 Replies)
Discussion started by: Raza Ali
4 Replies

3. Shell Programming and Scripting

Segregate files based on the time they are created

Hi All, I have scenario where I need to zip huge number of DB audit log files newer than 90 days and delete anything older than that. If the files are too huge in number,zipping will take long time and causing CPU spikes. To avoid this I wanted to segregate files based on how old they are and... (2 Replies)
Discussion started by: veeresh_15
2 Replies

4. UNIX for Advanced & Expert Users

Segregate file content using sed backreference

I have some text like EU1BTDAT:ASSGNDD filename='$SEQFILES/SUNIA.PJ008202.CARDLIB/DATECARD' EU1BTDATEST:ASSGNDD filename='$SEQFILES/SUNIA.PJ008202.CARDLIB/DATECARD' EU1CLOSEDATES:ASSGNDD filename='$SEQFILES/SUNIA.PJ008202.CARDLIB/DATECARD' EU1DATED:ASSGNDD... (8 Replies)
Discussion started by: gotamp
8 Replies

5. Shell Programming and Scripting

Segregate by suffixed file names using Korn Shell

I have following files at /dir1 a.csv.20131201 b.csv.20131201 c.csv.20131201 d.csv.20131201 a.csv.20131202 b.csv.20131202 c.csv.20131202 d.csv.20131202 ....................... ....................... ....................... ....................... I need to move these files to... (4 Replies)
Discussion started by: JaisonJ
4 Replies

6. Shell Programming and Scripting

Segregate alpha and digits

I have a variable containing values like 10d2, 7a7, a8 or d6 (i.e. <digits><alpha><digits>. Out of these leading digits may not be there. Out of this, I want three variables. The first having value Inital digits, the second one will be alpha and the third will be trailing digits. (In the previous... (2 Replies)
Discussion started by: Soham
2 Replies

7. Shell Programming and Scripting

Segregate a number from a String in the log

Hi, I have below lines in the log file. how to segregate and assign '3904' to a variable. XCMM018I Return Code: 8 Feedback: 0 :&PNUM=3904: (3 Replies)
Discussion started by: johnjs
3 Replies

8. Shell Programming and Scripting

Unix script to segregate dynamic and static content of a web application

I need to deploy a JAVA application on two separate servers: 1. Web server (IBM HTTP Web Servers (IHS)) 2. Application Server (WebSphere Application Server WAS7.0) The static content will have to be deployed and handled on Web server. These would include GIFs, HTML, CSS, etc files.... (0 Replies)
Discussion started by: chani27
0 Replies

9. Shell Programming and Scripting

segregate the file based on matching patterns

print 'test' SETUSER 'dbo' go create proc abc as /Some code here/ go SETUSER go print 'test1' SETUSER 'dbo' go Create Procedure xyz as /some code here/ go SETUSER go print 'test2' SETUSER 'dbo' (2 Replies)
Discussion started by: mad_man12
2 Replies
Login or Register to Ask a Question