Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Finding all strings in a file that start with ___

I have a file that looks like this

ifconfig-push 123.456.789.000 123.456.789.111

Is it possible to use grep or awk to print only the first string that start with 123 and not the second 123?

I want the output to look like 123.456.789.000 and have multiple files where this first IP address is different but always starts with 123

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

By default awk will separate values in between spaces. This means that if each line of your input will be similar to the sample you provided, the 2nd field would be your target. You can also add pattern matching to check for 123

awk '/123/ {print $2}' inputfile.txt
  • /123/ This is a check for 123 being on the line
  • {print $2} This prints the second field on that line
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading