简体   繁体   中英

How do I extract lines out of a text file using bash in linux?

I would like to extract any line that contains a MAC addresses out of a text file in linux using bash scripting, and then save it into another if this is possible?

There different examples of using sed, and grep to remove lines, but I have not been able to make them work for me so far. I am not very good with programming in general so it's probably done a lot easier then I think it can be.

Example of text file below that I am extracting from.

cat Test.txt

spawn ssh -l user x.x.x.x -p 22 "arp"
DD-WRT Mega
Release xx/xx/xx (SVN revison:xx)
root@x.x.x.x password:
Device1 (x.x.x.x) at xx:xx:xx:xx:xx:xx [ether] on br0
Device2 (x.x.x.x) at xx:xx:xx:xx:xx:xx [ether] on br0
Device3 (x.x.x.x) at xx:xx:xx:xx:xx:xx [ether] on br0

Below is what I am looking for as a result.

Result.txt

Device1 (x.x.x.x) at xx:xx:xx:xx:xx:xx [ether] on br0
Device2 (x.x.x.x) at xx:xx:xx:xx:xx:xx [ether] on br0
Device3 (x.x.x.x) at xx:xx:xx:xx:xx:xx [ether] on br0

If anyone knows how to extract the lines containing the MAC Addresses out of one file and send them to a another text file through a bash script that would be a great help.

Thank-you

It's probably simplest to use grep :

grep -E '(..:){5}..' < infile.txt > outfile.txt

It's worth noting this is a little relaxed in terms of enforcing matches as opposed to being strict that only a valid mac address is matched, but this expression is also a good bit simpler and will most likely suffice.

尝试:

grep -E "(..:){5}.." file

尝试这个:

grep '([0-9A-F]{2}[:-]){5}([0-9A-F]{2})' < in.txt > out.txt

尝试这样做,这是最可靠,最强大的解决方案=)

grep -E '\b([[:xdigit:]]{2}:){5}[[:xdigit:]]{2}\b' < file > newfile

well for starters you can use grep, then you can move onto sed, then you can learn awk. Seeing this is your first question, posting it sure took more effort than google the question :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM