LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to use command grep,cut,awk to cut a data from a file? (http://www.linuxquestions.org/questions/linux-newbie-8/how-to-use-command-grep-cut-awk-to-cut-a-data-from-a-file-669239/)

hocheetiong 09-11-2008 11:05 AM

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:

# cut -d " " -f 1 file1.txt
OUTPUT:

BSSID,
00:1B:70:A1:33:22,
00:62:B5:C9:03:63,
00:19:4D:3E:35:0E,

So this result is nearly my request, but i want just only line=3 DATA=00:1B:70:A1:33:22 only, because later i want to save it to my $varible.

Thank u very much...

Nylex 09-11-2008 11:30 AM

A somewhat cumbersome way to do it:

tail -n 3 file1.txt | head -n 1 | awk '{print $1}' | awk -F ',' '{print $1}'

I'm sure there's a better way, though :).

Edit:

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.

David the H. 09-11-2008 11:52 AM

Sed can be used to extract a single line of output:

Code:

cut -d "," -f 1 file1.txt | sed -n 3p
Note that I used a comma as the cut field separator instead of a space in order to grab only the value.

hocheetiong 09-11-2008 01:43 PM

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.

Thank again.

Nylex 09-11-2008 04:42 PM

If you want to write the output to a file, you can just use '>', e.g.

cut -d "," -f 1 file1.txt | sed -n 3p > file.

Also, if you want to store the output in a variable, you can use backticks (`), e.g.

var=`cut -d "," -f 1 file1.txt | sed -n 3p`,

though I think there's another way to do that (i.e. without using backticks).

hocheetiong 09-11-2008 05:27 PM

how to store variable to my variable $var1?
 
Hi, i have use command:

var=`cut -d "," -f 1 file1.txt | sed -n 3p`



but i want my this output "MAC address" store at my variable name $var1, i have try using command:

cut -d "," -f 1 file1.txt | sed -n 3p > $var1

but having error.


cut -d "," -f 1 file1.txt | sed -n 3p > output.txt
cat output.txt > $var1

also having error.




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

chrism01 09-11-2008 07:06 PM

If you look at Nylex's soln, you only use '>' to output to a file. To output to a var use

var1=`cut -d "," -f 1 file1.txt | sed -n 3p`

If you have a problem, please don't just say 'having error'. You have to show us exactly what cmd you ran and exactly what the error msg is.

jschiwal 09-11-2008 07:16 PM

Quote:

> $var1
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.


All times are GMT -5. The time now is 02:34 AM.