LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-20-2014, 05:02 PM   #1
tomtweber
LQ Newbie
 
Registered: Feb 2014
Posts: 7

Rep: Reputation: Disabled
Write a script that searches a txt file and write's specific data to a new file


Needs to work in Linux. I want to search for specific text in a .txt file. After finding that text, I want to copy the next 7 characters to a new text document. Then search for some other text and copy the next 17 characters to the same new text file separated by a comma. Then I want it to do a carriage return to search another text file and save the next piece of data. The file being searched will always be named the same and in the same location. The new file will already exist and will always have the same name.

Example:

Existing txt file: sample.txt


"mlb_serial_number"="1234567"
encap:Ethernet HWaddr 37:54:67:8a:66:39


New text file contents: extracted.txt

1234567,37:54:67:8a:66:39



First Run Complete, Start New Run




Existing txt file: sample.txt

"sku_number"="1"
"mlb_serial_number"="7654321"
encap:Ethernet HWaddr 37:54:67:9b:33:64




New text file contents: extracted.txt

1234567,37:54:67:8a:66:39
7654321,37:54:67:9b:33:64

Last edited by tomtweber; 02-20-2014 at 05:05 PM.
 
Old 02-20-2014, 05:10 PM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
what have you tried and where are you stuck ?

i think programs like grep, awk, cut, ... will get you what you need.
 
1 members found this post helpful.
Old 02-20-2014, 09:30 PM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,153

Rep: Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125
Interesting request for a first post - homework ?.
 
Old 02-20-2014, 09:49 PM   #4
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Consider this ...
Code:
awk '{if (substr($0,1,19)=="\"mlb_serial_number\"") print substr($0,22,7);
      if (substr($0,1,21)=="encap:Ethernet HWaddr") print substr($0,23)}'  \
 $InFile >>$OutFile
Daniel B. Martin
 
Old 02-21-2014, 12:52 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
@Daniel - I know you are trying to help, but by providing a solution the user is not learning anything. As would be why none of the first responders have given a solution.
 
1 members found this post helpful.
Old 02-21-2014, 07:36 AM   #6
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by grail View Post
@Daniel - I know you are trying to help, but by providing a solution the user is not learning anything. As would be why none of the first responders have given a solution.
Three years ago I was a complete newbie, floundering in the new (to me) world of Linux. LQ contributors -- you included -- provided help which sometimes included complete solutions. I learned from each and am now able to "give back." Where is the perfect balance point of hints vs working code? If I personally knew each individual OP then better judgements could be made.

Daniel B. Martin
 
Old 02-21-2014, 09:08 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
Fair enough, and I do see your point. I am also very pleased to see how far you have come

I guess personally i just like to see at least some effort from the OPs as has been indicated this does seem contrived as if a homework type question.

As for your solution, I feel the use of substr as part of your test may be overkill as you could have simply used a regex, plus I put it to you that your second
'if' has a far simpler print option than substr
 
Old 02-21-2014, 09:57 AM   #8
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
Quote:
Originally Posted by danielbmartin View Post
Three years ago I was a complete newbie, floundering in the new (to me) world of Linux. LQ contributors -- you included -- provided help which sometimes included complete solutions. I learned from each and am now able to "give back." Where is the perfect balance point of hints vs working code? If I personally knew each individual OP then better judgements could be made.

Daniel B. Martin
my opinion is that you gave too much of a hint especially since the op has shown no effort.
 
Old 02-21-2014, 10:34 AM   #9
tomtweber
LQ Newbie
 
Registered: Feb 2014
Posts: 7

Original Poster
Rep: Reputation: Disabled
It's not a homework assignment. It's a work assignment! I have been tasked with capturing the serial number and MAC address from Dell ChromeBooks. I have already developed a tool to do the same thing, but on Windows based computers via WinPE and VB scripts. I am just not familiar enough with Linux code to pull it off by myself. I can usually look at code and manipulate it to meet my needs. But I could not find anything that would work.

When all is said and done, I need a .csv file with Serial,MAC and a carriage return (or new line) after each entry. What I have done so far is this: Put the ChromeBook in developer mode, get to a shell prompt and log in as root. From there I mount a USB drive, run two commands that capture details about serial and MAC to a text file. That is where I am stuck. Now I need to put it all together and test it. I have played around with and coded in PHP, HTML, Javascript, Perl, MYSQL, VB, and a little C++. My goal here is to create a script and guide that an average tech could run on 1000's of ChromeBooks. Looking at Daniel's suggestion, I want to understand it. So let's break it down shall we....

awk '{if (substr($0,1,19)=="\"mlb_serial_number\"") print substr($0,22,7);
if (substr($0,1,21)=="encap:Ethernet HWaddr") print substr($0,23)}' \
$InFile >>$OutFile

I do not understand any of the numbers in (). Additionally, I only provided a snippet of the actual data in the text file that is created. I thought it would be easier. Basically I thought I would be able to find the text anywhere in the file and then print the next 7 characters after that text. Then put a comma, find the next string of data and print the next 17 characters after that, followed by a new line or carriage return. I am assuming $infile and $outfile are variables I would assign, but I don't see how the awk command gets the data from the original sample.txt file. The file names are irrelevant and was only used as an example. Below are the commands I am using to get to the point where I need assistance as well as the full contents of the text file I create in the process. I want to put steps 7-13 + the current question and then step 14 in a script. After 1-6 are done manually, the script would be ran and the rest would take care of itself. As far as the text file, I need service tag and MAC to be pulled out of the MAC.txt and placed in a new file as:

5DW3426,45:53:03:8a:63:74

Then put the USB in a different Chromebook and repeat the process. After the second time the new file should contain the exising and the new data:

5DW3426,45:53:03:8a:63:74
6DT3622,45:53:03:8a:62:39

1. Power on ChromeBook
2. Press Esc-Refresh+Power
3. At the recovery screen, press Ctrl-D
4. Press enter to turn off OS verification
5. press Ctrl-D to enter developer mode
6. Press Ctrl-Alt-F2 to enter command prompt
7. Type chronos
8. Type sudo bash
9. Type mount -t auto /dev/sdb1 /media/removable
10. Type dump_vpd_log --full --stdout > /media/removable/MAC.txt
11. Type ifconfig wlan0 >> /media/removable/MAC.txt
12. Type umount /dev/sdb1
13. Type eject /dev/sdb1
14. Type crossystem disable_dev_request=1; reboot

MAC.txt full contents:

"initial_locale"="en-US"
"initial_timezone"="America/Los_Angeles"
"keyboard_layout"="xkb:us::eng"
"model_name"="Dell Chromebook 11"
"region"="us"
"serial_number"="5DW3426"
"service_tag"="5DW3426"
"sku_number"="1"
"mlb_serial_number"="QDRMLK7021015248"
"gbind_attribute"="6b359deec451b36741ae43ba14a73690f44642be047aac4b7b47e0bab6a04c5933dd48c9"
"ubind_attribute"="687d337d15a685e8fcbcbcf9967afd25670046133a6d29727c79d195ba0925d76afa36ce"
"ActivateDate"="2014-07"
wlan0 Link encap:Ethernet HWaddr 45:53:03:8a:63:74
UP BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)

Last edited by tomtweber; 02-21-2014 at 10:37 AM.
 
Old 02-21-2014, 10:49 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
Please use [code][/code] tags around code or data to make it readable and maintain formatting
 
Old 02-21-2014, 11:07 AM   #11
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
kind of a moving target... do you see how the string encap:Ethernet HWaddr is not the same as wlan0 Link encap:Ethernet HWaddr
and mlb_serial_number seems to be not what you are extracting (serial_number ?)

therefore dan martins awk command wont work on your 2nd post.

does chromebook have a way to ssh into them so that you wouldnt have to sneakernet to each one ?

Last edited by schneidz; 02-21-2014 at 11:19 AM.
 
Old 02-21-2014, 11:24 AM   #12
Shadowmeph
Member
 
Registered: May 2008
Location: West Coast Canada
Posts: 282

Rep: Reputation: 29
Quote:
Originally Posted by danielbmartin View Post
Consider this ...
Code:
awk '{if (substr($0,1,19)=="\"mlb_serial_number\"") print substr($0,22,7);
      if (substr($0,1,21)=="encap:Ethernet HWaddr") print substr($0,23)}'  \
 $InFile >>$OutFile
Daniel B. Martin
where can I find out what this actually does, I know that the if really means if and the (substr) probably means substring? but what is this ($0,1,2)==( is the == mean equal to?)
I actually would lke to learn this just to learn it.
 
Old 02-21-2014, 11:25 AM   #13
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by tomtweber View Post
I do not understand any of the numbers in ().
Okay, this time only a hint.

Google is your friend. Do a Google search on awk substr and get lots of hits.
This one ... http://www.grymoire.com/Unix/Awk.html ... is excellent.

Daniel B. Martin
 
Old 02-21-2014, 11:30 AM   #14
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by Shadowmeph View Post
where can I find out what this actually does, I know that the if really means if and the (substr) probably means substring? but what is this ($0,1,2)==( is the == mean equal to?)
Google is your friend. Do a Google search on awk ==
You will get lot of hits. This one ... http://www.grymoire.com/Unix/Awk.html ... is excellent.

== is a relational operator.
= is a value assignment.

Daniel B. Martin
 
Old 02-21-2014, 11:37 AM   #15
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
Quote:
Originally Posted by schneidz View Post
...
does chromebook have a way to ssh into them so that you wouldnt have to sneakernet to each one ?
this worx for me since i have passwordless keys setup on my xbmc box:
Code:
[schneidz@hyper ~]$ ssh schneidz@xbmc ifconfig wlan0 | awk '/Ethernet  HWaddr/ {printf("%s,", $5)}; /inet6 addr/ {printf("%s\n", $3)}' >> tom.txt
[schneidz@hyper ~]$ cat tom.txt 
70:1a:04:96:ef:53,fe80::721a:4ff:fe96:ef53/64

Last edited by schneidz; 02-21-2014 at 11:39 AM.
 
  


Reply



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
[SOLVED] How to write a .sh script to read configuration file and write into .txt file ISStaras Linux - Newbie 8 09-06-2012 06:03 PM
[SOLVED] how do u write a script that searches files by date? Chupakabra Linux - Newbie 7 07-26-2011 05:56 PM
[SOLVED] write file to a specific sector wiener Linux - General 8 06-28-2010 04:59 PM
how to get the specific text from a txt file in bash script deepakdeore2004 Programming 8 04-30-2010 06:35 AM
write text to specific location in file mcbenus Linux - Desktop 3 02-28-2008 06:40 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:32 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration