PDA

View Full Version : Parse files with bash?



Krijk
September 10th, 2007, 10:41 PM
I want to parse logfiles which have multiple lines for single entries to single line entries.
I know how to get the required data with awk, but I don't know how to connect them with each other. The seperate entries are usually seperated by an empty line. Does anybody have some suggestions how to do this?

Example 1

something
1 else
2 other
3 first

otherthing
1 apple
2 coconut
3 pear

to output

something else
something other
something first
otherthing apple
otherthing coconut
otherthing pear

Example 2

Item1
2007 Sep 10
Proces 12345
Has done something

Item2
2007 Sep 10
Proces 12378
Has done something else

to output

Item1 ; 2007 Sep 10 ; Proces 12345 ; Has done something
Item2 ; 2007 Sep 10 ; Proces 12378 ; Has done something else

ghostdog74
September 11th, 2007, 01:12 AM
where's your awk code.?

nanotube
September 11th, 2007, 04:30 AM
starting from the input you have stated (multiline items, separated by empty lines), here's how i'd go about it with python:



f = open('yourinputfilename.txt','r')
text = f.read().split('\n\n')
text2 = [item.strip().replace('\n',' ; ') for item in text.split('\n\n')]
f2 = open('youroutputfile.txt','w')
f2.write('\n'.join(text2))
f.close()
f2.close()


hope that helps. :)

Krijk
September 11th, 2007, 12:14 PM
starting from the input you have stated (multiline items, separated by empty lines), here's how i'd go about it with python:



f = open('yourinputfilename.txt','r')
text = f.read().split('\n\n')
text2 = [item.strip().replace('\n',' ; ') for item in text.split('\n\n')]
f2 = open('youroutputfile.txt','w')
f2.write('\n'.join(text2))
f.close()
f2.close()


hope that helps. :)

So in bash I can probablyu achieve the same with sed, but then I'll have to replace the /n/n first with an identifier to mark the item-end?