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.
# 8  
Old 05-18-2015
Not only the field order changed, but the field separator as well. Plus, you can't simply use shell variables in awk scripts; you'll have to assign them to awk variables. Try
Code:
awk '$1 !~ /ABC|XYZ/ {$1="REM"} {print $2, $3 > DIR"/file_" $1}' FS=";" OFS=";" DIR="$MSD_DIR" code_data.csv

# 9  
Old 05-18-2015
Code:
awk -F\; '{ if ( match($1,"XYZ")) print "echo "$2,$3 " >> File_"$1;
            if ( match($1,"ABC")) print "echo "$2,$3 " >> File_"$1;
            if ( ( $1 != "XYZ" ) && ( $1 != "ABC" ) ) print "echo "$2,$3 " >> File_REM"}' code_data.csv | sh -x

# 10  
Old 05-18-2015
Hi Rudi,

Thanks for the script.

Your script is working fine.

I added some more for file format but it's not giving correct file name.
For some files some spaces are added in the file name.

Code:
date_yyyymmdd=$(my_date "" -e"%Y%m%d")
file_format="_$date_yyyymmdd.csv
awk '$1 !~ /ABC|XYZ|MSORT|SDDCCR/ {$1="REM"} {print $2, $3 > DIR"/file_" $1}' FS=";" OFS=";" DIR="$MSD_DIR" file_out="$file_format" code_data.csv

I got the file names as below.
Code:
file_ABC   _20150518.csv
file_XYZ   _20150518.csv
file_MSORT _20150518.csv
file_SDDCCR_20150518.csv

Please help me.

Thanks.

Last edited by ROCK_PLSQL; 05-18-2015 at 09:58 AM..
# 11  
Old 05-18-2015
I don't believe that with your above code snippet you'll have those file names. With
Code:
awk '$1 !~ /ABC|XYZ|MSORT|SDDCCR/ {$1="REM"} {print $2, $3 > DIR"/file_" $1 file_out}' FS=";" OFS=";" DIR="$MSD_DIR" file_out="$file_format" code_data.csv

I'm getting
Code:
file_ABC_20150518.csv
file_REM_20150518.csv
file_XYZ_20150518.csv

so I can't reproduce your problem.

---------- Post updated at 15:29 ---------- Previous update was at 15:27 ----------

Not all awks handle the concatenation > DIR"/file_" $1 file_out for the output file name correctly, though. Should that be the case for you, you'll need to compose the file name beforehand into a variable.
# 12  
Old 05-18-2015
Hi Rudi,

Might be You tried with old csv file. That is the reason you were not able to reproduce this.

Can u please try with this 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
MSORT;DINESH;SA6666464664646         
MSORT;SUJAN;GD6674474747              
MSORT;SOM ;MD6546474777 
SDDCCR;DINESH;SA6666464664646         
SDDCCR;SUJAN;GD6674474747              
SDDCCR;SOM ;MD6546474777

Thanks
# 13  
Old 05-18-2015
Must it be awk?
Any specific reason why this wouldnt work?

Cheers
# 14  
Old 05-18-2015
I get these files with your new file above:
Code:
file_ABC_20150518.csv
file_MSORT_20150518.csv
file_REM_20150518.csv
file_SDDCCR_20150518.csv
file_XYZ_20150518.csv
-->  file_ABC_20150518.csv:
KIRAN;CB789                        
KAMESH;A33535335                 
RAMANA;KS566767477747         
-->  file_MSORT_20150518.csv:
DINESH;SA6666464664646         
SUJAN;GD6674474747              
SOM ;MD6546474777 
-->  file_REM_20150518.csv:
KUMAR;MD678894                   
RITESH;SR3535355                  
RAHUL;PM366536666               
DINESH;SA6666464664646         
SUJAN;GD6674474747              
-->  file_SDDCCR_20150518.csv:
DINESH;SA6666464664646         
SUJAN;GD6674474747              
SOM ;MD6546474777
-->  file_XYZ_20150518.csv:
AMAR ;AB123456               
RAJ;CS78890                       
SOM ;MD6546474777              
GANE ;MS657869933553666747

Are you aware that there are many trailing spaces in many lines of your new file? It doesn't hurt for the task we're tackling here, but it might be unnecessary.
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