awk - Skip x Number of Lines in Counter


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - Skip x Number of Lines in Counter
# 1  
Old 10-01-2015
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:
Code:
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:

Code:
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!

Code:
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


Last edited by Corona688; 10-01-2015 at 03:00 PM..
# 2  
Old 10-01-2015
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 [code] and [/code] 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
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 10-01-2015
Simplifying your code and adding one line to skip the AA through DD lines:
Code:
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:
Code:
$ 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.
This User Gave Thanks to Don Cragun For This Post:
# 4  
Old 10-01-2015
Try
Code:
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[i] ":"  $FIELD[i]
         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

These 2 Users Gave Thanks to RudiC For This Post:
# 5  
Old 10-01-2015
Wow! Sir thank you so much! I really appreciate it! Have a nice day!
# 6  
Old 10-01-2015
Quote:
Originally Posted by RudiC
Try
Code:
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[i] ":"  $FIELD[i]
         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

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:

Code:
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[i] ": "  $FIELD[i]
         print ""
        }
' RS="" FS="\n" ~/circuits.list

# 7  
Old 10-01-2015
Code:
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[i] ":"  $FIELD[i]
         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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to skip lines find text and add text based on number

I am trying to use awk skip each line with a ## or # and check each line after for STB= and if that value in greater than or = to 0.8, then at the end of line the text "STRAND BIAS" is written in else "GOOD". So in the file of 4 entries attached. awk tried: awk NR > "##"' "#" -F"STB="... (6 Replies)
Discussion started by: cmccabe
6 Replies

2. Shell Programming and Scripting

awk to output lines less than number

I am trying to output all lines in a file where $7 is less than 30. The below code does create a result file, but with all lines in the original file. The original file is tab deliminated is that the problem? Thank you :). awk 'BEGIN{FS=OFS=","} $7 < 30 {print}' file.txt > result.txt... (3 Replies)
Discussion started by: cmccabe
3 Replies

3. Shell Programming and Scripting

How to use counter to run the script to limit number?

I want to run my shell script to the limit number.Suppose I know in advance that MAX=5 then I want that my script run 5 times only.Something like below$ vi testingMAX=5COMMAND=&quot;ssh -l stpuser VHLDVWSAD001 /projects/st/utils/deploy/deployall.sh >/dev/null 2>&1 &&quot; ; sleep 20;count=0while... (2 Replies)
Discussion started by: anuragpgtgerman
2 Replies

4. Shell Programming and Scripting

Awk number of lines

How do I get the last NR of a csv file? If I use the line awk -F, '{print NR}' csvfile.csv and there are 42 lines, I get: ... 39 40 41 42 How do I extract the last number, which in this case is 42? ---------- Post updated at 11:05 AM ---------- Previous update was at 10:57 AM... (1 Reply)
Discussion started by: locoroco
1 Replies

5. Shell Programming and Scripting

awk: skip x lines and ssh

Im trying to ssh to a remote machine to grep 'x info' *.log and Im able to get the grep output as expected but "after" the policies (1st 14 lines) - I need to skip the first 14 lines. Its SunOS. Plz help??? (7 Replies)
Discussion started by: anthonyraj75
7 Replies

6. Shell Programming and Scripting

awk - skip x lines and ssh

Im trying to ssh to a remote machine to grep 'x info' *.log and Im able to get the grep output as expected but "after" the policies (1st 14 lines) - I need to skip the first 14 lines. Its SunOS. Plz help??? (1 Reply)
Discussion started by: anthonyraj75
1 Replies

7. Shell Programming and Scripting

How to skip lines which don't begin with a number

Hi, I have a file: file.txt 1 word 2 word word word 3 word 4 word and I would like to create a set: set number = `cut -d" " -f1 ${1}` #${1} is the text file but it should only contain the lines which begin with numbers, and another set which contains the lines which begin with... (10 Replies)
Discussion started by: shira
10 Replies

8. Shell Programming and Scripting

How to skip lines in a KSH?

hi, I have a shell script that searches for a particular pattern in all the files inside a directory, and gives the count of that pattern occurences in a file. Now i should not count the pattern if it exists in side a { .... }, as shown below. { ...... ..... .... PATTERN1.......... (1 Reply)
Discussion started by: divak
1 Replies

9. Shell Programming and Scripting

awk, ignore first x number of lines.

Is there a way to tell awk to ignore the first 11 lines of a file?? example, I have a csv file with all the heading information in the first lines. I want to split the file into 5-6 different files but I want to retain the the first 11 lines of the file. As it is now I run this command: ... (8 Replies)
Discussion started by: trey85stang
8 Replies

10. Shell Programming and Scripting

How to print number of lines with awk ?

Can some body tell me how to print number of line from a particular file, with sed. ? Input file format AAAA BBBB CCCC SDFFF DDDD DDDD Command to print line 2 and 3 ? BBBB CCCC And also please tell me how to assign column sum to variable. I user the following command it... (1 Reply)
Discussion started by: maheshsri
1 Replies
Login or Register to Ask a Question