LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   sed or grep (https://www.linuxquestions.org/questions/programming-9/sed-or-grep-899728/)

pgb_710 08-30-2011 11:30 AM

bash-3.2$ awk '$1~/^SERV/{printf "%s\n",$1} $1~/,/{printf "%s\n",gensub(/,/,"",1,$1);next} {gsub(/,$/,"")}{if($1 !~ /[A-Z]/ && length($1)<8){printf "%s\n",$1}} $2~/,/{printf "%s\n",gensub(/,/,"\n","g",$2)}' test.log
SERVER001
awk: calling undefined function gensub
input record number 1, file test.log
source line number 1

Tinkster 08-30-2011 12:25 PM

Can you use GNU awk, or nawk instead? Your OSes awk must be a museum piece.

grail 08-30-2011 08:19 PM

Please use [code][/code] tags so we can ascertain if you are entering data correctly.

Also you say this for a server ... would it happen to be Solaris?I have had issues from other users on this platform
as awk seems to be quite dated. Maybe provide:
Code:

awk --version
Maybe you could alter the RS portion from mine to the following:
Code:

awk 'BEGIN{RS="[ ,\n]+"}!/^2011/' file

ArthurSittler 09-04-2011 02:00 AM

Patterns in awk can include newline and therefore handle input which continues across multiple lines.

ta0kira 09-04-2011 10:59 AM

I might be missing the point, but it seems like a one-liner won't do the trick since INSERT only takes one row at a time. I would therefore do something like this:
Code:

#!/bin/bash

filename="$1"

cat  "$filename" | tr '\n\r' '  ' | sed 's/, /,/g' | sed -r 's/ [[:digit:]]{8} /&\n/g' | while read line; do
  read server fields date < <( echo "$line" )
  echo "I am inserting [server=$server] [fields=$fields] [date=$date] into the table"
done

In the code above, you'd obviously create an INSERT statement from $server, $fields, and $date. I'm not sure how it's helpful to split the comma-separated values, however, since "SERVER001" and "SERVER002" don't have the same number of values.
Kevin Barry

Tinkster 09-04-2011 12:12 PM

Quote:

Originally Posted by ta0kira (Post 4461197)
I might be missing the point, but it seems like a one-liner won't do the trick since INSERT only takes one row at a time. I would therefore do something like this:
Code:

#!/bin/bash

filename="$1"

cat  "$filename" | tr '\n\r' '  ' | sed 's/, /,/g' | sed -r 's/ [[:digit:]]{8} /&\n/g' | while read line; do
  read server fields date < <( echo "$line" )
  echo "I am inserting [server=$server] [fields=$fields] [date=$date] into the table"
done

In the code above, you'd obviously create an INSERT statement from $server, $fields, and $date. I'm not sure how it's helpful to split the comma-separated values, however, since "SERVER001" and "SERVER002" don't have the same number of values.
Kevin Barry


Exactly ... which is (in less words) what I said in #7.


Cheers,
Tink


All times are GMT -5. The time now is 02:12 PM.