grep help after context

Hi,

There's a file with below contents which I have to read based on the input parameter provided by the user.

 
FILE_ID=1
FILE_FTP_ID=ftp.server1.com
FILE_FTP_USER=user1
FILE_FTP_PASS=pass1
 
FILE_ID=2
FILE_FTP_ID=ftp.server2.com
FILE_FTP_USER=user2
FILE_FTP_PASS=pass2
 
FILE_ID=3
FILE_FTP_ID=ftp.server3.com
FILE_FTP_USER=user3
FILE_FTP_PASS=pass3
 
FILE_ID=4
FILE_FTP_ID=ftp.server4.com
FILE_FTP_USER=user4
FILE_FTP_PASS=pass4
 

For e.g. if user entered 1

 
./ftp_files 1

Then the script should only read the first block i.e. and assign the values for ftp site, user & pass,etc....

FILE_ID=1
FILE_FTP_ID=ftp.server1.com
FILE_FTP_USER=user1
FILE_FTP_PASS=pass1

which I am doing with the below command

grep -A3 "FILE_ID=$1" ftp_info_file

Now the problem is that no. of lines between

FILE_ID=1
FILE_ID=2

may vary (there could be more information to read); so -A3 would not help me. I need to know dynamically some how the no. of lines between the first and the next pattern.
I thought of something like using -m & -n options of grep

grep -m `cat ftp_info_file | wc -l` "FILE_ID" -n ftp_info_file

which displays lines matched with the line numbers and then I can subtract those numbers to get no. of lines between the two contiguous groups of matches. But I am wondering if there's a neater way to do it?
Please suggest.

-dips

Something like this?

awk -F"=" -v id="$1" '$2==id{f=1}f && !NF{exit}f' ftp_info_file

Not sure if this is what you want...

ftpdata

FILE_ID=1
FILE_FTP_ID=ftp.server1.com
FILE_FTP_USER=user1
FILE_FTP_PASS=pass1
 
FILE_ID=2
FILE_FTP_ID=ftp.server2.com
FILE_FTP_USER=user2
FILE_FTP_PASS=pass2
FILE_FTP_PARAM_1=param1
FILE_FTP_PARAM_2=param2
 
FILE_ID=3
FILE_FTP_ID=ftp.server3.com
FILE_FTP_USER=user3
FILE_FTP_PASS=pass3
FILE_FTP_VALUE_1=val1
FILE_FTP_VALUE_2=val2
FILE_FTP_VALUE_3=val3
 
FILE_ID=4
FILE_FTP_ID=ftp.server4.com
FILE_FTP_USER=user4
FILE_FTP_PASS=pass4

getdata.sh

#!/bin/sh

string="FILE_ID=$1"

sed -n '/'$string'/,/^ /p;' ftpdata | sed '$d'
$ ./getdata.sh 1
FILE_ID=1
FILE_FTP_ID=ftp.server1.com
FILE_FTP_USER=user1
FILE_FTP_PASS=pass1
$ ./getdata.sh 2
FILE_ID=2
FILE_FTP_ID=ftp.server2.com
FILE_FTP_USER=user2
FILE_FTP_PASS=pass2
FILE_FTP_PARAM_1=param1
FILE_FTP_PARAM_2=param2
$ ./getdata.sh 3
FILE_ID=3
FILE_FTP_ID=ftp.server3.com
FILE_FTP_USER=user3
FILE_FTP_PASS=pass3
FILE_FTP_VALUE_1=val1
FILE_FTP_VALUE_2=val2
FILE_FTP_VALUE_3=val3
$ ./getdata.sh 4
FILE_ID=4
FILE_FTP_ID=ftp.server4.com
FILE_FTP_USER=user4
FILE_FTP_PASS=pass4
$ 
[root@sistem1lnx first]# cat ftpfile
FILE_ID=1
FILE_FTP_ID=ftp.server1.com
FILE_FTP_USER=user1
FILE_FTP_PASS=pass1

FILE_ID=2
FILE_FTP_ID=ftp.server2.com
FILE_FTP_USER=user2
FILE_FTP_PASS=pass2

FILE_ID=3
FILE_FTP_ID=ftp.server3.com
FILE_FTP_USER=user3
FILE_FTP_PASS=pass3

FILE_ID=4
FILE_FTP_ID=ftp.server4.com
FILE_FTP_USER=user4
FILE_FTP_PASS=pass4

First of all we change your ftpdata file :wink:

# Let Format changing our ftpdata file

[root@sistem1lnx first]# cat ftpfile | sed -e 's/.*=//g' | sed -e :j -e '$b;N;s/\n/ /;bj' | sed -e 's/  /\n/g' > newftpfile

# Our newftpfile format

[root@sistem1lnx first]# cat newftpfile
1 ftp.server1.com user1 pass1
2 ftp.server2.com user2 pass2
3 ftp.server3.com user3 pass3
4 ftp.server4.com user4 pass4

And then ;
# Let Modifying our script with our newftpdata file

[root@sistem1lnx first]# cat ftp_files

#!/bin/bash
while read ftpid ftpadr ftpusr ftppass
  do
    if [ $ftpid = $1 ] ; then
  FILE_FTP_ID=$ftpadr
  FILE_FTP_USER=$ftpusr
  FILE_FTP_PASS=$ftppass
   fi
 done < newftpfile

# test
echo "FILE_FTP_ID="$FILE_FTP_ID
echo "FILE_FTP_USER="$FILE_FTP_USER
echo "FILE_FTP_PASS="$FILE_FTP_PASS

# Now let start our ftp script

[root@sistem1lnx first]# ./ftp_files 3
FILE_FTP_ID=ftp.server3.com
FILE_FTP_USER=user3
FILE_FTP_PASS=pass3

And other exa..

[root@sistem1lnx first]# ./ftp_files 1
FILE_FTP_ID=ftp.server1.com
FILE_FTP_USER=user1
FILE_FTP_PASS=pass1

[root@sistem1lnx first]# ./ftp_files 4
FILE_FTP_ID=ftp.server4.com
FILE_FTP_USER=user4
FILE_FTP_PASS=pass4

I hope this job is OK :slight_smile:

Regards ygemici

Thanks once again Franklin52 your solution works perfectly!

But can you please expalin the command (I understand bits of it but not the whole of it :(; which is useless!)

pseudocoder- I tried your command

sed -n '/'FILE_ID=1'/,/^ /p;' ftp_info_file | sed '$d'

but it still prints the whole file.

FILE_ID=1
FILE_FTP_ID=ftp.server1.com
FILE_FTP_USER=user1
FILE_FTP_PASS=pass1

FILE_ID=2
FILE_FTP_ID=ftp.server2.com
FILE_FTP_USER=user2
FILE_FTP_PASS=pass2

FILE_ID=3
FILE_FTP_ID=ftp.server3.com
FILE_FTP_USER=user3
FILE_FTP_PASS=pass3

FILE_ID=4
FILE_FTP_ID=ftp.server4.com
FILE_FTP_USER=user4
FILE_FTP_PASS=pass4

-dips

Sure.:slight_smile:

awk -F"=" -v id="$1" '$2==id{f=1}f && !NF{exit}f'

-F"=" < set fieldseparator

-v id="$1" < set awk variable

$2==id{f=1} < if field 2 == id set flag

f && !NF{exit} < exit if flag is set and record doesn't have a field

f < print the record if flag is set

Thanks Franklin52 for the explanation.
-dips