LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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

Reply
 
Search this Thread
Old 09-11-2008, 11:05 AM   #1
hocheetiong
Member
 
Registered: Jul 2007
Location: Penang , Malaysia.
Distribution: red hat linux
Posts: 133

Rep: Reputation: 15
Smile 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...
 
Old 09-11-2008, 11:30 AM   #2
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
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.

Last edited by Nylex; 09-11-2008 at 11:37 AM.
 
Old 09-11-2008, 11:52 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
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.

Last edited by David the H.; 09-11-2008 at 11:55 AM.
 
Old 09-11-2008, 01:43 PM   #4
hocheetiong
Member
 
Registered: Jul 2007
Location: Penang , Malaysia.
Distribution: red hat linux
Posts: 133

Original Poster
Rep: Reputation: 15
Smile 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.
 
Old 09-11-2008, 04:42 PM   #5
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
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).
 
Old 09-11-2008, 05:27 PM   #6
hocheetiong
Member
 
Registered: Jul 2007
Location: Penang , Malaysia.
Distribution: red hat linux
Posts: 133

Original Poster
Rep: Reputation: 15
Smile 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
 
Old 09-11-2008, 07:06 PM   #7
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,269

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
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.
 
Old 09-11-2008, 07:16 PM   #8
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
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.

Last edited by jschiwal; 09-11-2008 at 07:37 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
grep and cut command tanveer Linux - General 3 09-05-2008 02:17 AM
PHP + bash: grep, cut or explode ???? :S ALInux Programming 3 12-13-2005 01:11 PM
cut , paste or grep ziox Programming 1 12-15-2004 10:51 PM
tcprobe+grep+cut Axion Programming 1 08-24-2004 05:27 AM
cut / awk command?? Sammy2ooo Linux - Newbie 1 05-27-2003 05:46 PM


All times are GMT -5. The time now is 02:04 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration