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,

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...

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

How many keywords do you have?
If they are not more,

$ 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?!!

1 Like

Maybe:

[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
1 Like

Here is the code

cat filename | awk '
/ftp SERVER/{ print $0;
              i = 1;
              while ( i < 3  )
              { getline;
                print $0;
                i++;
              }
            }'
1 Like

With awk:

awk '/ftp/ || /user/ || /send/' file
1 Like

If all you want are sections whose first line begins with "ftp" and whose last line begins with "send":

sed -n '/^ftp/,/^send/p'

Regards,
Alister

1 Like

@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 :slight_smile: 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.