Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

splitting single line into multiple line in numbering format using awk

I'm trying to split the value using awk command into three parts. Need help to break into 3.

Content = 1. 11683 (<server01>: du.size[/,free] : 0.5 % 2. 21683 (<server02>: du.size[/,free] : 1.5 % 3. 31683 (<server03>: du.size[/,free] : 3.5 %

I want the above content to break into 3 lines in a numbering system.

Sample output required :

1. 11683 (<server01>: du.size[/,free] : 0.5 % 
2. 21683 (<server02>: du.size[/,free] : 1.5 % 
3. 31683 (<server03>: du.size[/,free] : 3.5 %

I have tried by the following commands :

echo $content | awk -F"3. " '{ print $2 }' 

and i get

31683 (<server03>: du.size[/,free] : 3.5 %

Similarly for others, But still failed to get sample output in proper numbering format as shown above.

Can any one help me on this ?

like image 502
San Avatar asked Dec 04 '22 12:12

San


2 Answers

The % symbol looks like a good candidate to split on. Try:

sed 's/% /%\n/g'

Testing:

echo "1. 11683 (<server01>: du.size[/,free] : 0.5 % 2. 21683 (<server02>: du.size[/,free] : 1.5 % 3. 31683 (<server03>: du.size[/,free] : 3.5 %" | sed 's/% /%\n/g'

Results:

1. 11683 (<server01>: du.size[/,free] : 0.5 %
2. 21683 (<server02>: du.size[/,free] : 1.5 %
3. 31683 (<server03>: du.size[/,free] : 3.5 %
like image 138
Steve Avatar answered May 18 '23 20:05

Steve


You could use sed instead:

sed "s#[^^]\([0-9]\.\) #\n\1 #g"

Example:

[~/Desktop]
==> echo "1. 11683 (<server01>: du.size[/,free] : 0.5 % 2. 21683 (<server02>: du.size[/,free] : 1.5 % 3. 31683 (<server03>: du.size[/,free] : 3.5 %" | sed "s#[^^]\([0-9]
\.\) #\n\1 #g"
1. 11683 (<server01>: du.size[/,free] : 0.5 %
2. 21683 (<server02>: du.size[/,free] : 1.5 %
3. 31683 (<server03>: du.size[/,free] : 3.5 %
like image 41
Daniel Haley Avatar answered May 18 '23 19:05

Daniel Haley