How to select line by line in shell


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to select line by line in shell
# 1  
Old 10-11-2006
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 07:28 PM..
# 2  
Old 10-11-2006
You need a space ie cut -c 30-37

Also try cat data.lst | awk '{print $3}' > /tmp/newfile
# 3  
Old 10-11-2006
Hey Andrek,
The awk worked.I did't get the first one.Anyways thank you very much.

Have a great day.Bye
# 4  
Old 10-11-2006
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  
Old 10-11-2006
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  
Old 10-11-2006
I am using vi.Will I be able to use this code ?

Its giving the error for sysntax.
# 7  
Old 10-11-2006
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

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Get an output of lines in pattern 1st line then 10th line then 11th line then 20th line and so on.

Input file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 (6 Replies)
Discussion started by: Sagar Singh
6 Replies

2. Shell Programming and Scripting

Shell script UNIX to read text file line by line

i have a text file as belows, it includes 2 columns, 1st is the column name, 2nd is the file_name data_file.txt column_name file_name col1 file1 col2 file2 col3 file1 col4 file1 col5 file2 now, i would like to... (4 Replies)
Discussion started by: tester111
4 Replies

3. Shell Programming and Scripting

How to read a two files, line by line in UNIX script and how to assign shell variable to awk ..?

Input are file and file1 file contains store.bal product.bal category.bal admin.bal file1 contains flip.store.bal ::FFFF:BADC:CD28,::FFFF:558E:11C5,6,8,2,1,::FFFF:81C8:CA8B,::FFFF:BADC:CD28,1,0,0,0,::FFFF:81C8:11C5,2,1,0,0,::FFFF:81DC:3111,1,0,1,0 store.bal.... (2 Replies)
Discussion started by: veeruasu
2 Replies

4. Shell Programming and Scripting

Select only the last line from the pattern

Hi, I am really new in the shell script, but it is really useful for me to learn. I have one question, I have a large text document (actually few of them), inside there are lines with information about energies, between 10 to 20 of this lines, varies from one doc to another one, my questions... (11 Replies)
Discussion started by: hmartine1983
11 Replies

5. Shell Programming and Scripting

Shell script to read multiple options from file, line by line

Hi all I have spent half a day trying to create a shell script which reads a configuration file on a line by line basis. The idea of the file is that each will contain server information, such as IP address and various port numbers. The line could also be blank (The file is user created). Here... (1 Reply)
Discussion started by: haggismn
1 Replies

6. Shell Programming and Scripting

PERL or SHELL Scrript to search in Directories by taking line by line from a text file

Unix box server version *********** >uname -r B.11.00 >echo $SHELL /usr/bin/ksh --> in this server, I have the path like /IMbuild/dev/im0serv1 ---> in that directory I have the folders startup(.jsp files nearly 100 jsp's ) and scripts(contains .js files nearly 100 files) ... (9 Replies)
Discussion started by: pasam
9 Replies

7. Shell Programming and Scripting

Print pipe separated list as line by line in Korn Shell

Korn Shell in AIX 6.1 I want to print the below shown pipe (|) separated list line by line. line=es349889|nhb882309|ts00293|snh03524|bg578835|bg37900|rnh00297|py882201|sg175883 for i in line do echo "Hello $line " done I wanted to execute the above for loop. But i can't even set the... (3 Replies)
Discussion started by: polavan
3 Replies

8. Shell Programming and Scripting

Shell script to read a text file line by line & process it...

Hi , I am trying to write an shell, which reads a text file (from a location) having a list of numbers of strictly 5 digits only ex: 33144 Now my script will check : 1) that each entry is only 5 digits & numeric only, no alphabets, & its not empty. 2)then it executes a shell script called... (8 Replies)
Discussion started by: new_to_shell
8 Replies

9. Shell Programming and Scripting

get the fifth line of a text file into a shell script and trim the line to extract a WORD

FOLKS , i have a text file that is generated automatically of an another korn shell script, i want to bring in the fifth line of the text file in to my korn shell script and look for a particular word in the line . Can you all share some thoughts on this one. thanks... Venu (3 Replies)
Discussion started by: venu
3 Replies

10. Shell Programming and Scripting

select a line that contains the strings

hi i have a file, from the file i need to extract the lines that contains both 17 and Received msg string, i used cat <filename> | grep 17 | grep "Received msg" > <new file> but it is not giving the required o/p please help thanks in advance Satya (3 Replies)
Discussion started by: Satyak
3 Replies
Login or Register to Ask a Question