LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Arch
User Name
Password
Arch This Forum is for the discussion of Arch Linux.

Notices


Reply
  Search this Thread
Old 04-22-2013, 07:57 AM   #1
rlagreid
LQ Newbie
 
Registered: Apr 2013
Posts: 3

Rep: Reputation: Disabled
Script to extract text from one file to another


Hi,

I need a script to extract and reformat some text from one file into another.
I am a total newbie and need proper, detailed descriptions.

The file I have looks like this:

22-04-13, 1055
TempIn 24
TempEx 14
WindHi 5
WindAv 5
WindDr 90
BarmPs 29825
HumdIn 27
HumdEx 49
RnFall 0.00
DailyRnFall 0.28

...and the output I need looks like this:


outsideTemp=14
insideTemp=24


I need to have this script running at 10 minute intervals.
Thanks in advance.
 
Old 04-22-2013, 09:53 AM   #2
Sigg3.net
Member
 
Registered: Mar 2008
Location: Oslo, Norway
Distribution: Slackware 14.1 64-bit, Ubuntu 15.10, Fedora 17, Ubuntu 12 LTS and Ubuntu server 10.04
Posts: 173

Rep: Reputation: 28
You can easily do that with sed substitute:

Code:
sed 's/foo/bar/g'
sed is the program, s stands for substitute, /foo/ is the search query and bar/ its replacement. Example:

Code:
sed 's/TempIn /insideTemp=/g'
Will substitute TempIn with insideTemp=. Notice the space after TempIn to substitute that as well.

Are you going to store or just display this info? Sed does this "on the fly" more or less (not humanly visible).

For a file testfile do:
Code:
cat testfile | sed 's/TempIn /insideTemp=/g'
to output the new line "live".

Otherwise you'll need 2 files: inputfile and outputfile


Code:
#!/bin/bash
inputfile="/path/to/inputfile"
outputfile="/path/to/outputfile"

# in case output file does not exist
touch -a $outputfile

cat testfile | sed 's/TempIn /insideTemp=/g' >> $outputfile
Repeat for each line. The double >> just appends and does not replace text. If you want to use 2 files and display the latter, you'll need to empty or just delete the outputfile on consecutive runs;


Code:
#!/bin/bash
inputfile="/path/to/inputfile"
outputfile="/path/to/outputfile"

# if outputfile exists delete it, else touch/create it
if [ -f $outputfile ] ; then
	rm $outputfile
else
	touch -a $outputfile
fi

# substitutions:
cat testfile | sed 's/TempIn /insideTemp=/g' >> $outputfile

# fill in the rest above, then
exit
Then you'll need to save this as something.sh, chmod +x something.sh to make it executable (like a program), and then ask cron to run this at your desired interval.

Last edited by Sigg3.net; 04-22-2013 at 09:55 AM.
 
Old 04-22-2013, 10:22 AM   #3
rlagreid
LQ Newbie
 
Registered: Apr 2013
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thanks.
It moves me in the right direction, but I need to replace the first line which is changing for every run, with a blank line and remove the lines following the TempEx line.
 
Old 04-22-2013, 10:36 AM   #4
david1941
Member
 
Registered: May 2005
Location: St. Louis, MO
Distribution: CentOS7
Posts: 267

Rep: Reputation: 58
try this sed pipe

sed 's/TempIn/insideTemp=/g; s/TempEx/outsideTemp=/g ' <infile |grep 'Temp' >outfile
 
Old 04-22-2013, 10:39 AM   #5
Sigg3.net
Member
 
Registered: Mar 2008
Location: Oslo, Norway
Distribution: Slackware 14.1 64-bit, Ubuntu 15.10, Fedora 17, Ubuntu 12 LTS and Ubuntu server 10.04
Posts: 173

Rep: Reputation: 28
Okay. Instead of trimming away individual lines (you could do that with tail -n +2 "$FILE"), do a grep to select the lines you DO want.



Code:
#!/bin/bash
inputfile="/path/to/inputfile"
outputfile="/path/to/outputfile"

# if outputfile exists delete it, else touch/create it
if [ -f $outputfile ] ; then
	rm $outputfile
else
	touch -a $outputfile
fi

# substitutions:
cat testfile | grep "TempIn" | sed 's/TempIn /insideTemp=/g' >> $outputfile
cat testfile | grep "TempEx" | sed 's/TempEx /outsideTemp=/g' >> $outputfile

exit
grep "TempIn" will grep (or grip) the whole line of text, THEN pipe this line to sed..
You can also use tr (translate) instead of sed, but I'm much more comfortable with the latter
 
Old 04-22-2013, 10:46 AM   #6
rlagreid
LQ Newbie
 
Registered: Apr 2013
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thank you so much.

My problem is solved :-)
 
Old 04-22-2013, 11:05 AM   #7
Sigg3.net
Member
 
Registered: Mar 2008
Location: Oslo, Norway
Distribution: Slackware 14.1 64-bit, Ubuntu 15.10, Fedora 17, Ubuntu 12 LTS and Ubuntu server 10.04
Posts: 173

Rep: Reputation: 28
No problem

I advise you to play around in bash as much as you can. It can make GNU/Linux so much easier and fun!
 
Old 04-23-2013, 11:25 AM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986Reputation: 1986
Code:
sed -n '/TempIn/ s/TempIn /insideTemp=/p ; /TempEx/ s/TempEx /outsideTemp=/p' infile.txt
-n suppresses output by default, and the 'p' modifier to the substitution command prints out only the lines that get modified. The output of the two lines will in the order they appear in the input, however. If you really need to reverse them it's a bit more complex.

Code:
sed -n '/TempIn/ { s/TempIn /insideTemp=/; h} ; /TempEx/ s/TempEx /outsideTemp=/p; $ { g ;p }' infile.txt
The "h" command moves the modified "TempIn" line into the hold buffer for later use. Then the "TempEx" substitution is made and printed. When it reaches the last line, it gets the contents of the hold buffer and prints it.

Here are a few useful sed references:
http://www.grymoire.com/Unix/Sed.html
http://sed.sourceforge.net/grabbag/
http://sed.sourceforge.net/sedfaq.html
http://sed.sourceforge.net/sed1line.txt
http://www.catonmat.net/series/sed-one-liners-explained
 
Old 04-24-2013, 01:45 PM   #9
Sigg3.net
Member
 
Registered: Mar 2008
Location: Oslo, Norway
Distribution: Slackware 14.1 64-bit, Ubuntu 15.10, Fedora 17, Ubuntu 12 LTS and Ubuntu server 10.04
Posts: 173

Rep: Reputation: 28
Nice!
 
  


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
How to do search & replace on a text file--need to extract URLs from a sitemap file Mountain Linux - General 4 08-07-2015 11:52 AM
Prompt the user for a file to open, extract the XML and write to another text file. richiep Linux - Newbie 7 10-22-2010 04:34 PM
Extract certain text info from text file xmrkite Linux - Software 30 02-26-2008 12:06 PM
script to extract blocks of text from many files. gruessle Programming 4 10-19-2007 03:31 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Arch

All times are GMT -5. The time now is 08:47 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration