How can I use awk to remove all text after a certain character ; that appears on every line of my text file? (I then need to run for loops on the text)
Jenny,Sarah,John;North Dakota Henry,Frank;Illinois Aaron,Kathryn,Caitlin,Harris;New York There are two general approaches.
Set awk's field separator to that character. You can then get the parts you want as $1:
$ echo "Today was cloudy; yesterday too" | awk -F';' '{print $1}' Today was cloudy Use gsub() to substitute it with an empty string:
$ echo "Today was cloudy; yesterday too" | awk '{sub(/;.*/,""); print}' Today was cloudy So, for your example:
$ awk -F';' '{print $1}' file Jenny,Sarah,John Henry,Frank Aaron,Kathryn,Caitlin,Harris Here's an answer with sed -- since you're not really doing any field processing, awk is probably overkill.
sed 's/;.*//' Sometimes you may want to replace all characters after a certain word with another string. For example:
original_string="abc blabla foo bar" and you want to replace words after blabla with 'hello world'
echo $original_string | sed -E 's/(.+ blabla) .+/\1 hello world/' Using Raku (formerly known as Perl_6):
raku -pe 's:g/ \; .*? $$//;' OUTPUTS:
Jenny,Sarah,John Henry,Frank Aaron,Kathryn,Caitlin,Harris The above code implements the command line -pe linewise-autoprinting flags, in conjunction with the well-known s/// substitution construct. The code tells Raku to :g globally search for a ;, identify .*? 0-or-more characters that follow (? means non-greedily), up to the end-of-line ($$).
(Actually, since the OP seems to indicate that the ; only occurs once-per-line, the :g can be omitted. Also, since the -pe command line linewise-autoprinting flags are in use, you can use the $ end-of-string assertion, instead of the $$ end-of-line assertion).
The OP seems to indicate that he/she will be running for-loops over the text. This sounds like a simple comma-separated list of names is desired? If so, the following code works:
raku -e 'lines.grep(*.chars).map(*.subst(/\; .*? $$/)).join(",").put;' OUTPUTS:
Jenny,Sarah,John,Henry,Frank,Aaron,Kathryn,Caitlin,Harris