I am new to AWK.
Can you please let me know how to read a line from file using awk? How can I get a value using substring function from read line and store it into a variable? For example here is the data set:
01 001 410070300186169 359829047319420
01 002 410070234186169 359829043245420
01 001 410070234186169 359829047319420
I want to check if at position 4 to 6 , value is “001” then write the data in file file_1 and if the value is “002” then write the data in file file_2.
Here you go; you can put this all on one line, but I’ve broken it down into multiple for readability:
Explanation:
awkseparates each line into fields using spaces and tabs as delimiters by default, so for each line read, values in your 2nd column will be saved in$2$2=="001" {print > "file_1"}: if$2is001, print the entire line intofile_1$2=="002" {print > "file_2"}: likewise for002