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 8125983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:59:57+00:00 2026-06-06T06:59:57+00:00

The following awk statement is working as expected. It returns 1st, 2nd and last

  • 0

The following awk statement is working as expected.
It returns 1st, 2nd and last 4 columns from a text file delimited by |

awk '{print $1,"|", $2,"|", $(NF-3), "|",$(NF-2), "|", $(NF-1),"|", $(NF)}'

I will like to return the middle columns in between. In other words I need to replace the $something from the following line to something that will work.

awk '{print $1, $2, $something, $(NF-3), $(NF-2) $(NF-1), $(NF)}'

There can be 3 to 11 columns those will replace the something variable.

  • 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-06T07:00:02+00:00Added an answer on June 6, 2026 at 7:00 am

    I think I have understood what you are expecting your output to look like. Try this line of awk:

    awk 'BEGIN { OFS="|" } { for (i=1; i<=NF; i++) if (i <= 3 || i >= NF - 3) printf "%s"OFS, $i; else printf "%s", $i; print "" }' file.txt
    

    Test input:

    1 2 3 4 5 6 7 8 9 10 11 12 13 14
    X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12 X13 X14
    Y1 Y2 Y3 Y4 Y5 Y6 Y7 Y8 Y9 Y10 Y11 Y12 Y13 Y14
    101 102 103 104 1010 1011 1012 1013 1014
    

    Test output:

    1|2|3|4567891011|12|13|14
    X1|X2|X3|X4X5X6X7X8X9X10X11|X12|X13|X14
    Y1|Y2|Y3|Y4Y5Y6Y7Y8Y9Y10Y11|Y12|Y13|Y14
    101|102|103|10410101011|1012|1013|1014
    

    HTH

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following awk code is working as expected. I want to check if the
I wrote the following awk to print lines from the match line until EOF
I write the following script #!/usr/bin/perl system( awk -F= '{s[$1]++}{print $1s[$1],$2}' OFS== /var/tmp/file );
The following awk code is working as expected. The date like 2012-03-10~12:59:41 is rounded
I write the following awk ( print VAL_1 & VAL_2 if match in file
I have the following command that returns a date from a file tail -1
I'm attempting to decipher the action in the following awk statement specifically what the
When I run the following awk -F\, '{print $2,:,$1}' It prints First : Second
I have found the following command in AWK useful in Vim :'<,'>!awk '{ print
The following works as expected. awk -F'^' '{printf set %s:%s %s\n,$1,$2, $7}' todel.txt |

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.