Input File: F_Data_File.txt
10001|A1|TRANS|Y
10002|A2|MED|Y
10003|A3|FIN|N
10004|A4|HR|Y
10005|A5|CSR|N
10006|A6|TRANS|Y
10007|A7|FIN|N
Desired Output:
1. Here we want to create files based on Department, i.e. we want to have all the employee of same department in a single file named with that department only.
For example, user 10001 & 10002 should be available in a single file name in the fomr F_<DEPT_NAME>.txt hence for dept Finance file name should be F_FIN.txt
2. Generate files dynamically based on ACTIVE or Inactive status, i.e. all the active employees in a file and all the inactive in another file. File name should follow the following naming F_<Y|N>.txt
Solution 1:
$ awk -F"|" 'NR>1{print > "F_"$3".txt"}' F_Data_File.txt
(or)
$ awk 'NR>1{V_FileName="F_"$1".txt"; print $0 > V_FileName;}' F_Data_File.txt
NR>1 because we don't want to use header line in AWK processing.
Output:
it will create five files, each for one department and will have employee records associated to that department.
F_TRANS.txt
F_MED.txt
F_FIN.txt
F_HR.txt
F_CSR.txt
Explanation:
1. NR>1 ignore the first line which is header line of the base file.
2. Every time when AWK reads a line it re-direct it to the corresponding file internally.
Solution 2:
$ awk -F"|" 'NR>1{if($4=="Y") print > "F_"$4".txt"; else print > "F_"$4".txt" }' F_Data_File.txt
(or)
$ awk -F"|" 'NR>1{V_FileName="F_"$4".txt"; if($4=="Y") print $0 > V_FileName; else print $0> V_FileName }' F_Data_File.txt
NR>1 because we don't want to use header line in AWK processing.
Explanation:
1. NR>1 ignore the first line which is header line of the base file.
2. Here if block is used to check whether $4(FLAG) Y or N, ased on Y & N AWK redirects the processing rows into associated files.
3. Every time when AWK reads a line it re-direct it to the corresponding file internally.
10001|A1|TRANS|Y
10002|A2|MED|Y
10003|A3|FIN|N
10004|A4|HR|Y
10005|A5|CSR|N
10006|A6|TRANS|Y
10007|A7|FIN|N
Desired Output:
1. Here we want to create files based on Department, i.e. we want to have all the employee of same department in a single file named with that department only.
For example, user 10001 & 10002 should be available in a single file name in the fomr F_<DEPT_NAME>.txt hence for dept Finance file name should be F_FIN.txt
2. Generate files dynamically based on ACTIVE or Inactive status, i.e. all the active employees in a file and all the inactive in another file. File name should follow the following naming F_<Y|N>.txt
Solution 1:
$ awk -F"|" 'NR>1{print > "F_"$3".txt"}' F_Data_File.txt
(or)
$ awk 'NR>1{V_FileName="F_"$1".txt"; print $0 > V_FileName;}' F_Data_File.txt
NR>1 because we don't want to use header line in AWK processing.
Output:
it will create five files, each for one department and will have employee records associated to that department.
F_TRANS.txt
F_MED.txt
F_FIN.txt
F_HR.txt
F_CSR.txt
Explanation:
1. NR>1 ignore the first line which is header line of the base file.
2. Every time when AWK reads a line it re-direct it to the corresponding file internally.
Solution 2:
$ awk -F"|" 'NR>1{if($4=="Y") print > "F_"$4".txt"; else print > "F_"$4".txt" }' F_Data_File.txt
(or)
$ awk -F"|" 'NR>1{V_FileName="F_"$4".txt"; if($4=="Y") print $0 > V_FileName; else print $0> V_FileName }' F_Data_File.txt
NR>1 because we don't want to use header line in AWK processing.
Explanation:
1. NR>1 ignore the first line which is header line of the base file.
2. Here if block is used to check whether $4(FLAG) Y or N, ased on Y & N AWK redirects the processing rows into associated files.
3. Every time when AWK reads a line it re-direct it to the corresponding file internally.
No comments:
Post a Comment