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.
# 15  
Old 05-18-2015
Hi,

I have added some more codes "MSORT and SDDCCR" in my CSV file "code_data.csv"
I have to generate saparate files for these codes also.
Code:
file_ABC_20150518.csv
file_XYZ_20150518.csv
file_MSORT_20150518.csv
file_SDDCCR_20150518.csv
file_REM_20150518.csv

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

Why you got only belowthree files.It suppose to be five files.
Code:
file_ABC_20150518.csv
file_REM_20150518.csv
file_XYZ_20150518.csv

Thanks,

Last edited by Franklin52; 05-18-2015 at 11:11 AM.. Reason: Please use code tags
# 16  
Old 05-18-2015
Quote:
Originally Posted by sea
Must it be awk?
Any specific reason why this wouldnt work?

Cheers
I can find three:
- the IFS is set to the empty string; the semicolon should be quoted.
- there's two esacs but no done (for the while...do)
- "$out" is overwritten for every line, not appended.

If these are corrected, that snippet works as required.

---------- Post updated at 16:10 ---------- Previous update was at 16:08 ----------

Quote:
Originally Posted by ROCK_PLSQL
Hi,

I have added some more codes "MSORT and SDDCCR" in my CSV file "code_data.csv"
I have to generate saparate files for these codes also.

file_ABC_20150518.csv
file_XYZ_20150518.csv
file_MSORT_20150518.csv
file_SDDCCR_20150518.csv
file_REM_20150518.csv

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

Why you got only belowthree files.It suppose to be five files.

ile_ABC_20150518.csv
file_REM_20150518.csv
file_XYZ_20150518.csv

Thanks,
Who are you talking to? I proved that the required five files are generated and do have the correct contents?
# 17  
Old 05-18-2015
I would use grep rather than awk. But if your professor wants awk,
then you need to use awk... Smilie

Code:
grep    "ABC" code_data.txt                 > file1.csv
grep    "XYZ" code_data.txt                 > file2.csv
grep -v "ABC" code_data.txt | grep -v "XYZ" > file3.csv

# 18  
Old 05-18-2015
Quote:
Originally Posted by RudiC
I can find three:
- the IFS is set to the empty string; the semicolon should be quoted.
- there's two esacs but no done (for the while...do)
- "$out" is overwritten for every line, not appended.

If these are corrected, that snippet works as required.
Ops, you're right, sorry.
Its fixed now.
To add these new 'condititions', simply add a modified line similar to the existing ones for ABC and XYZ.

hth
# 19  
Old 05-18-2015
Hi All,

I ran this script I got the files as

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


Code:
file_ABC   _20150518.csv
file_XYZ   _20150518.csv
file_MSORT _20150518.csv
file_SDDCCR_20150518.csv
file_REM   _20150518.csv

What is wrong with me.

Please help me.

Thanks.

Last edited by Don Cragun; 05-19-2015 at 04:34 AM..
# 20  
Old 05-18-2015
You may have <TAB>s in your input file that won't be removed as the field separator is ";". Post the output of od -ctx1for a few lines of your data file.
# 21  
Old 05-19-2015
Hi,

I am extremely sorry ,
Earlier I haven't formated my code_data.csv file.
Please find the data in the code_data.csv file as below.
code_data.csv

Code:
SDDCCR;  SOM   ; MD6546474777   ;
ABC   ;  KIRAN ; CB789          ;
ABC   ;  RAMANA; KS566767477747 ;
ABC   ;  KAMESH; A33535335      ;
SDDCCR;  DINESH; GD6674474747   ;
SDDCCR;  SUJAN ; SA6666464664646;
XYZ  ;   AMAR  ; AB123456       ;
XYZ  ;   RAJ   ; CS78890        ;
XYZ  ;   GANE  ; MD6546474777   ;
XXX  ;   MANO  ; MD6546474777   ;


Please find the output.

Code:
> od -ctx1 code_data.csv
0000000   S   D   D   C   C   R   ;           S   O   M               ;
          53  44  44  43  43  52  3b  20  20  53  4f  4d  20  20  20  3b
0000020       M   D   6   5   4   6   4   7   4   7   7   7
          20  4d  44  36  35  34  36  34  37  34  37  37  37  20  20  20
0000040   ;  \n   A   B   C               ;           K   I   R   A   N
          3b  0a  41  42  43  20  20  20  3b  20  20  4b  49  52  41  4e
0000060       ;       C   B   7   8   9
          20  3b  20  43  42  37  38  39  20  20  20  20  20  20  20  20
0000100           ;  \n   A   B   C               ;           R   A   M
          20  20  3b  0a  41  42  43  20  20  20  3b  20  20  52  41  4d
0000120   A   N   A   ;       K   S   5   6   6   7   6   7   4   7   7
          41  4e  41  3b  20  4b  53  35  36  36  37  36  37  34  37  37
0000140   7   4   7       ;  \n   A   B   C               ;           K
          37  34  37  20  3b  0a  41  42  43  20  20  20  3b  20  20  4b
0000160   A   M   E   S   H   ;       A   3   3   5   3   5   3   3   5
          41  4d  45  53  48  3b  20  41  33  33  35  33  35  33  33  35
0000200                           ;  \n   S   D   D   C   C   R   ;
          20  20  20  20  20  20  3b  0a  53  44  44  43  43  52  3b  20
0000220       D   I   N   E   S   H   ;       G   D   6   6   7   4   4
          20  44  49  4e  45  53  48  3b  20  47  44  36  36  37  34  34
0000240   7   4   7   4   7               ;  \n   S   D   D   C   C   R
          37  34  37  34  37  20  20  20  3b  0a  53  44  44  43  43  52
0000260   ;           S   U   J   A   N       ;       S   A   6   6   6
          3b  20  20  53  55  4a  41  4e  20  3b  20  53  41  36  36  36
0000300   6   4   6   4   6   6   4   6   4   6   ;  \n   X   Y   Z
          36  34  36  34  36  36  34  36  34  36  3b  0a  58  59  5a  20
0000320       ;               A   M   A   R           ;       A   B   1
          20  3b  20  20  20  41  4d  41  52  20  20  3b  20  41  42  31
0000340   2   3   4   5   6                               ;  \n   X   Y
          32  33  34  35  36  20  20  20  20  20  20  20  3b  0a  58  59
0000360   Z           ;               R   A   J               ;       C
          5a  20  20  3b  20  20  20  52  41  4a  20  20  20  3b  20  43
0000400   S   7   8   8   9   0                                   ;  \n
          53  37  38  38  39  30  20  20  20  20  20  20  20  20  3b  0a
0000420   X   Y   Z           ;               G   A   N   E           ;
          58  59  5a  20  20  3b  20  20  20  47  41  4e  45  20  20  3b
0000440       M   D   6   5   4   6   4   7   4   7   7   7
          20  4d  44  36  35  34  36  34  37  34  37  37  37  20  20  20
0000460   ;  \n   X   X   X           ;               M   A   N   O
          3b  0a  58  58  58  20  20  3b  20  20  20  4d  41  4e  4f  20
0000500       ;       M   D   6   5   4   6   4   7   4   7   7   7
          20  3b  20  4d  44  36  35  34  36  34  37  34  37  37  37  20
0000520           ;  \n
          20  20  3b  0a
0000524

Please help me.

Thanks,
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