Selecting a range of Lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Selecting a range of Lines
# 1  
Old 07-14-2010
Selecting a range of Lines

Hi All,

Is there a way to get a range of lines from a file??? I want to search through a set of scripts and need to select the group of lines which do the FTP.

Say,
Code:
Line1
Line2
ftp SERVER
user UNAME PASS
send FILE_TO_BE_SENT
close
Line3
Line4
Line5
ftp SERVER1
user USER1 PASS1
send FILE_TO_BE_SENT_1
Line6
Line6

From the above code fragments, I need to select the lines where the FTP is happening...
Code:
ftp SERVER
user UNAME PASS
send FILE_TO_BE_SENT
ftp SERVER1
user USER1 PASS1
send FILE_TO_BE_SENT_1.

Using grep, i am not able to do this.. and sed also has not came handy. Is there any ways??

Thanks
beinthemiddle

Last edited by Franklin52; 07-14-2010 at 08:00 AM.. Reason: Please use code tags
# 2  
Old 07-14-2010
How many keywords do you have?
If they are not more,

Code:
$ egrep '^ftp|^user|^send' file
ftp SERVER
user UNAME PASS
send FILE_TO_BE_SENT
ftp SERVER1
user USER1 PASS1
send FILE_TO_BE_SENT_1
$

if this is not the case, please provide all cases.

also, surprised you don't have "heredoc labels" within the ftp sessions?!!
This User Gave Thanks to clx For This Post:
# 3  
Old 07-14-2010
Maybe:

Code:
[house@leonov] grep -A 2 '^ftp ' data.file
ftp SERVER
user UNAME PASS
send FILE_TO_BE_SENT
--
ftp SERVER1
user USER1 PASS1
send FILE_TO_BE_SENT_1

This User Gave Thanks to dr.house For This Post:
# 4  
Old 07-14-2010
Here is the code
Code:
cat filename | awk '
/ftp SERVER/{ print $0;
              i = 1;
              while ( i < 3  )
              { getline;
                print $0;
                i++;
              }
            }'


Last edited by Franklin52; 07-14-2010 at 08:05 AM.. Reason: Please use code tags
This User Gave Thanks to rujuraha For This Post:
# 5  
Old 07-14-2010
With awk:
Code:
awk '/ftp/ || /user/ || /send/' file

This User Gave Thanks to Franklin52 For This Post:
# 6  
Old 07-14-2010
If all you want are sections whose first line begins with "ftp" and whose last line begins with "send":
Code:
sed -n '/^ftp/,/^send/p'

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 7  
Old 07-14-2010
@anchal_khare
It was just an example code which simulates my scenario. Thanks for the suggestion.

@dr.house
My system do not support grep with keyword -A. Unfortunately I cannot use it. But if I need to , I will get a GNU grep and will surely use ur suggestion. Thanks for your suggestion.

@rujuraha
Your suggestion worked fine for me. Lots Of Smiles :-) and Thank you.


@Franklin52
I am in the process of learning awk. Thanks for your suggestion. I will use it.

@alister,
I am a bit hesitant to use sed. I dont know exactly. Anyway thanks for ur suggestions.

Thanks All for those replies... I am able to go to the next level of my project. unix.com always rocks. Keep up the good work.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Selecting lines having same values for first two columns

Hello to all. This is first post. Kindly excuse me if I do not adhere to any rules and regulations of this forum. I have a file containing some rows with three columns each per row(separeted by a space). There are certain rows for which first two columns have same value but the value in... (6 Replies)
Discussion started by: manojmalhotra13
6 Replies

2. UNIX for Dummies Questions & Answers

Selecting highest value within a range

Within millions of lines of data, there are perhaps 20 "spikes" that are very narrow. I want only the highest value from each spike within a range of 10 rows. Possible? My data looks like this, 8 columns of integers, millions of rows. There are clear spikes when you graph columns. ... (6 Replies)
Discussion started by: markymarkg123
6 Replies

3. Shell Programming and Scripting

selecting and deleting specific lines with condition

I have a set of data as below: The first field, $1 represent "|". The $3 (3rd field) and $6 (6th field) in my data file represent "number-molecule" which has arrangement as below: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 ... (9 Replies)
Discussion started by: vjramana
9 Replies

4. Shell Programming and Scripting

selecting lines using awk

I have a file which contains five hundred thousand lines (500,000). I want to select lines of every 1000 lines from that file using AWK. I can think something like this in bash as below: for lines in {0..500000..1000} do ........ ........ done But I want to use AWK. I do not know how to... (4 Replies)
Discussion started by: vjramana
4 Replies

5. Shell Programming and Scripting

Selecting lines of a file

Say I wanted to select the 5th line of a file without knowing the context of the file. Would I use grep and pipe it into wc or is there a more simple way of doing this? (3 Replies)
Discussion started by: puttster
3 Replies

6. Shell Programming and Scripting

Selecting lines with sed

Hi all, I have a file with special characters like this file1 691775025 ýÄqJ8^Z^Y{ 2004-08-23E P 100.00 45585025 0527541139295037342008-07-25OEP 100.00 6983025 ýB<9D>x<^F^Xb 2004-11-16SPP 100.00 I need a sed command to print the lines which don't have special characters.ie., only line 2... (9 Replies)
Discussion started by: allinshell
9 Replies

7. Shell Programming and Scripting

about selecting lines

Hello , i got text file like that' C:\Users\Public\Pictures\Sample Pictures\aa.jpg C:\Users\Public\Pictures\Sample Pictures\thumb.jpg C:\Users\Public\Pictures\vv\cc.jpg C:\Users\Public\Pictures\Sample Pictures\ee.jpg C:\Users\Public\aa\Sample Pictures\cvswsr.jpg... (1 Reply)
Discussion started by: davidkhan
1 Replies

8. Shell Programming and Scripting

Selecting Lines on text file

Hi All, I am creating a script that sends log data from text files to a Database and I will like to read sugestions, as I think that there might be better ways to achive this than with my shell script; maybe perl or I don't know, but I will like to read some sugestions. The log is from... (10 Replies)
Discussion started by: oconmx
10 Replies

9. Shell Programming and Scripting

Selecting files between a user inputed date range

Hi all! I'm working on a KSH script to select files between a user inputed date range (stored in a variable) and then move them and unzip them. I'm stuck at how to get the files between the user inputed date range selected. Any help would be greatly appreciated! The files are as such: ... (6 Replies)
Discussion started by: kelldan
6 Replies

10. Shell Programming and Scripting

selecting only few lines from many based on a common pattern

Hi, I have a file having some thousand records with the following sort of lines: Error: Failed to get order data Order: PO-BBBTGZE Error: No CLI Error: Failed to get order data Order: PO-SBDJUZA Order: PO-XBBIDEN Error: No CLI Error: Failed to get order data Order: PO-BBDJUTQ Order:... (2 Replies)
Discussion started by: damansingh
2 Replies
Login or Register to Ask a Question