Splitting one line into multiple lines in numbering format using awk
I am trying to split a value using awk command into three parts. Need help to hack 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 be broken into 3 lines in the numbering system.
Required 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've tried the following commands:
echo $content | awk -F"3. " '{ print $2 }'
and i get
31683 (<server03>: du.size[/,free] : 3.5 %
Likewise for others, but still failed to get the output sample in the correct numbering format as shown above.
Can anyone help me with this?
source to share
Instead, sed
you can use
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 %
source to share
The symbol %
looks like a good candidate for splitting. 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 %
source to share
perl -pe 's/(\d+\.\s)/\n$1/g'
tested:
> 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 %" | perl -pe 's/(\d+\.\s)/\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 %
source to share
Your data appears to be fixed in number of items.
{
print $1, $2, $3, $4, $5, $6, $7
print $8, $9, $10, $11, $12, $13, $14
print $15, $16, $17, $18, $19, $20, $21
}
Useful if you need to do something with the data other than printing. In the meantime, there is no need to know about it. ”
source to share