LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   help parsing file in bash (https://www.linuxquestions.org/questions/linux-general-1/help-parsing-file-in-bash-4175487348/)

computx 12-09-2013 12:48 AM

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.

unSpawn 12-09-2013 01:51 AM

Substr? Like in
Code:

awk '{print $1, $2, substr($4,1,5)}'

syg00 12-09-2013 02:15 AM

Or gensub ? - removes the requirement on knowing the length of the (sub-)string in advance
Code:

awk '{print $1, $2, gensub(/F$/,"","",$4)}'

danielbmartin 12-09-2013 07:28 AM

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

danielbmartin 12-09-2013 07:35 AM

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


All times are GMT -5. The time now is 09:08 PM.