awk - Skip x Number of Lines in Counter

Hello,

I am new to AWK and in UNIX in general. I am hoping you can help me out here.

Here is my data:

root@ubuntu:~# cat circuits.list
WORD1
AA
BB
CC
DD
Active
ISP1
ISP NAME1
XX-XXXXXX1

WORD1
AA
BB
CC
DD
Active
ISP2
ISP NAME2
XX-XXXXX2


WORD2
AA
BB
CC
DD
Active
ISP1
ISP NAME1
XX-XXXXX1

WORD2
AA
BB
CC
DD
Active
ISP2
ISP NAME2
XX-XXXXX2

I would like to skip AA up to DD and Create an output like this:

Location: WORD1 
Status: Active 
Service Type: ISP1 
Service Provider: ISP NAME1
Name ID: XX-XX-XXX1

Location: WORD1 
Status: Active 
Service Type: ISP2
Service Provider: ISP NAME2
Name ID: XX-XX-XXX2

My current command is the one written below but AA-DD is getting in the way. Please help!

root@ubuntu:~# awk '
BEGIN{split("Location,Status,Service Type,Service Provider,ID",h,",")}
/WORD1/{print""; c=7}
c&&c--&&c!=1&&c!=0{print h[7-c]": " $0}' circuits.list

Output:

Location: WORD1
Status: AA
Service Type: BB
Service Provider: CC
ID: DD

Location: WORD1
Status: AA
Service Type: BB
Service Provider: CC
ID: DD

To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags

```text
 and 
```

by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums

1 Like

Simplifying your code and adding one line to skip the AA through DD lines:

awk '
BEGIN{n=split("Location,Status,Service Type,Service Provider,Name ID",h,",");c=n}
/AA/,/DD/{next}
/WORD1/{print"";c=0}
c<n{print h[++c]": " $0}' circuits.list > out.txt

seems to do what you want. Note that I changed ID in the string you gave to split() to Name ID to match the output you said you wanted. If the 1st line in circuits.list will always be WORD1 , you won't need the ;c=n in the BEGIN clause. With your sample input the above produces the output:

$ cat out.txt

Location: WORD1
Status: Active
Service Type: ISP1
Service Provider: ISP NAME1
Name ID: XX-XXXXXX1

Location: WORD1
Status: Active
Service Type: ISP2
Service Provider: ISP NAME2
Name ID: XX-XXXXX2
$ 

which matches the output you said you wanted execept:

  • there are no trailing spaces in the output, and
  • no hyphens were added to the input in the Name ID line output values.
1 Like

Try

awk '
BEGIN   {n=split ("Location,Status,Service Type,Service Provider,ID", HD, ",")
         n=split ("1 6 7 8 9", FIELD)
        }
        {for (i=1; i<=n; i++) print HD ":"  $FIELD
         print ""
        }
' RS="" FS="\n" file
Location:WORD1
Status:Active
Service Type:ISP1
Service Provider:ISP NAME1
ID:XX-XXXXXX1

Location:WORD1
Status:Active
Service Type:ISP2
Service Provider:ISP NAME2
ID:XX-XXXXX2

Location:WORD2
Status:Active
Service Type:ISP1
Service Provider:ISP NAME1
ID:XX-XXXXX1

Location:WORD2
Status:Active
Service Type:ISP2
Service Provider:ISP NAME2
ID:XX-XXXXX2

2 Likes

Wow! Sir thank you so much! I really appreciate it! Have a nice day!

Hello,

First, I would like to thank you for your assistance.

I need to know where would the user input will be located when I would like to select a specific location? For example:

read -p "Select Location:" sitecode

awk '
BEGIN   {n=split ("Location,Status,Service Type,Service Provider,ID", HD, ",")
         n=split ("1 6 7 8 9", FIELD)
        }
        {for (i=1; i<=n; i++) print HD ": "  $FIELD
         print ""
        }
' RS="" FS="\n" ~/circuits.list

awk '
BEGIN   {n=split ("Location,Status,Service Type,Service Provider,ID", HD, ",")
         n=split ("1 6 7 8 9", FIELD)
        }
$1==LOC {for (i=1; i<=n; i++) print HD ":"  $FIELD
         print ""
        }
' RS="" FS="\n" LOC="$sitecode" file
Location:WORD1
Status:Active
Service Type:ISP1
Service Provider:ISP NAME1
ID:XX-XXXXXX1

Location:WORD1
Status:Active
Service Type:ISP2
Service Provider:ISP NAME2
ID:XX-XXXXX2

Sorry for being such a noob but I tried to do what you gave and it appears it is not giving me any output:

Content of script:

root@ubuntu:~# cat circuitops.sh

read -p "Please enter site code: " sitecode
awk '
BEGIN   {n=split ("Location,Status,Service Type,Service Provider,ID", HD, ",")
         n=split ("1 6 7 8 9", FIELD)
        }
$1==LOC {for (i=1; i<=n; i++) print HD ":"  $FIELD
         print ""
        }
' RS="" FS="\n" LOC="$sitecode" ~/circuits.list

When I run the script:

root@ubuntu:~# ./circuitops.sh
Please enter site code: WORD1
root@ubuntu:~#

Content of file name circuits.list:

root@ubuntu:~# cat circuits.list
WORD1
AA
BB
CC
DD
Active
ISP1
ISP NAME1
XX-XXXXXX1

WORD1
AA
BB
CC
DD
Active
ISP2
ISP NAME2
XX-XXXXX2


WORD2
AA
BB
CC
DD
Active
ISP1
ISP NAME1
XX-XXXXX1

WORD2
AA
BB
CC
DD
Active
ISP2
ISP NAME2
XX-XXXXX2

---------- Post updated at 05:12 PM ---------- Previous update was at 04:13 PM ----------

Hi Guys,

Thanks again for the assistance. I hate to add grep to the line but I ended up doing so:

read -p "Please enter site code: " sitecode

awk '
BEGIN   {n=split ("Location,Status,Service Type,Service Provider,ID", HD, ",")
         n=split ("1 6 7 8 9", FIELD)
        }
        {for (i=1; i<=n; i++) print HD ":"  $FIELD
         print ""
        }
' RS="" FS="\n" ~/circuits.list | grep $sitecode -A 5

Output:

$./circuitops.sh

Please enter site code: WORD2

Location:WORD2
Status:Active
Service Type:ISP1
Service Provider:ISP NAME1
ID:XX-XXXXX1

Location:WORD2
Status:Active
Service Type:ISP2
Service Provider:ISP NAME2
ID:XX-XXXXX2

Overall I am happy. :slight_smile: You can consider this problem as resolved.

Cheers!

The scripts:

read -p "Please enter site code: " sitecode
awk '
BEGIN   {n=split ("Location,Status,Service Type,Service Provider,ID", HD, ",")
         n=split ("1 6 7 8 9", FIELD)
        }
$1==LOC {for (i=1; i<=n; i++) print HD ":"  $FIELD
         print ""
        }
' RS="" FS="\n" LOC="$sitecode" ~/circuits.list

and:

read -p "Please enter site code: " sitecode
awk '
BEGIN   {n=split ("Location,Status,Service Type,Service Provider,ID", HD, ",")
         n=split ("1 6 7 8 9", FIELD)
        }
        {for (i=1; i<=n; i++) print HD ":"  $FIELD
         print ""
        }
' RS="" FS="\n" ~/circuits.list | grep $sitecode -A 5

Should produce the same output unless:

  • you typed in leading or trailing spaces or tabs in response to the prompt (which would be ignored by the grep since the expansion $sitecode was not surrounded by double quotes), or
  • the string that you typed in response to the prompt contains characters that have special meaning in a basic regular expression (which would be processed as a BRE by grep and as a fixed string by $1==LOC in awk ).
1 Like

Another possibility is, there's DOS <CR> line terminators in the file so that $sitecode would not match WORD1<CR> ...