Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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


Aaron,Kathryn,Caitlin,Harris;New York
share|improve this question
Please show us the actual text file, the devil's in the details with this kind of thing. – terdon Feb 28 '14 at 3:54
I gave an example now @terdon – Jenny Feb 28 '14 at 3:59

There are two general approaches.

  1. 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
  2. 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


share|improve this answer

And also just cut ..

cut -d\; -f1 file
share|improve this answer

Here's an answer with sed -- since you're not really doing any field processing, awk is probably overkill.

sed 's/;.*//'
share|improve this answer
+1 but based on the OP's comments, I am assuming this is all part of a larger script. @Jenny, that's the kind of detail you should include in your questions by the way. – terdon Feb 28 '14 at 4:13

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.