While awk
is fine for this problem, you really should also understand how to read the file in a shell script and use parameter expansions to trim the unwanted text from each line to isolate the state and capital and write each to their respective files.
It is a basic bread and butter part of shell scripting. (and quite easy here) For example:
#!/bin/bash
states=${2:-states} ## states as 2nd argument (default "states")
capitals=${3:-capitals} ## capitals as 3rd argument (default "capitals")
:>$states ## truncate both files
:>$capitals
while read -r line || [ -n "$line" ]; do
echo "${line%,*}" >> "$states" ## trim line from right to 1st comma
echo "${line#*,}" >> "$capitals" ## trim line from left to 1st comma
done < "$1"
(note: the script reads from the filename provided as the first argument to the program and writes to the state and capital files optionally provided as the 2nd and 3rd arguments)
Example Input File
$ cat file
Alabama,Montgomery
Alaska,Juneau
Arizona,Phoenix
Arkansas,Little Rock
California,Sacramento
Colorado,Denver
Example Use
$ bash separate.sh file
Resulting Output Files
States:
$ cat states
Alabama
Alaska
Arizona
Arkansas
California
Colorado
Capitals:
$ cat capitals
Montgomery
Juneau
Phoenix
Little Rock
Sacramento
Denver
awk
will be faster, but the script above will be orders of magnitude more efficient than your original attempt that spawns multiple subshells per-iteration piping output to cut
. Look things over and let me know if you have further questions.
Adding The Combined File
I guess you would also want a combined file for both state and capital on separate lines. Simply add another file for the output, e.g.
#!/bin/bash
states=${2:-states} ## states as 2nd argument (default "states")
capitals=${3:-capitals} ## capitals as 3rd argument (default "capitals")
combined=${4:-combined} ## combined as 4th argument (default "combined")
:>$states ## truncate all files
:>$capitals
:>$combined
while read -r line || [ -n "$line" ]; do
echo "${line%,*}" >> "$states" ## trim line from right to 1st comma
echo "${line#*,}" >> "$capitals" ## trim line from left to 1st comma
printf "%s\n%s\n" "${line%,*}" "${line#*,}" >> "$combined"
done < "$1"
(note: adding || [ -n "$line" ]
to your while
loop condition will handle the last line without a POSIX end-of-file ('\n'
at end of last line))
Resulting Output Files
Combined:
$ cat combined
Alabama
Montgomery
Alaska
Juneau
Arizona
Phoenix
Arkansas
Little Rock
California
Sacramento
Colorado
Denver