Input File: F_Data_File.txt
EMPID|EMPNAME|EMPDEPT|EMPSAL|LOCATION
10001|A1|HR|10000|USA
10002|A2|FIN|20000|USA
10003|A3|NSS|30000|IND
10004|A4|SEC|40000|USA
10005|A5|TECH|50000|IND
10006|A6|TECH|60000|IND
10007|A7|TECH|70000|IND
1. We want to swap location with Salary means now location column sould be the 4th column which is 5th in original file.
$ awk 'BEGIN{FS="|";OFS="|";}{print $1 OFS $2 OFS $3 OFS $5 OFS $4}' F_Data_File.txt
(or)
$ awk 'BEGIN{FS="|";OFS="|";}{print $1 OFS $2 OFS $3 OFS $5 OFS $4}' F_Data_File.txt > F_Temp_File.txt
We have redirected the output of the awk command into a file for permanent changes as below:
Output:
EMPID|EMPNAME|EMPDEPT|LOCATION|EMPSAL
10001|A1|HR|USA|10000
10002|A2|FIN|USA|20000
10003|A3|NSS|IND|30000
10004|A4|SEC|USA|40000
10005|A5|TECH|IND|50000
10006|A6|TECH|IND|60000
10007|A7|TECH|IND|70000
2. We want to remove salary column from the input data.
$ awk 'BEGIN{FS="|";OFS="|";}{print $1 OFS $2 OFS $3 OFS $5 OFS $4}' F_Data_File.txt
(or)
$ awk 'BEGIN{FS="|";OFS="|";}{print $1 OFS $2 OFS $3 OFS $5}' F_Data_File.txt > F_Temp_File.txt
Output:
EMPID|EMPNAME|EMPDEPT|LOCATION
10001|A1|HR|USA
10002|A2|FIN|USA
10003|A3|NSS|IND
10004|A4|SEC|USA
10005|A5|TECH|IND
10006|A6|TECH|IND
10007|A7|TECH|IND
3. Concatenate two fields, let say EMPNAME & EMPDEPT and create a new column EMPNAME-DEPT.
$ awk 'BEGIN{FS="|";OFS="|";print "EMPID|EMPNAME-DEPT|LOCATION"} NR>1{print $1 OFS $2"-"$3 OFS $4 OFS $5}' F_Data_File.txt
(or)
$awk 'BEGIN{FS="|";OFS="|";}{print $1 OFS $2"-"$3 OFS $4 OFS $5}' F_Data_File.txt > F_Temp_File.txt
As we have concatenated two columns hence data will be available in new column but header still have 5 column names. To update the header we have initialize the header in BEGIN block and eliminated the first line from the existing file.
Output:
EMPID|EMPNAME-DEPT|LOCATION
10001|A1-HR|10000|USA
10002|A2-FIN|20000|USA
10003|A3-NSS|30000|IND
10004|A4-SEC|40000|USA
10005|A5-TECH|50000|IND
10006|A6-TECH|60000|IND
10007|A7-TECH|70000|IND
EMPID|EMPNAME|EMPDEPT|EMPSAL|LOCATION
10001|A1|HR|10000|USA
10002|A2|FIN|20000|USA
10003|A3|NSS|30000|IND
10004|A4|SEC|40000|USA
10005|A5|TECH|50000|IND
10006|A6|TECH|60000|IND
10007|A7|TECH|70000|IND
1. We want to swap location with Salary means now location column sould be the 4th column which is 5th in original file.
$ awk 'BEGIN{FS="|";OFS="|";}{print $1 OFS $2 OFS $3 OFS $5 OFS $4}' F_Data_File.txt
(or)
$ awk 'BEGIN{FS="|";OFS="|";}{print $1 OFS $2 OFS $3 OFS $5 OFS $4}' F_Data_File.txt > F_Temp_File.txt
We have redirected the output of the awk command into a file for permanent changes as below:
Output:
EMPID|EMPNAME|EMPDEPT|LOCATION|EMPSAL
10001|A1|HR|USA|10000
10002|A2|FIN|USA|20000
10003|A3|NSS|IND|30000
10004|A4|SEC|USA|40000
10005|A5|TECH|IND|50000
10006|A6|TECH|IND|60000
10007|A7|TECH|IND|70000
2. We want to remove salary column from the input data.
$ awk 'BEGIN{FS="|";OFS="|";}{print $1 OFS $2 OFS $3 OFS $5 OFS $4}' F_Data_File.txt
(or)
$ awk 'BEGIN{FS="|";OFS="|";}{print $1 OFS $2 OFS $3 OFS $5}' F_Data_File.txt > F_Temp_File.txt
Output:
EMPID|EMPNAME|EMPDEPT|LOCATION
10001|A1|HR|USA
10002|A2|FIN|USA
10003|A3|NSS|IND
10004|A4|SEC|USA
10005|A5|TECH|IND
10006|A6|TECH|IND
10007|A7|TECH|IND
3. Concatenate two fields, let say EMPNAME & EMPDEPT and create a new column EMPNAME-DEPT.
$ awk 'BEGIN{FS="|";OFS="|";print "EMPID|EMPNAME-DEPT|LOCATION"} NR>1{print $1 OFS $2"-"$3 OFS $4 OFS $5}' F_Data_File.txt
(or)
$awk 'BEGIN{FS="|";OFS="|";}{print $1 OFS $2"-"$3 OFS $4 OFS $5}' F_Data_File.txt > F_Temp_File.txt
As we have concatenated two columns hence data will be available in new column but header still have 5 column names. To update the header we have initialize the header in BEGIN block and eliminated the first line from the existing file.
Output:
EMPID|EMPNAME-DEPT|LOCATION
10001|A1-HR|10000|USA
10002|A2-FIN|20000|USA
10003|A3-NSS|30000|IND
10004|A4-SEC|40000|USA
10005|A5-TECH|50000|IND
10006|A6-TECH|60000|IND
10007|A7-TECH|70000|IND
No comments:
Post a Comment