This will create a new file for each matched pattern with an extension of .splt in the current working directory and write all matching lines to it:
sed in a shell for loop:
for i in "10830110" "1083021" "10840110" "1088022100" "10850110" "1085022100" "1086022100" # Patterns to match
do
sed -n "/^$i/p" FileName > "$i.splt" # Change "FileName" to your file name
done
You can do the same, as well, with awk in a shell for loop:
for i in "10830110" "1083021" "10840110" "1088022100" "10850110" "1085022100" "1086022100" # Patterns to match
do
awk -v pat="^$i" -v pat2="$i" '$0 ~ pat { print $0 > pat2".splt"}' FileName # Change "FileName" to your file name
done
awk with an array of patterns:
awk '{pat["0"] = "10830110";
pat["1"] = "1083021";
pat["2"] = "10840110";
pat["3"] = "1088022100";
pat["4"] = "10850110";
pat["5"] = "1085022100";
pat["6"] = "1086022100";} {for (i in pat) { if ($0 ~ "^"pat[i]) print $0 > pat[i]".splt"}}' YourFile
or save patterns as lines(each pattern on a new line) in a pat.txt file and let awk build the array of patterns like so:
awk 'FILENAME=="pat.txt" { pat[$i]=$0; next } { for (i in pat) { if ($0 ~ "^"pat[i]) print $0 > pat[i]".splt"}}' pat.txt YourFile
Speed test (for science)
I tested the solutions(tested 3 times each and took the rounded average) provided in my answer as well as in the answers by @steeldriver and @Sheldon and here are the results(tested on the same average specifications PC) with the same patterns pat.txt contains:
$ cat pat.txt
10830110
1083021
10840110
1088022100
10850110
1085022100
1086022100
and the data file file.dat is a 1.1G containing 8,484,000 lines which is made by duplicating the lines in the example provided by the OP i.e.:
1083021106e581c71003b987a75f18543cf5858b9fcfc5e04c0dddd79cd18764a865ba86d027de6d1900dc171e4d90a0564abbce99b812b821bd0d7d37aad72ead19c17
10840110dbd43121ef0c51a8ba62193eac247f57f1909e270eeb53d68da60ad61519f19cfb0511ec2431ca54e2fcabf6fa985615ec06def5ba1b753e8ad96d0564aa4c
1084011028375c62fd132d5a4e41ffef2419da345b6595fba8a49b5136de59a884d878fc9789009843c49866a0dc97889242b9fb0b8c112f1423e3b220bc04a2d7dfbdff
10880221005f0e261be654e4c52034d8d05b5c4dc0456b7868763367ab998b7d5886d64fbb24efd14cea668d00bfe8048eb8f096c3306bbb31aaea3e06710fa8c0bb8fca71
108501103461fca7077fc2f0d895048606b828818047a64611ec94443e52cc2d39c968363359de5fc76df48e0bf3676b73b1f8fea5780c2af22c507f83331cc0fbfe6ea9
1085022100a4ce8a09d1f28e78530ce940d6fcbd3c1fe2cb00e7b212b893ce78f8839a11868281179b4f2c812b8318f8d3f9a598b4da750a0ba6054d7e1b743bb67896ee62
1086022100638681ade4b306295815221c5b445ba017943ae59c4c742f0b1442dae4902a56d173a6f859dc6088b6364224ec17c4e2213d9d3c96bd9992b696d7c13b234b50
The results are ordered from the fastest on top and the code I used for timing is provided under each result:
#1 grep in a shell loop @Sheldon (18 seconds)
s=$(date +%s); while read prefix
do
grep "^${prefix}" file.dat > ${prefix}.splt
done < pat.txt; e=$(date +%s); echo $(($e-$s))
More accurate timing:
$ time (while read prefix
do
grep "^${prefix}" file.dat > ${prefix}.splt
done < pat.txt)
real 0m17.969s
user 0m4.437s
sys 0m2.176s
#2 sed @steeldriver (20 seconds)
s=$(date +%s); sed 's:.*:/&/w&.splt:' pat.txt | sed -n -f - file.dat; e=$(date +%s); echo $(($e-$s))
More accurate timing with ^ added in response to the comment by @terdon:
$ time (sed 's:.*:/^&/w&.splt:' pat.txt | sed -n -f - file.dat)
real 0m18.748s
user 0m10.408s
sys 0m1.546s
#3 sed in a shell loop @Raffa (21 seconds)
s=$(date +%s); for i in "10830110" "1083021" "10840110" "1088022100" "10850110" "1085022100" "1086022100" # Patterns to match
do
sed -n "/^$i/p" file.dat > "$i.splt" # Change "FileName" to your file name
done; e=$(date +%s); echo $(($e-$s))
#4 awk in a shell loop @Raffa (35 seconds)
s=$(date +%s); for i in "10830110" "1083021" "10840110" "1088022100" "10850110" "1085022100" "1086022100" # Patterns to match
do
awk -v pat="^$i" -v pat2="$i" '$0 ~ pat { print $0 > pat2".splt"}' file.dat # Change "FileName" to your file name
done; e=$(date +%s); echo $(($e-$s))
#5 awk @Raffa (414 seconds) <-- That was a shock
s=$(date +%s); awk '{pat["0"] = "10830110";
pat["1"] = "1083021";
pat["2"] = "10840110";
pat["3"] = "1088022100";
pat["4"] = "10850110";
pat["5"] = "1085022100";
pat["6"] = "1086022100";} {for (i in pat) { if ($0 ~ "^"pat[i]) print $0 > pat[i]".splt"}}' file.dat; e=$(date +%s); echo $(($e-$s))