I have a file in which the values are stored as below.

Assistant Professor at SASTRA University
St John's memorial college
Paul George Univeristy
Department Chair at University of Texas

I am extracting all the characters after the first at using the below command.

grep -o ' at .*$' checkin.txt | cut -c5-

Now, I am trying to get the output as,

SASTRA University
St John's memorial college
Paul George University
University of Texas

This is the script I have.

while read -r line; do
echo $line
done < <( grep -o ' at .*$' checkin.txt | cut -c5- )

However, the script gives me the below output.

SASTRA University
University of Texas

How can I extract all the values?

  • 1
    It really would be much easier if you posted one question showing your initial input and final output. I appreciate the effort you are putting into each question and that's great but we could have solved the whole thing in one go which would have been better for you. – terdon Feb 10 '14 at 23:53
  • @Terdon, Sorry for posting multiple questions. I thought if I split into multiple questions, it would be easier for viewing the question. Anyways, I will keep this in mind and post as a single question from next time. – Ramesh Feb 11 '14 at 0:00
  • Don't worry about it, seriously, I can see the effort you put into making your questions clear and I appreciate it. It would just be much easier for you and also, it would help us understand the whole problem. Asking for small pieces of the puzzle like this is likely to lead to XY problems. – terdon Feb 11 '14 at 0:01

The problem here is that not all lines have at. Try this instead:

$ sed 's/.* at //'  file 
SASTRA University
St John's memorial college
Paul George Univeristy
University of Texas

If you can have mutiple ats, use Perl instead:

$ perl -pe 's/.+? at //' file 
| improve this answer | |
  • Saw you answered after I hit, post it. Deleting my almost identical A. Thanks for the edit BTW, earlier today! – slm Feb 11 '14 at 0:04
  • @slm great minds :) and you're very welcome. – terdon Feb 11 '14 at 0:09
  • @terdon, It prints all the lines correctly. But in lines that have more than one "at", it takes the last at and gives me the value only after the last at. – Ramesh Feb 11 '14 at 0:23
  • @Ramesh see updated answer. – terdon Feb 11 '14 at 0:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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