LINES and FIRST WORDS of THOSE LINES

Hi everyone! I need your help. Can not find out what I am doing wrong :frowning:

I have a file 1.txt. It looks like

apple orange juice
table computer banana
tea weather home
bed water mandarin

So I need to print only first words of each line like

apple
table
tea
bed

My script looks like

#!/bin/sh
for i in `cat 1.txt`
do
echo `cat 1.txt`| awk '{print $1}' 1.txt
done

But it do not work properly ;( can anyone help me?

Do not post classroom or homework problems in the main forums. Homework and coursework questions can only be posted in this forum under special homework rules.

Please review the rules, which you agreed to when you registered, if you have not already done so.

More-than-likely, posting homework in the main forums has resulting in a forum infraction. If you did not post homework, please explain the company you work for and the nature of the problem you are working on.

If you did post homework in the main forums, please review the guidelines for posting homework and repost.

Thank You.

The UNIX and Linux Forums.

awk '{print $1}' <1.txt

when I run this
#!/bin/sh
for line in `cat 1.txt`
do
#echo "$line"
echo awk `{print $1}` < 1.txt
done
it gives me
apple
orange
juice
table
etc.

But I need only
apple
table
tea
bed ;(

try:

#!/bin/sh
 
while read a b
do
   echo $a
done < 1.txt

Did you check joeyg's post - #3

You just need to run a single line awk program:

awk '{print $1}' 1.txt

If you do not mind can explain why a b? what does it mean???? sorry, i am new in shell....P.S. it works btw, I just want to understand. Thank you for helping

read first word of line into variable a the rest goes into variable b .

1 Like

Thread closed as it seems to be homework