[SOLVED] How to extract the first word following a string
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
crts--I just tried that and wrote in test2:
105
46
You can see where those numbers came from if you look back at the line. Interesting result. Thank you all for staying with me. I really appreciate it.
Try
Code:
sed -nr "s/.*${pattern}[ ]*([^ ]+).*/\1/p" /opt/gamess/tests/exam12.inp > test2
The problem is that \w will get a whole word. The '.' is considered a word boundary and therefor not part of the result. Since you want 46.xxx as result \w is not suitable. This was not obvious from your first post. That is why you should always provide sample data as accurate as possible.
P.S.: If you can be certain that there are no tabs in your file you can omit the tab characters and use only spaces inside [] instead of both.
Sorry, I will try to be clearer. Now I got
105.14088
46.45648
How do I just get the first number?
Based on your sample data
Code:
I tried typing the input by the forum seems to automatically take out extra spaces (obviously ctrs found a way around that). Here is what the particular line would look like:
![3 spaces]Heat OF FORMATION[9 spaces]105.14088[3 spaces]93.45997[3 spaces]46.89387
you already got the first number. The number 46.45648 is from another line. It is not 46.89387 form the same line. Notice, that they are not equal. If you do not want to process anymore lines after the first match then try
Code:
sed -nr "/${pattern}/ {s/.*${pattern}[ ]*([^ ]+).*/\1/p;q}" /opt/gamess/tests/exam12.inp > test2
Maybe it is best if you post your data file as attachment and tell us what data you want extracted.
That worked great! Thank you very much! I wanted to attach my data but could not figure out how. Next time!
When you reply (not quick reply) look out for the "paper-clip" symbol. Keep in mind that attaching is "extension" sensitive. You will have to rename the file like
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.