LINES and FIRST WORDS of THOSE LINES


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting LINES and FIRST WORDS of THOSE LINES
# 1  
Old 01-24-2013
LINES and FIRST WORDS of THOSE LINES

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

I have a file 1.txt. It looks like

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

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

Code:
apple
table
tea
bed

My script looks like
Code:
#!/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?

Last edited by joeyg; 01-24-2013 at 11:19 AM.. Reason: Please wrap commands and data with CodeTags
# 2  
Old 01-24-2013
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.
# 3  
Old 01-24-2013
why not simply...

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

# 4  
Old 01-24-2013
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 ;(
# 5  
Old 01-24-2013
try:
Code:
#!/bin/sh
 
while read a b
do
   echo $a
done < 1.txt

# 6  
Old 01-24-2013
Did you check joeyg's post - #3

You just need to run a single line awk program:
Code:
awk '{print $1}' 1.txt

# 7  
Old 01-24-2013
Quote:
Originally Posted by rdrtx1
try:
Code:
#!/bin/sh
 
while read a b
do
   echo $a
done < 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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to sort lines according words?

Hello I greped some lines from an xml file and generated a new file. but some entries are missing my table is unsorted. e.g. NAME="Adel" ADDRESS="Donaustr." NUMBER="2" POSTCODE="33333" NAME="Adel" ADDRESS="Donaustr." NUMBER="2" POSTCODE="33333" NAME="Adel" NUMBER="2" POSTCODE="33333"... (5 Replies)
Discussion started by: witchblade
5 Replies

2. Shell Programming and Scripting

how to print specific lines or words

Hi, Please have a look on below records. STG_HCM_STATE_DIS_TAX_TBL.1207.Xfm: The value of the row is: EMPLID = 220677 COMPANY = 919 BALANCE_ID = 0 BALANCE_YEAR = 2012 STG_HCM_STATE_DIS_TAX_TBL.1207.Xfm: ORA-00001: unique constraint (SYSADM.PS_TAX_BALANCE) violated ... (4 Replies)
Discussion started by: Sachin Lakka
4 Replies

3. UNIX for Dummies Questions & Answers

Extract lines with specific words with addition 2 lines before and after

Dear all, Greetings. I would like to ask for your help to extract lines with specific words in addition 2 lines before and after these lines by using awk or sed. For example, the input file is: 1 ak1 abc1.0 1 ak2 abc1.0 1 ak3 abc1.0 1 ak4 abc1.0 1 ak5 abc1.1 1 ak6 abc1.1 1 ak7... (7 Replies)
Discussion started by: Amanda Low
7 Replies

4. Shell Programming and Scripting

Bringing together words from multiple lines

I want to get a file together that lists router name and IP address of an interface together like so... SOMERTR1A 10.10.10.20 SOMERTR1B 10.10.10.30 OTHRRTR1A 192.168.1.120 The file I'm trying to extract the text from looks like the below: SOMERTR1A#show run int Lo0 | i add ... (5 Replies)
Discussion started by: branrobi
5 Replies

5. Shell Programming and Scripting

Keep lines with specific words up in an order

I hava a file with following data: number|CREDIT_ID|NULL date|SYS_CREATION_DATE|NULL varchar2|GGS_COMMIT_CHAR|NULL varchar2|GGS_OP_TYPE|NULL number|GGS_SCN|NULL| number|GGS_LOG_SEQ|NULL number|GGS_LOG_POS|NULL number|GGS_ORACREC_SCN|NULL varchar2|BATCH_ID|NULL char|GGS_IMAGE_TYPE|NULL ... (6 Replies)
Discussion started by: kolesunil
6 Replies

6. Shell Programming and Scripting

Count the no of lines between two words

Please help in the following problem: Input is: Pritam 123 456 Patil myname youname Pritam myproject thisproject iclic Patil remaining text some more text I need the command which will display the no of lines between two words in the whole file. e.g. Display all the no of lines... (5 Replies)
Discussion started by: zsudarshan
5 Replies

7. Shell Programming and Scripting

how to remove words between /* and */ in several lines of file

hi, I want to remove comments in somany files. coments started from /* and ends with */. so i wnt to remove words /* to */ how to remove words between /* and */ in several lines of file EX : cat haha.txt "HI",/*hk*/ob1,raju,/*hjh*/boju,beeju output should be : "HI",ob1,raju,boju,beeju ... (8 Replies)
Discussion started by: spc432
8 Replies

8. Shell Programming and Scripting

Adding words to beginning of lines

I have a file that contains a great number of lines, let's say 183 lines, and I want to add: echo " to the beginning of each line. What is the easiest way to do it? Tx (9 Replies)
Discussion started by: Ernst
9 Replies

9. Shell Programming and Scripting

Delete lines that contain 3 or more words?

How can I delete lines that contain 3 or more words? I have a file, old.txt, that has multi-word phrases in it and I want to remove the lines with 3 words or more and send the output to new.txt. I've tried the following using sed but it doesn't seem to work: sed '/(\b\w+\b){3,}/d' old.txt >... (5 Replies)
Discussion started by: revax
5 Replies

10. UNIX for Dummies Questions & Answers

How to count lines - ignoring blank lines and commented lines

What is the command to count lines in a files, but ignore blank lines and commented lines? I have a file with 4 sections in it, and I want each section to be counted, not including the blank lines and comments... and then totalled at the end. Here is an example of what I would like my... (6 Replies)
Discussion started by: kthatch
6 Replies
Login or Register to Ask a Question