LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 12-09-2013, 12:48 AM   #1
computx
LQ Newbie
 
Registered: Mar 2003
Location: Kirksville, Mo
Distribution: gentoo, mandrake
Posts: 16

Rep: Reputation: 0
help parsing file in bash


I need help parsing a file with awk and sed.
I have a program that outputs to a file in the following format
2013/12/09 00:22:16 Temperature 76.10F
2013/12/09 00:22:17 Temperature 76.10F
I can use awk to clean this up a bit with the following command
awk '{print $1, $2, $4}' temp.txt >fixedtemp.txt
This is the output
2013/12/09 00:22:17 76.10F
2013/12/09 00:22:17 76.10F
2013/12/09 00:22:18 75.99F
I am planning to import this into a spreadsheet but I want to remove the F at the end of each line. How can I accomplish that? Thanks.
 
Old 12-09-2013, 01:51 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Substr? Like in
Code:
awk '{print $1, $2, substr($4,1,5)}'
 
Old 12-09-2013, 02:15 AM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,139

Rep: Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122Reputation: 4122
Or gensub ? - removes the requirement on knowing the length of the (sub-)string in advance
Code:
awk '{print $1, $2, gensub(/F$/,"","",$4)}'
 
Old 12-09-2013, 07:28 AM   #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
With this InFile ...
Code:
2013/12/09 00:22:16 Temperature 76.10F
2013/12/09 00:22:17 Temperature 76.10F
... this awk ...
Code:
awk -F " |F" '{print $1,$2,$4}' $InFile >$OutFile
... produced this OutFile ...
Code:
2013/12/09 00:22:16 76.10
2013/12/09 00:22:17 76.10
Daniel B. Martin
 
Old 12-09-2013, 07:35 AM   #5
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
With this InFile ...
Code:
2013/12/09 00:22:16 Temperature 76.10F
2013/12/09 00:22:17 Temperature 76.10F
... this tr ...
Code:
tr -d '[:alpha:]' <$InFile >$OutFile
... produced this OutFile ...
Code:
2013/12/09 00:22:16  76.10
2013/12/09 00:22:17  76.10
Note the (possibly insignificant) flaw: double blank between fields 2 and 3.

Daniel B. Martin
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Help bash: parsing file into command fajarpri Programming 5 04-06-2012 05:38 AM
Bash scripting: parsing a text file character-by-character Completely Clueless Programming 13 08-12-2009 09:07 AM
BASH RegEx file name parsing Hewson Linux - General 7 04-27-2007 05:37 PM
Help on parsing a log file in BASH globemast Programming 5 01-11-2007 01:56 AM
Parsing a File in a Bash Script TGWDNGHN Programming 4 12-02-2005 02:38 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 12:58 AM.

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