LinuxQuestions.org

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

pgb_710 08-26-2011 10:46 PM

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

ta0kira 08-27-2011 12:07 AM

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

grail 08-27-2011 12:48 AM

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

kurumi 08-27-2011 01:15 AM

Code:

$ ruby -F' ' -ane 'printf("%s\n%s", $F[0],$F[1].gsub(",","\n")) if $F[0]["SERVER001"]' file

theNbomr 08-27-2011 01:18 AM

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.

pgb_710 08-28-2011 08:16 PM

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

Tinkster 08-28-2011 09:06 PM

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...

kurumi 08-28-2011 09:37 PM

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


Tinkster 08-28-2011 10:19 PM

Quote:

Originally Posted by kurumi (Post 4455726)
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?

kurumi 08-28-2011 11:37 PM

Quote:

Originally Posted by Tinkster (Post 4455741)
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

grail 08-29-2011 03:37 AM

Code:

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

kurumi 08-29-2011 03:45 AM

Quote:

Originally Posted by grail (Post 4455894)
Code:

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

Nice, and this is assuming OP's data has no other irrelevant information present...

grail 08-29-2011 03:57 AM

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 :redface:

pgb_710 08-30-2011 12:25 PM

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 :(

pgb_710 08-30-2011 12:26 PM

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


All times are GMT -5. The time now is 09:12 AM.