-2

I'm trying to sort the data that I have but unfortunately It's not working as expected.

Data

interface Vlan198
  description *****T3*****
  no ip redirects
  ip address 0.0.0.0/29
  no ipv6 redirects
  hsrp 198
    ip 172.1.71.129

interface Vlan1004
  no ip redirects
  no ipv6 redirects

interface Vlan184
  description **** Reserved for T2    ****
  no ip redirects
  ip address 1.1.1.1/26
  no ipv6 redirects
  hsrp 184
    ip 1.1.1.2

interface Vlan750
  no ip redirects
  ip address 2.2.2.2/24
  no ipv6 redirects
  hsrp 188
    ip 2.2.2.3

interface Vlan183
  description **** Reserved for T1   ****
  no ip redirects
  ip address 3.3.3.3/26
  no ipv6 redirects
  hsrp 183
    ip 3.3.3.4

As you can see in above data, I'm trying to print the data horizontally using below command but the data in not printing as expected because some of the paragraph contains 3,4,7 and I want this to follow 7 line sorting or highest line sorting.

Output of Above Data

interface Vlan198,  description *****DC-New backup server installation*****,  no ip redirects,  ip address 172.1.71.130/29,  no ipv6 redirects,  hsrp 198,    ip 172.1.71.129
interface Vlan1004,  no ip redirects,  no ipv6 redirects,interface Vlan184,  description **** Reserved for Tetration Implementation2    ****,  no ip redirects,  ip address 172.1.122.195/26
  no ipv6 redirects,  hsrp 184,    ip 172.1.122.193,interface Vlan750,  no ip redirects,  ip address 172.1.113.2/24,  no ipv6 redirects
  hsrp 188,    ip 172.1.113.1,interface Vlan183,  description **** Reserved for Tetration Implementation1   ****,  no ip redirects,  ip address 172.1.122.131/26,  no ipv6 redirects
  hsrp 183,    ip 172.1.122.129,

Command that I'm using

cat test2 | grep -i 'interface\|description\|ip address\|hsrp\|ip' | awk -v RS='[,\n]' '{printf "%s%s",$0,(NR%7?",":"\n")}'

Expected Output

enter image description here

DrGeek
  • 45
  • 6
  • 2
    please update the question to include the expected output as actual text (and not as an image); also update the question to include the actual fields/values you're looking to sort by (*`follow 7 line sorting or highest line sorting`* doesn't make sense) – markp-fuso Dec 18 '22 at 05:14
  • the current code attempts to keep only those lines that contain one of 5 different strings, but these match all of the input lines; if there are input lines that you do not want to show up in the final output then please update the sample input to show some of these lines ... otherwise the current sample input and code seems to imply you want to keep/display all input lines, which in turn means we don't actually need the current `grep` ... ??? – markp-fuso Dec 18 '22 at 05:29
  • Tangentially, you want to avoid the [useless `cat`](https://stackoverflow.com/questions/11710552/useless-use-of-cat) – tripleee Dec 18 '22 at 06:20

3 Answers3

3

Using GNU sed

$ sed -Ez 's/\n +/, /g;s/\n\n/\n/g' input_file
interface Vlan198, description *****T3*****, no ip redirects, ip address 0.0.0.0/29, no ipv6 redirects, hsrp 198, ip 172.1.71.129
interface Vlan1004, no ip redirects, no ipv6 redirects
interface Vlan184, description **** Reserved for T2    ****, no ip redirects, ip address 1.1.1.1/26, no ipv6 redirects, hsrp 184, ip 1.1.1.2
interface Vlan750, no ip redirects, ip address 2.2.2.2/24, no ipv6 redirects, hsrp 188, ip 2.2.2.3
interface Vlan183, description **** Reserved for T1   ****, no ip redirects, ip address 3.3.3.3/26, no ipv6 redirects, hsrp 183, ip 3.3.3.4
HatLess
  • 10,622
  • 5
  • 14
  • 32
2

With awk. First awk removes all leading, trailing and multiple consecutive spaces. The second formats each section into a separate line.

awk '{$1=$1}1' file | awk 'BEGIN{FS=RS; OFS=","; ORS=RS; RS="\n\n|\n$"} {$1=$1}1'

Output:

interface Vlan198,description *****T3*****,no ip redirects,ip address 0.0.0.0/29,no ipv6 redirects,hsrp 198,ip 172.1.71.129
interface Vlan1004,no ip redirects,no ipv6 redirects
interface Vlan184,description **** Reserved for T2 ****,no ip redirects,ip address 1.1.1.1/26,no ipv6 redirects,hsrp 184,ip 1.1.1.2
interface Vlan750,no ip redirects,ip address 2.2.2.2/24,no ipv6 redirects,hsrp 188,ip 2.2.2.3
interface Vlan183,description **** Reserved for T1 ****,no ip redirects,ip address 3.3.3.3/26,no ipv6 redirects,hsrp 183,ip 3.3.3.4

See: 8 Powerful Awk Built-in Variables – FS, OFS, RS, ORS, NR, NF, FILENAME, FNR

Cyrus
  • 84,225
  • 14
  • 89
  • 153
1

Assumptions:

  • all blocks start with `interface' as the 1st field in the line
  • we are to display all lines within a block (ie, we are not filtering out, or ignoring, any lines)

One awk idea:

awk '
function print_line() { if (line) print line; line="" }

$1=="interface" { print_line() }                          # start of a new block so flush last block
                { $1=$1                                   # strip leading spaces from 1st field
                  if ($0)                                 # if line is not empty then ...
                     line=line (line ? ", " : "") $0      # append to "line"
                } 
END             { print_line() }                          # flush last set of data
' test2

This generates:

interface Vlan198, description *****T3*****, no ip redirects, ip address 0.0.0.0/29, no ipv6 redirects, hsrp 198, ip 172.1.71.129
interface Vlan1004, no ip redirects, no ipv6 redirects
interface Vlan184, description **** Reserved for T2    ****, no ip redirects, ip address 1.1.1.1/26, no ipv6 redirects, hsrp 184, ip 1.1.1.2
interface Vlan750, no ip redirects, ip address 2.2.2.2/24, no ipv6 redirects, hsrp 188, ip 2.2.2.3
interface Vlan183, description **** Reserved for T1   ****, no ip redirects, ip address 3.3.3.3/26, no ipv6 redirects, hsrp 183, ip 3.3.3.4
markp-fuso
  • 28,790
  • 4
  • 16
  • 36