LinuxQuestions.org
Visit the LQ Articles and Editorials section
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-26-2011, 09:46 PM   #1
pgb_710
LQ Newbie
 
Registered: Aug 2007
Location: Pune
Posts: 10

Rep: Reputation: 0
sed or grep


Hello Folks,
I want to print one log file


$ cat test.log
SERVER001 234,432,3456,1234 20110908
SERVER002 456,675,7465,7676,
56456,
56734,
345
20110909


I want to insert the logs in mysql table
want output like

SERVER001
234
432
3456
1234
 
Old 08-26-2011, 11:07 PM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
To be literal, it looks like grep, cut, and tr will convert test.log to what you have:
Code:
grep 'SERVER001' test.log | cut -d' ' -f1,2 | tr ' ,' '\n\n'
This obviously works for the exact input and output you described; however, it would be better if you described what you expect as input and what you require as output in a more general situation. The format of test.log isn't consistent enough for all lines to be converted; however, it appears that at least the "SERVER002" might also be a candidate for conversion, also.
Kevin Barry
 
Old 08-26-2011, 11:48 PM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,442

Rep: Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880
I agree we need more information to understand if it is only the first line to be interpreted each time.
However the following will also give output requested as per example:
Code:
awk '{gsub(",","\n",$2);print $1"\n"$2;exit}' file
 
Old 08-27-2011, 12:15 AM   #4
kurumi
Member
 
Registered: Apr 2010
Posts: 223

Rep: Reputation: 45
Code:
$ ruby -F' ' -ane 'printf("%s\n%s", $F[0],$F[1].gsub(",","\n")) if $F[0]["SERVER001"]' file
 
Old 08-27-2011, 12:18 AM   #5
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Yes, there is not enough consistency in your sample data. The sample should be accompanied by some written description of the data and the requirement.
'12 columns of numeric data separated by commas and whitespace, rows separated by.... I need fields 1, 3, 5,6, & 8'

--- rod.
 
Old 08-28-2011, 07:16 PM   #6
pgb_710
LQ Newbie
 
Registered: Aug 2007
Location: Pune
Posts: 10

Original Poster
Rep: Reputation: 0
Not resolved

Thanks for the reply, however not any one solved my problem. information for server01 is only printed
My requirement is like SERVER name and its values to be printed for specific dates

The data between SERVER% and 2011% to be printed ( It can present on one line or in multiple lines)

cat test.log
SERVER001 234,432,3456,1234 20110908
SERVER002 456,675,7465,7676,
56456,
56734,
345
20110909
 
Old 08-28-2011, 08:06 PM   #7
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,966
Blog Entries: 11

Rep: Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865
Quite ugly, does the job, and relies on none of the numerical values
having more than 7 digits.
Code:
cat server 
SERVER001 234,432,3456,1234 20110908
SERVER002 456,675,7465,7676,
56456,
56734,
345
20110909
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)}' server 
SERVER001
234
432
3456
1234
SERVER002
456
675
7465
7676
56456
56734
345

Cheers,
Tink

P.S.: I wonder how you intend to store that structure sensibly in
a relational database...

Last edited by Tinkster; 08-28-2011 at 09:19 PM.
 
Old 08-28-2011, 08:37 PM   #8
kurumi
Member
 
Registered: Apr 2010
Posts: 223

Rep: Reputation: 45
Code:
$ ruby -F' ' -ane 'printf("%s\n%s\n", $F[0],$F[1].gsub(",","\n")) if $F[0][/SERVER/]' file
SERVER001
234
432
3456
1234
SERVER002
456
675
7465
7676
 
Old 08-28-2011, 09:19 PM   #9
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,966
Blog Entries: 11

Rep: Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865
Quote:
Originally Posted by kurumi View Post
Code:
$ ruby -F' ' -ane 'printf("%s\n%s\n", $F[0],$F[1].gsub(",","\n")) if $F[0][/SERVER/]' file
SERVER001
234
432
3456
1234
SERVER002
456
675
7465
7676

What about
Code:
56456
56734
345
of SERVER002?
 
Old 08-28-2011, 10:37 PM   #10
kurumi
Member
 
Registered: Apr 2010
Posts: 223

Rep: Reputation: 45
Quote:
Originally Posted by Tinkster View Post
What about
Code:
56456
56734
345
of SERVER002?
thanks, missed the requirement

Code:
$ ruby -0777 -ane '$_.scan(/(SERVER.*?)2011..../m){|x| puts x[0].gsub(/[,\s]/,"\n") }' file

Last edited by kurumi; 08-29-2011 at 03:05 AM.
 
Old 08-29-2011, 02:37 AM   #11
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,442

Rep: Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880
Code:
awk '!/^2011/' RS="[ ,\n]+" file
 
Old 08-29-2011, 02:45 AM   #12
kurumi
Member
 
Registered: Apr 2010
Posts: 223

Rep: Reputation: 45
Quote:
Originally Posted by grail View Post
Code:
awk '!/^2011/' RS="[ ,\n]+" file
Nice, and this is assuming OP's data has no other irrelevant information present...
 
Old 08-29-2011, 02:57 AM   #13
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,442

Rep: Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880Reputation: 1880
Quote:
Nice, and this is assuming OP's data has no other irrelevant information present...
True ... I find it hard to break the old requirements doc habit ... ie if its not in then you get what you asked for
 
Old 08-30-2011, 11:25 AM   #14
pgb_710
LQ Newbie
 
Registered: Aug 2007
Location: Pune
Posts: 10

Original Poster
Rep: Reputation: 0
Dear experts thanks for the reply.
Only the solution mentioned with RUBY is working fine, some reason I can not use Ruby on the server
I thought it should also be doable with sed, grep or awk .. I could not find anything working as per my expectations other than RUBY
 
Old 08-30-2011, 11:26 AM   #15
pgb_710
LQ Newbie
 
Registered: Aug 2007
Location: Pune
Posts: 10

Original Poster
Rep: Reputation: 0
bash-3.2$ cat test.log
SERVER001 234,432,3456,1234 20110908
SERVER002 456,675,7465,7676,
56456,
56734,
345
20110909

bash-3.2$ ruby -0777 -ane '$_.scan(/(SERVER.*?)2011..../m){|x| puts x[0].gsub(/[,\s]/,"\n") }' test.log
SERVER001
234
432
3456
1234
SERVER002
456
675
7465
7676

56456

56734

345

bash-3.2$ awk '!/^2011/' RS="[ ,\n]+" test.log
SERVER001 234,432,3456,1234 20110908
SERVER002 456,675,7465,7676,
56456,
56734,
345
20110909
 
  


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


All times are GMT -5. The time now is 11:20 PM.

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