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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:16:41+00:00 2026-05-27T05:16:41+00:00

I am pretty new to bash commands, and I’m trying to make a for

  • 0

I am pretty new to bash commands, and I’m trying to make a for statement that prints out each line with a value of 3 in the 6th column (space delimited) to a file. My syntax looks like this:

for i in `cat test`
do
section=`echo ${i} | cut -d ' ' -f 6`
line=`echo ${i}`
if [ ${section} == "3" ];
then
echo ${line} > test.out
fi
done

Unfortunately, this doesn’t work… Can anyone tell me what’s wrong with this?

  • 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-05-27T05:16:41+00:00Added an answer on May 27, 2026 at 5:16 am

    You’re not reading the file the right way.

    You can debug it by trying:

    for i in `cat file.txt`
        echo $i
    done
    

    you will see that it prints each word on a new line, because the for-loop is actually iterating over each word in the file, instead of each line.

    To read a file line by line use the following:

    while IFS= read -r line
    do
         echo "$line"
    done < file.txt
    

    By the way, if you want an alternative solution, you can use this awk one-liner:

    awk '$6 == "3"' test > test.out
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm pretty new to c# and I'm trying to make a function that checks
Pretty new to dependency injection and I'm trying to figure out if this is
Pretty new to ASP.NET and trying to figure out how to have the code
I'm still pretty new to asp.net web forms. I have a requirement to make
I'm trying to write (what I thought would be) a simple bash script that
Pretty new to sqlite (and sql). Trying to modify one table using another. create
im pretty new to jQuery, and i dont know how to do that, and
I'm pretty new to bash scripting and I'm obviously missing some fairly basic understanding
I am very new to bash. So if this is a pretty basic question,
Am pretty new to SharePoint, am working on creating a custom permission that 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.