0
Received timestamp=1459434658969:
ABC: Field id=0 double 11.4
DEF: Field id=1 string >def<
GHI: Field id=2 string >g_hi<

I would like to read a file that contains input in the above format and would like to output the following data into an xml file:

ABC: 11.4
DEF: def
GHI: g_hi
3
  • Is there a pattern in the input file? For example, is it always the time stamp in the first row and than always the fields (separated by a space) 1 and 5 in the following rows you want to get? Commented Apr 1, 2016 at 9:52
  • yes the format is always as displayed in the question starting with the timestamp Commented Apr 1, 2016 at 9:54
  • 2
    That's not an XML File. Commented Apr 1, 2016 at 10:08

2 Answers 2

0

You could use grep, awk and sed for this. First, filter out the not wanted first line containing the word received (you should use here unique expressions to make sure that only this line is filtered), that let awk split the following lines by and than remove the < and > with sed:

cat a.txt  | grep -v received | awk '{print $1 " " $5}' | sed -e 's/[<>]//g'

In the above example a.txt contains the input

received timestamp=1459434658969:
ABC: Field id=0 double 11.4
DEF: Field id=1 string >def<
GHI: Field id=2 string >g_hi<

The result is:

ABC: 11.4
DEF: def
GHI: g_hi
0
0

there is no need to pipe grep,awk ans sed

perl -e 'while (<>) {
  s/[<>]//g ;
  next if ( /received/ ) ;
  @Fld = split(" ", $_);
  printf "%s %s\n",$Fld[0],$Fld[4] ;}' a.txt

will do it.

you can put code in a perl file

perl -f a.pl  a.txt

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.