Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8986277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:30:45+00:00 2026-06-15T21:30:45+00:00

Following command doesn’t pick 2nd string from odd line and 4th from odd-even line.

  • 0

Following command doesn’t pick 2nd string from odd line and 4th from odd-even line.
show_statistic.sh | grep -A 1 vlan | awk '{ if ( ( NR % 2 ) == 1 ) { print $2 } else { print $4 } }'
It prints each line’s second and fourth string. What I did wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T21:30:46+00:00Added an answer on June 15, 2026 at 9:30 pm

    It looks like your expected result is from the lines containing “vlan” and the following ones.

    Your code will work with an unique response, but with more response than one your differents results will be separated bi a line containig only “–” as documented in grep :

      -A NUM
          Places  a  line  containing  a  group  separator  (--)   between
          contiguous  groups  of  matches.  With the -o or --only-matching
          option, this has no effect and a warning is given.
    

    So with this line more you will want the second argument on line 1, 4, 7…
    And the fourth on line 2, 5, 8…

    So you code can be :

    show_statistic.sh | grep -A 1 vlan | awk '{  if ( ( NR % 3 ) == 1 ) { print $2 } else { if ( NR % 3 == 2 ) { print $4 } } }'
    

    I write a little text file to test :

    1line here
    2foo
    vlan 2a 3a 4a 5a
    1 2 3 4 5
    bar
    
    line here
    vlan 2a 3a 4a 5a
    1 2 3 4 5
    line here
    baz
    

    The result is :

    $ grep -A 1 vlan file| awk '{  if ( ( NR % 3 ) == 1 ) { print $2 } else { if ( NR % 3 == 2 ) { print $4 } } }'
    2a
    4
    2a
    4
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am running the following command from my home directory: python -m CGIHTTPServer This
I'm currently using the following command in bash to list the info from an
I am trying to rotate PNG images using exiftool using following command line: exiftool
I have the following command line that outputs vmstat every second with a time
I need to iterate lines in a file. Following command doesn't work: set filename=c:\program
The following rake command doesn't work rake db:schema:dump /Library/Ruby/Gems/1.8/gems/bundler-1.1.4/lib/bundler/runtime.rb:211: warning: Insecure world writable dir
I am updating table in database using the following command: public long UpdateIncomeDB(String item,String
Could one help translating following sed command so it does the same on aix
Following command will let me know the names of databases. $ mysqlshow But how
The following command gives many manuals of Zsh man zsh<tab> alt text http://dl.getdropbox.com/u/175564/zsh.png I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.