How to use command grep,cut,awk to cut a data from a file?
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.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How to use command grep,cut,awk to cut a data from a file?
Hi, i have a file inside there have a infomation, may i know how to using suitable command to select which data i want in my data file. I show my example data file=file1.txt look like this:
# cat -n file1.txt
1
2 BSSID, First time seen, Last time seen, channel, Speed, Privacy,
3 00:1B:70:A1:33:22, 2008-09-11 16:20:25, 2008-09-11 16:33:44, 11,
4. 00:62:B5:C9:03:63, 2008-09-11 16:20:25, 2008-09-11 16:33:45, 11,
5. 00:19:4D:3E:35:0E, 2008-09-11 16:20:25, 2008-09-11 16:33:44, 11,
If i want to take out "line"=3 , only want to take out "data"=00:1B:70:A1:33:22 i should using what command, i dont want to type a long MAC address:00:1B:70:A1:33:22, is it posible to using command to take out this specify info "00:1B:70:A1:33:22" by no need to type this long MAC address, i have try to using command cut:
Allow me to explain what this does. "tail -n 3" prints the last 3 lines of the file and then "head -n 1" prints out the first line of that output (which is the line you want). awk splits each line (called a record) into fields, separated by whitespace, so the "awk '{print $1}'" prints out the first field, which is the MAC address with a comma at the end. I use awk in the same way again, this time specifying the comma as the separator.
after get the output, how to write to file and how to write to variable?
Thank Nylex and David the H.
Ya, the both command also can be use, thank u very much.
ok, now i obtain what output i want, than how to write this output to file= /home/testing/output.txt , or i want to write to my varible $var1 on my script? than i can call this file or varible to use.
1. How to store my this output from cut -d "," -f 1 file1.txt | sed -n 3p to my variable $var1
2. How to store my this output from cut -d "," -f 1 file1.txt | sed -n 3p > output.txt , than the output copy to filename output.txt, than how to store my variable $var1 from this file output.txt
This is wrong unless you set $var1 to be the name of the file.
Use "var1=`cut -d "," -f 1 file1.txt | sed -n 3p` instead. It would be better to use "sed -n 3p" first. Then the cut command has less to do. You could use "sed -n '3p;4q'" so that the sed command quits after the third line is read. That can save a lot of time if you have a long output.
Code:
sed -n '3p;4q' file1.txt | cut -d "," -f 1
Doing it this way, only 3 lines are read by sed and one line is processed by cut instead of the entire file.
This may work better:
Code:
var1=$(sed -n '1,/^BSSID/d;s/, .*$//;3p;4q' testf | tee output.txt)
The tee command copies STDOUT to a file. The standard output is assigned to the variable because of the $() around the command.
The form $( ... ) is the same as using backticks. You can also have one $(...) embedded in another. You will see both forms in use.
---
On second thought, I'm wondering if the first blank line is atypical. You may want to use:
Code:
var1=$(sed '1,/BSSID/d;s/, .*$//;q' file1.txt | tee output.txt)
instead. This will print the first MAC address after the header line and quit. If a program works the same way you would clearly describe it, that tends to be a good sign.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.