The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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
Need to replace the first word of a line if it occurs again in the next line(shell) geeko Shell Programming and Scripting 4 06-18-2009 02:36 PM
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 07:07 AM
awk program to select a portion of a line anju Shell Programming and Scripting 3 01-11-2008 06:33 AM
Select matches between line number and end of file? Jerrad Shell Programming and Scripting 4 05-24-2006 07:50 AM
awk to select a column from particular line number mab_arif16 Shell Programming and Scripting 4 05-08-2006 05:26 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-11-2006
preethgideon preethgideon is offline
Registered User
  
 

Join Date: Jun 2006
Posts: 71
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 06:28 PM..
  #2 (permalink)  
Old 10-11-2006
Andrek Andrek is offline
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
  #3 (permalink)  
Old 10-11-2006
preethgideon preethgideon is offline
Registered User
  
 

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

Have a great day.Bye
  #4 (permalink)  
Old 10-11-2006
preethgideon preethgideon is offline
Registered User
  
 

Join Date: Jun 2006
Posts: 71
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 !!
  #5 (permalink)  
Old 10-11-2006
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,513
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
  #6 (permalink)  
Old 10-11-2006
preethgideon preethgideon is offline
Registered User
  
 

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

Its giving the error for sysntax.
  #7 (permalink)  
Old 10-11-2006
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Quote:
Originally Posted by preethgideon
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 !!
To send numbers with R16 to file filewithR16

Code:
sed -n "/R16/p" file | sed -n "s/\([^0-9]*\)\([0-9]\{4\}R16\).*/\2/w filewithR16"
or
Code:
cut -d" " -f3 file | grep "R16" > filewithR16
or
Code:
awk ' $3 ~ "R16" { print $3 }' file > filewithR16
To send numbers without R16 to file filewithoutR16

Code:
sed -n "/R16/! p" file | sed -n "s/\([^0-9]*\)\([0-9][0-9]*\).*/\2/w filewithoutR16"
or
Code:
cut -d" " -f3 file | grep -v "R16" > filewithoutR16
or
Code:
awk ' $3 !~ "R16" { print $3 }' file > filewithR16
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:53 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0