LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-30-2011, 12:30 PM   #16
pgb_710
LQ Newbie
 
Registered: Aug 2007
Location: Pune
Posts: 10

Original Poster
Rep: Reputation: 0

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
 
Old 08-30-2011, 01:25 PM   #17
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,000
Blog Entries: 11

Rep: Reputation: 893Reputation: 893Reputation: 893Reputation: 893Reputation: 893Reputation: 893Reputation: 893
Can you use GNU awk, or nawk instead? Your OSes awk must be a museum piece.
 
Old 08-30-2011, 09:19 PM   #18
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,655

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
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
 
Old 09-04-2011, 03:00 AM   #19
ArthurSittler
Member
 
Registered: Jul 2008
Distribution: Slackware
Posts: 124

Rep: Reputation: 30
Patterns in awk can include newline and therefore handle input which continues across multiple lines.
 
Old 09-04-2011, 11:59 AM   #20
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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
 
Old 09-04-2011, 01:12 PM   #21
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,000
Blog Entries: 11

Rep: Reputation: 893Reputation: 893Reputation: 893Reputation: 893Reputation: 893Reputation: 893Reputation: 893
Quote:
Originally Posted by ta0kira View Post
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
 
  


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
Grep or Sed ? How can I use it ? ArOnaXx Linux - Newbie 6 04-12-2009 03:08 PM
grep -B using sed w3bd3vil Programming 3 08-11-2008 07:37 PM
how to use grep/sed tnik Linux - General 3 11-06-2007 08:51 PM
I need some help with SED and/or GREP... TheEngineer Linux - Newbie 1 02-02-2007 09:17 PM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 12:36 PM


All times are GMT -5. The time now is 01:38 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration