Change the write in file

Hello

I have a file log with one information in many lines but i want the information log just per line.
How can i do to do this in shell langage.

Input file :

 Info : [Log] etstst trerer
 tetststsss
 Warning : [Log] informatopnn
 eysysysysys
 rererererer
 Error : [Log] this is an error log  
 with information in many phrases  

what I need in output file:

 Info : [Log] etstst trerer tetststsss
 Warning : [Log] informatopnn eysysysysys
 Error : [Log] this is an error log with information in many phrases 

Thanks you in advance

1) Please use code tag
2) What have you tried so far ?

Hello RavinderSingh1,

Thanks you very much but Some time we have not the word before the [log] . could you please to do it with only the kew word [Log]

---------- Post updated at 08:55 AM ---------- Previous update was at 06:25 AM ----------

Input file :

Info : [Log] etstst trerer
tetststsss
Warning : [Log] informatopnn
eysysysysys
rererererer
Error : [Log] this is an error log 
with information in many phrases 
[Log] this is anererer
rerererere

what I need in output file:

Info : [Log] etstst trerer tetststsss
Warning : [Log] informatopnn eysysysysys
Error : [Log] this is an error log with information in many phrases
[Log] this is anererer rerererere.

I want just to use the keywork [Log] to write all the information from multiple line to one line.

This one prints/empties the buffer sooner. Saves memory if the log file is big.

awk '/\[Log\]/ {if (NR>1) print buf; buf=$0; next} {buf=buf FS $0} END {if (NR>1) print buf}' file

With sed

sed -n '
  :L
  $p; N
  /\n.*\[Log\]/{
    P; D
  }
  s/\n/ /
  bL
' file

Guys, I think when a fellow, established member asks "What have you tried so far" it's courteous to wait for a response from the O/P in such regard before offering possible solutions.

My $0.02.

1 Like

Hello Scott,

Greetings !!

Good to see you sir. Yes, even I do agree when a member asks user question we shouldn't reply answers. If you see the time of my and Pravin's post we posted on exact sametime. After that when I tried to delete that post I got an error not sure if it's a browser issue may be, so couldn't delete it.

EDIT: I have deleted my both posts now. Somehow I was not able to do that yesterday.

Thanks,
R. Singh

1 Like

Hey, mate, thanks, good to see you too :slight_smile:

It's not an issue, of course, but without knowing where boudake's knowledge is at we can't know at which level to pitch our support. (that seemed to make sense when I wrote it lol)

1 Like

Now that I have added CODE tags so we can see leading and trailing spaces in your data, we can now see that your examples in post #1 and in post #4 are very different.

In post #1, your sample output removes trailing spaces from each input line and concatenates all lines that do not contain the string [log] to the end of the previous line with no other added space (note that each sample input line has a leading <space> character.

In post #4, your sample output does not remove trailing spaces, adds at most one line that does not contain [log] to the end of the previous line that did contain [log] after adding a <space> character to separate those two lines, deletes any other lines that do not contain the string [log] , and adds a <period> character to the end of the last line printed. (Note that there are no leading <space> characters in the output or output samples in this post.)

But, your English description for both examples is the same. Please CLEARLY state your requirements and post sample input (displayed in CODE tags) and the exact output that should be produced from that sample input (also displayed in CODE tags).

If you can't clearly state your requirements and show us sample input and output that correspond to those requirements, we do not have a chance of producing code that accurately meets your requirements. Please help us help you!

1 Like

Hello,
Thanks you guys, it is worked well.
I have a another question but I want to search by myself before asking here.
My level is low in shell programming. I just replacing a colleague that why i am doing it.

Hello boudake,

You are always welcome to ask queries(each query in a separate thread no mix ups please) and learn from the answers. So deal is we have rules and regulations of this forum where we should encourage people to learn not dependent on others, everyone starts one day learning that is not an issue but as we grow with time our knowledge should also grow with that. So you are more than welcome to ask queries, you should explain the problem very clearly in plain English, try to give us some sample Input_file(within code tags), show us sample Output_file with all the conditions too(within code tags). Also you could use search option in this forum too by which you could find related threads and could get a pulse of issues and their solutions. I hope this helps.

Thanks,
R. Singh