'using awk to split a large file

I have a file that looks like this:

##DATA
##D=H|<...>|<...>|...
...
H|233|AAPL|US|1389593.1533|
A|1|113581395.1353|524532||525|1541|a|19191|1919
E|2|413581395.1353|123332||525|1541|c|15333|1533
...
H|233|TSLA|US|133.1533|
A|1|13538815.1353|524532||525|6466|a|686|123
E|2|76976978.1353|123332||525|2343|d|6968|697
...

I want split this file by stocks, e.g. AAPL.txt, and TSLA.txt, and create a separate text file with messages for each stock.

I tried using awk to get all the lines that start with H and get the stock symbol:

awk -F"|" `/^H/ {print $3}' input.txt

but I'm not sure how to get all the messages for each stock.

awk


Solution 1:[1]

Using awk

$ awk -F"|" -v path="/path/to/file/" '/^H/ { close(var); var=path$3".txt" } var { print > var }' file

or if a variable to the path already exist

$ awk -F"|" -v path="$variable" '/^H/ { close(var); var=path$3".txt" } var {print > var}' file

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1