Need script for making files based on some conditions.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need script for making files based on some conditions.
# 1  
Old 05-06-2015
Need script for making files based on some conditions.

Hi All,

I have a text file (code_data.txt) with the followig data.
Code:
AMAR     AB123456                  XYZ
KIRAN    CB789                        ABC
RAJ      CS78890                       XYZ
KAMESH   A33535335                 ABC
KUMAR    MD678894                   MAT
RITESH   SR3535355                  SAB
RAHUL    PM366536666               SAS
RAMANA   KS566767477747         ABC
DINESH   SA6666464664646         SAB
SUJAN    GD6674474747              SAS
SOM      MD6546474777              XYZ
GANE     MS657869933553666747  XYZ

I have to make three files using this file (code_data.txt) based on the below conditions.

The records which belongs to the code "XYZ" should be in one file,
the records which belongs to the code "ABC" should be in one file
and remaining records which belongs to all other codes should be in one file.

In the target three files we should exclude the code.

The expected files should be as below.

Code:
file1_XYZ

AMAR     AB123456               
RAJ       CS78890                
SOM      MD6546474777          
GANE     MS657869933553666747   

file2_ABC

KIRAN     CB789                  
KAMESH   A33535335              
RAMANA   KS566767477747         

file3_REM

KUMAR    MD678894               
RITESH   SR3535355              
RAHUL    PM366536666            
DINESH   SA6666464664646        
SUJAN    GD6674474747

Please help me.

Thanks in advance.

Last edited by ROCK_PLSQL; 05-06-2015 at 08:20 AM..
# 2  
Old 05-06-2015
Please use code tags as required by forum rules!

Any attempts from your side to solve the problem?
# 3  
Old 05-06-2015
Hi,

Sorry for violating rules.
I have edited my post .

I am new to Unix. I am not getting any ideas how to cut the lines based on data.

Please help me.

Thanks.
# 4  
Old 05-06-2015
Code:
awk '$3 !~ /ABC|XYZ/ {$3="REM"} {print $1, $2 > "file_" $3}' code_data.txt

If you REALLY need the file numbering, we need to readdress.
# 5  
Old 05-18-2015
Hi,

This is working fine as per my intial requirement.

awk '$3 !~ /ABC|XYZ/ {$3="REM"} {print $1, $2 > "file_" $3}' code_data.txt

Now the file has been changed from txt to csv
and also code is coming as first column in the file.

Code:
XYZ;AMAR ;AB123456               
ABC;KIRAN;CB789                        
XYZ;RAJ;CS78890                       
ABC;KAMESH;A33535335                 
MAT;KUMAR;MD678894                   
SAB;RITESH;SR3535355                  
SAS;RAHUL;PM366536666               
ABC;RAMANA;KS566767477747         
SAB;DINESH;SA6666464664646         
SAS;SUJAN;GD6674474747              
XYZ;SOM ;MD6546474777              
XYZ;GANE ;MS657869933553666747


Code:
awk '$1 !~ /ABC|XYZ/ {$1="REM"} {print $2, $3 > "file_" $1}' code_data.csv

Code:
file1_XYZ

;AMAR    ;AB123456               
;RAJ       ;CS78890                
;SOM      ;MD6546474777          
;GANE     ;MS657869933553666747

The intial ";" should not come in the file.
The file should be as below.The has to be created in a particular directory.


Code:
file1_XYZ

AMAR    ;AB123456               
RAJ       ;CS78890                
SOM      ;MD6546474777          
GANE     ;MS657869933553666747

I tried the below code its not working.


Code:
awk '$1 !~ /ABC|XYZ/ {$1="REM"} {gsub(";","",$2);print $2, $3 > $MSD_DIR"file_" $1}' code_data.csv

Please help me.
Thanks.
# 6  
Old 05-18-2015
Heya

Code:
oIFS="$IFS"
IFS=";"

file_abc=~/abc.csv
file_xyz=~/xyz.csv
file_rest=~/rest.csv

while read id name num;do
	case $id in
	ABC)	out=$file_abc	;;
	XYZ)	out=$file_xyz	;;
	*)	out=$file_rest	;;
	esac
	echo "$name ; $num" >> "$out"
done<code_data.csv
IFS="$oIFS"

Hope this helps

Last edited by sea; 05-18-2015 at 11:27 AM.. Reason: Typos fixed, should have mentioned: untested...
# 7  
Old 05-18-2015
Hi,

This is giving some syntax error.
Code:
awk '$1 !~ /ABC|XYZ/ {$1="REM"} {print $2, $3 > $MSD_DIR"file_" $1}' code_data.csv

And also the files should be created in the directory $MSD_DIR

Thanks

Last edited by Franklin52; 05-18-2015 at 10:33 AM.. Reason: Please use code tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Merge input from two files into one based on conditions

Using Linux (bash), I have two files which contain information about berries. Example: file1.txt: Blueberry blue 14 Raspberry red 12 Blackberry dark 4 file2.txt Blackberry sour 4 3 Blueberry tasty 12 78 Strawberry yummy 33 88 I want to merge these two files into one. The desired... (5 Replies)
Discussion started by: Zooma
5 Replies

2. Shell Programming and Scripting

Split File based on different conditions

I need to split the file Conditions: Ignore any record that either starts with 1 or 9 Split the file at position 404 , if position 404 is abc or def then write all the records in a file > File 1 , the remaining records should go in to a file > File 2 Further I want to split the... (7 Replies)
Discussion started by: protech
7 Replies

3. Shell Programming and Scripting

Making a script to copy files not seen before (using md5sum)

Hello, I would like to make a script that searches through a SRC folder and copies only files it's never seen before to a DEST folder. SRC = /user/.phonesync/photos-backup DST = /usr/.phonesync/photos-new So basically, I'd start with a: md5sum /user/.phonesync/photos-backup/* >... (29 Replies)
Discussion started by: nbsparks
29 Replies

4. UNIX for Dummies Questions & Answers

Shell script to extract data from csv file based on certain conditions

Hi Guys, I am new to shell script.I need your help to write a shell script. I need to write a shell script to extract data from a .csv file where columns are ',' separated. The file has 5 columns having values say column 1,column 2.....column 5 as below along with their valuesm.... (1 Reply)
Discussion started by: Vivekit82
1 Replies

5. Shell Programming and Scripting

Report generation based on certain conditions

Hi I recently joined a project where I have been asked to generate a report using shell script accessing UNIX box. I have no idea on how to do it as I am a beginner and learning shell scripts. Suppose I have a XML: Code: ... (3 Replies)
Discussion started by: vat1kor
3 Replies

6. Shell Programming and Scripting

PHP Script Help - Making links to files Clickable

Ok so I wrote a php script that outputs the below to users on a webpage. # Download: /home/content/d/i/v/divine1234/eBookDownloads/ScalpRemedy_jablaa12734.zip the php code that outputs the above is: echo ("<li>Download: $download_link</li>\n"); The thing is, I dont want... (1 Reply)
Discussion started by: SkySmart
1 Replies

7. Shell Programming and Scripting

awk merging files based on 2 complex conditions

1. if the 1st row IDs of input1 (ID1/ID2.....) is equal to any IDNames of input2 print all relevant values together as defined in the output. 2. A bit tricky part is IDno in the output. All we need to do is numbering same kind of letters as 1 (aa of ID1) and different letters as 2 (ab... (4 Replies)
Discussion started by: ruby_sgp
4 Replies

8. UNIX for Dummies Questions & Answers

any script for joining files based on simple conditions

Condition1; If NPID and IndID of both input1 and input2 are same take all the vaues relevant to them and print together as output Condition2; IDNo in output: Take the highly repeated same letter of similar NPID-IndID as *1* Second highly repeated same letter... (0 Replies)
Discussion started by: stateperl
0 Replies

9. Shell Programming and Scripting

validating a file based on conditions

i have a file in unix in which the records are like this aaa 123 233 aaa 234 222 aaa 242 222 bbb 122 111 bbb 122 123 ccc 124 222 In the output i want only the below records aaa ccc The validation logic is 1st column and 2nd column need to be considered if both columns values are... (8 Replies)
Discussion started by: trichyselva
8 Replies

10. UNIX for Dummies Questions & Answers

creating conditions for making a new line?

Basically I want to change this: a:b c:d:e f:g h:i:j k:l into a:b c d:e f:g h i:j k:l so like if there is two :'s in one line making the first into a new line. If anyone knows how to do this I would be very appreciative! (9 Replies)
Discussion started by: Audra
9 Replies
Login or Register to Ask a Question