[SOLVED] Write a script that searches a txt file and write's specific data to a new file
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
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 ?
ssh works over wifi as demonstrated by my previous post about ssh'ing to my xbmc box... it seems like the sshd server daemon is disabled in chrome-os by default and it would take some hacking to re-enable (mite not be worth it).
so it seems like you have what you need to script up something that can strip out the wanted text and redirect it to a file ?
Thanks to danielbmartin and schneidz I am very close to a working solution. Seeing as the data in the file could vary, searching by the number of characters from the beginning as offered by danielbmartin will not work. Searching just for the REGEX does not seem to be a valid solution either. But based on those two examples (and a lot of research on Google) I was able to come up with what I think will work. Please let me know if the following code is correctly formatted and whether it would work properly. I would place the script on a USB flashdrive. The tech would perform steps 1-12. At that point the script should run and output the service tag and WiFi MAC address to MAC.csv. Additionally as far as steps 8-12, can any of those steps be part of the script or is there a method of accomplishing the tasks is an easier way?
1. Plug in USB flashdrive
2. Power on ChromeBook
3. Press Esc-Refresh+Power
4. At the recovery screen, press Ctrl-D
5. Press enter to turn off OS verification
6. press Ctrl-D to enter developer mode
7. Press Ctrl-Alt-F2 to enter command prompt
8. Type chronos
9. Type sudo bash
10. Type mount -t auto /dev/sdb1 /media/removable
11. Type Chmod 755 /media/removable/MAC.sh
12. Type ./media/removable/MAC.sh
Code:
#!/bin/bash
#MAC.sh Capture the service tag and MAC address to a file
clear
infile=/media/removable/data.txt #Sets data.txt as the source data file
outfile=/media/removable/MAC.csv #Sets MAC.csv as the destination data file
dump_vpd_log --full --stdout > $infile #Writes system summary to data.txt
ifconfig wlan0 >> $infile #Appends WIFI info to data.txt
awk 'match($0, /service_tag/) { #Finds the service tag and assigns it to variable serial
serial=substr($0,RSTART+RLENGTH+3,7)
print $serial","
}
;
match($0, /Ethernet HWaddr/) { #Finds the MAC address and assigns it to variable MAC
MAC=substr($0,RSTART+RLENGTH+1,17)
print $MAC\n
}
' $infile >> $outfile #Uses data.txt as the AWK input and outputs the AWK results to MAC.CSV
umount /dev/sdb1 #Unmounts the USB flashdrive
eject /dev/sdb1 #Safely ejects the USB flashdrive
crossystem disable_dev_request=1; reboot #Removes the ChromeBook from developer mode and reboots the system
... searching by the number of characters from the beginning as offered by danielbmartin will not work.
The details of your overall task are way over my head. However, I suggest you become familiar with the Built-In Function index.
Code:
index(in, find)
This searches the string in for the first occurrence of the string find,
and returns the position in characters where that occurrence begins in the string in.
You may find this helpful in writing string-handling code of this nature ...
I finally have a fully functioning, tested and working, script. Thanks to everyone for their expertise and feedback.
Code:
#!/bin/bash
# MAC.sh Capture the service tag and MAC address to a CSV file named MAC.csv
#Clears the terminal screen
clear
#Erases all variables before starting
servicetag=''
mac=''
infile=''
outfile=''
size=''
#Sets MAC.txt as the source data file
infile=/media/ubuntu/Win7/Dell/MAC.txt
#Sets MAC.csv as the destination data file
outfile=/media/ubuntu/Win7/Dell/MAC.csv
#Writes system summary to MAC.txt
# dump_vpd_log --full --stdout > $infile
#Appends BIOS read serial number to MAC.txt
dmidecode -s system-serial-number >> $infile
#Finds the BIOS detected serial number and assigns it to variable servicetag3
servicetag3=$(dmidecode -s system-serial-number)
#Finds the Linux detected MAC address and assigns it to variable mac3
mac3=$(cat /sys/class/net/eth0/address)
mac3=$(echo $mac3 | tr '[:lower:]' '[:upper:]')
#Appends WIFI info to data.txt
# ifconfig wlan0 >> $infile
#Finds the service tag and assigns it to variable servicetag
servicetag=$(awk 'match($0,/service_tag/) {servicetag2=substr($0,RSTART+RLENGTH+3,7); print servicetag2}' $infile)
#Finds the MAC address and assigns it to variable mac
mac=$(awk 'match($0,/Ethernet HWaddr/) {mac2=substr($0,RSTART+RLENGTH+1,17); print mac2}' $infile)
#Shows the service tag and WiFi MAC address
echo "Service Tag 3 is" $servicetag3" and MAC 3 is" $mac3". The service tag is" $servicetag". The WiFi MAC is" $mac". The following has been written to MAC.csv: "$servicetag","$mac "\n The ChromeBook will exit developer mode and reboot in 10 seconds. Do not turn off the computer, please wait."
#Checks if MAC.csv exists, if not, creates file and writes spreadsheet headers
[ -f $outfile ] || echo "Service Tag,WiFi MAC Address" > $outfile
#Checks actual size of MAC.csv
size=$(du -b "$outfile" | cut -f 1)
#Checks if MAC.csv is greater than 5 bytes, if not, writes spreadsheet headers
[ $size -gt 5 ] || echo "Service Tag,WiFi MAC Address" >> $outfile
#Writes the service tag and WiFi MAC address to MAC.csv
echo $servicetag","$mac >> $outfile
#Unmounts the USB flashdrive
# umount /dev/sdb1
#Safely ejects the USB flashdrive
# eject /dev/sdb1
#Removes the ChromeBook from developer mode and reboots the system
#Pauses for 10 seconds before continuing
sleep 10
echo "Rebooting now!"
# crossystem disable_dev_request=1; reboot
if chromebooks have a .profile or .bash_profile, you can consider putting that script in the last line of that file.
or maybe you would like to put it in /etc/rc.local or maybe you can crontab it and put it in a @reboot line.
not sure if chromos supports those since it seems to be a heavily reduced form of gnu/linux (like android) but its worth a shot.
edit: i just realized you only need to run this once per machine so automating it would be an unnecessary step (so my above advice wouldnt apply).
Last edited by schneidz; 02-22-2014 at 12:34 PM.
Reason: retracted
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.