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
# 8  
Old 10-11-2006
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

# 9  
Old 10-11-2006
Shince you asked for shell:

Code:
while read a b c junk; do
     echo $c
done < file >newfile

# 10  
Old 10-12-2006
hi,

This is the code for getting lines with last 3 digits of the third field ends with R16.

awk ' $3 ~ "R16$" { print $3 }' file > filewithR16
# 11  
Old 10-12-2006
Quote:
Originally Posted by yashavant.a
hi,

This is the code for getting lines with last 3 digits of the third field ends with R16.

awk ' $3 ~ "R16$" { print $3 }' file > filewithR16
But this gives me only the third fiels but what if I need the whole line.
# 12  
Old 10-12-2006
Quote:
Originally Posted by preethgideon
But this gives me only the third fiels but what if I need the whole line.
Code:
awk ' $3 ~ "R16$" { print  }' file > filewithR16

# 13  
Old 10-12-2006
Then print the whole line instead of just the third field. Change
Code:
awk ' $3 ~ "R16$" { print $3 }' file > filewithR16

to
Code:
awk ' $3 ~ "R16$" { print $0 }' file > filewithR16

A basic understanding of awk would help.
Code:
man awk

# 14  
Old 10-12-2006
Hi Anbu
The commnad sed -n "/R16/p" file | sed -n "s/\([^0-9]*\)\([0-9]\{4\}R16\).*/\2/w filewithR16"
It only looks for R16 at the end of any fields if it finds it then it will place till that point in the newfile.But I want the whole line.
 
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