Move a text to next line in a file

Hi ,

I need your help for the below issue.

I have a file which has data as below

An error came (/u01/app/12.csv)
pkg1.func1: detail s 1111-->pkg1.func1: detail s 2222-->

Now pkg1.func1: .... --> can come multiple times in the second line.
I need to arrange the data in the below way for the file.

An error came (/u01/app/12.csv)
pkg1.func1: detail s 1111
pkg1.func1: detail s 222

The above thing will happen for any number of occurrences of pkg1.func1: .... --> text.

Thanks in advance.

So, essentially you want to change
"-->" to a new-line character (carriage return/line feed)
???

check this one

 
sed 's/-->/\n/g' input_file.txt

Thanks for the reply.
Yes I want this thing along with

pkg1.func1: detail s 1111
pkg1.func1: detail s 222

which means pkg1.func1: will be coming everytime .

An awk approach:

awk '/pkg1\.func1/{gsub("-->",RS)}1' file
1 Like

Thanks for your help. It worked.
Can you please explain me the code as I'm not very much efficient in awk.

awk '
        /pkg1\.func1/ {                 # Check if pattern: pkg1.func1 present in current record.
                gsub ("-->", RS)        # Global Substitute "-->" with RS (RS is a special awk variable, by default a newline)
        }
        1                               # 1 == true (if true, the default awk action is to print current record)
' file
1 Like

Many thanks for this explanation.
It is totally clear now.

Hi,

I want to do some added functionality in this file.
Now my file will ahve this type of text.

  1. and 2. are the number s which will come along with previous message.This number cab be of n digits.

Now I need to arrange the text in teh below way.

Now I have arranged the text as per the numeric value associated with text and replaced the numeric value in final text.

Thanks in advance.

Starting with Yoda's proposal, you may try:

$ awk     '/pkg1\.func1/  {n = split ($0, TMP, "-->")
                         for (i=1; i<n; i++)    {m = match (TMP, /: [0-9]*\./)
                                                 x = substr (TMP, RSTART+2, RLENGTH-3) + 0
                                                 sub (/: [0-9]*\./, ": ", TMP)
                                                 OutArr[x] = TMP
                                                 if (x > max) max = x
                                                }
                         for (i=1; i<=max; i++)  if (OutArr) print OutArr   
                         delete OutArr
                         max = 0
                         next
                        }
         1
        ' file
An error came (/u01/app/12.csv)
pkg1.func1: detail is dept error
pkg1.func1: detail is loc error