The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to Select or cut from the certain filed to the end of the line grajesh_955 Shell Programming and Scripting 5 04-10-2008 04:07 AM
awk program to select a portion of a line anju Shell Programming and Scripting 3 01-11-2008 03:33 AM
Need to replace the first word of a line if it occurs again in the next line(shell) geeko Shell Programming and Scripting 1 09-25-2007 07:15 AM
Select matches between line number and end of file? Jerrad Shell Programming and Scripting 4 05-24-2006 04:50 AM
awk to select a column from particular line number mab_arif16 Shell Programming and Scripting 4 05-08-2006 02:26 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 10-11-2006
Registered User
 

Join Date: Jun 2006
Posts: 59
How to select line by line in shell

Hi,
This is what the file data.lst contains.


aaaaaaaa eeeeeeeeeeeeeeee 4444444 rrrrrrrrrrrrr tttttttttttt
bbbbbbbb eeeeeeeeeeeeeeee 7777777 uuuuuuuu eeeeeeeee
qqqqqqqq gggggggggggggggg 6666666 oooooooo ppppppppp

Here I want to cut the third field and put it in a new file
I tried this way

echo
for i in `cat data.lst`
do
echo $i | cut -c 30-37 >> newfile.lst
done



But this doesn't work.Can someone help me.
Thanks in advance

Last edited by preethgideon; 10-11-2006 at 03:28 PM.
Reply With Quote
Forum Sponsor
  #2  
Old 10-11-2006
Registered User
 

Join Date: Aug 2006
Posts: 122
You need a space ie cut -c 30-37

Also try cat data.lst | awk '{print $3}' > /tmp/newfile
Reply With Quote
  #3  
Old 10-11-2006
Registered User
 

Join Date: Jun 2006
Posts: 59
Hey Andrek,
The awk worked.I did't get the first one.Anyways thank you very much.

Have a great day.Bye
Reply With Quote
  #4  
Old 10-11-2006
Registered User
 

Join Date: Jun 2006
Posts: 59
Hi Andrek,
I have another problem.
I have have a similar file this way


aaaaaaaa eeeeeeeeeeeeeeee 4444R16 rrrrrrrrrrrrr tttttttttttt
bbbbbbbb eeeeeeeeeeeeeeee 7777777 uuuuuuuu eeeeeeeee
qqqqqqqq gggggggggggggggg 6666R16 oooooooo ppppppppp

In some lines last 3 digits of the third field ends with R16.If it ends with R16 then concatinate to a new file else proceed.

Help !!
Reply With Quote
  #5  
Old 10-11-2006
Registered User
 

Join Date: Sep 2006
Posts: 1,580
Python alternative:
Code:
o = open("output","a")
for line in open("input.txt"):
	line = line.strip().split()
	if line[2].endswith("R16"):
		print >>o, ' '.join(line)
o.close()
Output:
/home> python test.py
aaaaaaaa eeeeeeeeeeeeeeee 4444R16 rrrrrrrrrrrrr tttttttttttt
qqqqqqqq gggggggggggggggg 6666R16 oooooooo ppppppppp
Reply With Quote
  #6  
Old 10-11-2006
Registered User
 

Join Date: Jun 2006
Posts: 59
I am using vi.Will I be able to use this code ?

Its giving the error for sysntax.
Reply With Quote
  #7  
Old 10-11-2006
Registered User
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,397
Quote:
Originally Posted by preethgideon
Hi,
This is what the file data.lst contains.


aaaaaaaa eeeeeeeeeeeeeeee 4444444 rrrrrrrrrrrrr tttttttttttt
bbbbbbbb eeeeeeeeeeeeeeee 7777777 uuuuuuuu eeeeeeeee
qqqqqqqq gggggggggggggggg 6666666 oooooooo ppppppppp

Here I want to cut the third field and put it in a new file
I tried this way

echo
for i in `cat data.lst`
do
echo $i | cut -c 30-37 >> newfile.lst
done



But this doesn't work.Can someone help me.
Thanks in advance
Code:
cut -d" " -f3 data.lst > newfile.lst
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 04:33 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0