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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:14:00+00:00 2026-06-15T12:14:00+00:00

how to extract some lines from a text file using unix shell script awk.

  • 0

how to extract some lines from a text file using unix shell script awk.

e.g.
1) input: file_name_test.txt

**<header> asdfdsafdsf**  
11 asd sad
12 sadf asdf
13 asdfsa asdf
14 asd sdaf
**15 asd asdfsdf
16 sadfsadfsaf sdfsdf
17 asdf sdaf
18 asfd saf
19 sadf asdf
10 asf asf**

2) expected output:

**<header> asdfdsafdsf
15 asd asdfsdf
16 sadfsadfsaf sdfsdf
17 asdf sdaf
18 asfd saf
19 sadf asdf
10 asf asf**

3) code for test.sh:

FILENAME=$1
threshold=$2
awk '{line_count++;
if (line_count==1 || (line_count>$threshold))
print $0;
}' $FILENAME > overflow_new2

4)

sh test.sh file_name_test.txt 5

5) It only prints the first line which is:

<header> asdfdsafdsf

in the output file overflow_new2. and return these lines in putty:

awk: Field $() is not correct.
The input line number is 2. The file is file_name_test.txt
The source line number is 2.

Any idea? Thank you.

  • 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-15T12:14:02+00:00Added an answer on June 15, 2026 at 12:14 pm

    You need to pass the shell variables to awk using -v flag:

    filename=$1
    threshold=$2
    
    awk -v thres="$threshold" '
        { line_count++ }
        line_count==1 || line_count > thres { print }
    ' $filename > overflow_new2
    

    When run like:

    ./script.sh file_name_test.txt 5
    

    Results/contents of overflow_new2:

    **<header> asdfdsafdsf**  
    **15 asd asdfsdf
    16 sadfsadfsaf sdfsdf
    17 asdf sdaf
    18 asfd saf
    19 sadf asdf
    10 asf asf**
    

    Also, to reproduce the desired results exactly, here’s the way I’d do it:

    filename=$1
    threshold=$2
    
    awk -v thres="$threshold" '
        FNR == 1 {
            sub(/**\s*$/,"")
            print
        }
        FNR > thres {
            sub(/^**/,"")
            print
        }
    ' $filename > overflow_new2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to extract some specific lines from a text file to other text
I would like to extract some text from an html file using Regex. I
I would like to extract some lines from a text file, I have started
im trying to extract some files from a jar-file downloaded using java-webstart. below code
When I extract text from an XML file Here is some text before the
I would like to extract 2 pieces information from a text file using the
I'm trying to extract two blocks of code from a text file using Java
I need to extract text from pdf files using iText. The problem is: some
I have a 1.3GB text file that I need to extract some information from
I'm trying to use WWW::Mechanize to extract some links from the HTML page using

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.