grep help after context


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep help after context
# 1  
Old 05-08-2010
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.
Code:
 
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

Code:
 
./ftp_files 1

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

Code:
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
Code:
grep -A3 "FILE_ID=$1" ftp_info_file

Now the problem is that no. of lines between
Code:
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
Code:
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
# 2  
Old 05-08-2010
Something like this?
Code:
awk -F"=" -v id="$1" '$2==id{f=1}f && !NF{exit}f' ftp_info_file

# 3  
Old 05-08-2010
Not sure if this is what you want...

ftpdata
Code:
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
Code:
#!/bin/sh

string="FILE_ID=$1"

sed -n '/'$string'/,/^ /p;' ftpdata | sed '$d'

Code:
$ ./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
$


Last edited by pseudocoder; 05-08-2010 at 05:41 PM..
# 4  
Old 05-08-2010
MySQL

Code:
[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 Smilie

# Let Format changing our ftpdata file
Code:
[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
Code:
[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
Code:
[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
Code:
[root@sistem1lnx first]# ./ftp_files 3
FILE_FTP_ID=ftp.server3.com
FILE_FTP_USER=user3
FILE_FTP_PASS=pass3

And other exa..
Code:
[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 Smilie

Regards ygemici
# 5  
Old 05-09-2010
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 Smilie; which is useless!)
Quote:
Code:
awk -F"=" -v id="$1" '$2==id{f=1}f && !NF{exit}f' ftp_info_file

pseudocoder- I tried your command
Code:
sed -n '/'FILE_ID=1'/,/^ /p;' ftp_info_file | sed '$d'

but it still prints the whole file.
Code:
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
# 6  
Old 05-09-2010
Quote:
Originally Posted by dips_ag
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 Smilie; which is useless!)
Sure.Smilie
Code:
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
# 7  
Old 05-10-2010
Thanks Franklin52 for the explanation.
-dips
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Questions related to if in awk context and if without awk context

I wrote this code, questions follow #! /bin/bash -f # Purpose - to show how if syntax is used within an awk clear; ls -l; echo "This will print out the first two columns of the inputted file in this directory"; echo "Enter filename found in this directory"; read input; ... (11 Replies)
Discussion started by: Seth
11 Replies

2. UNIX for Advanced & Expert Users

Interupt Context Switching

If suppose a middle level interrupt is being serviced and a high priority interrupts comes in then in that case what all process will take place. The interrupt context switch will happen. But where will the interrupt context be saved? Is there something called as part process data area? (4 Replies)
Discussion started by: rupeshkp728
4 Replies

3. UNIX for Dummies Questions & Answers

Regarding context to add in a file

Hi Folks, I have one query is that I can reach to a location of a file named Integration_Config_3.properties through putty cd /usr/local/pos/jlan/config/byStore/il ls -ltr I can open this file in vi editior also vi Integration_Config_3.properties But now my query is I want to add the... (3 Replies)
Discussion started by: SankalpS
3 Replies

4. Shell Programming and Scripting

Grep multiple search terms with context

I have a file that is a sort library in the format: ##def title1 content1 stuff1 content2 stuff2 ##enddef ##def title2 etc.. I want to grep def and content and pull some trailing context from content so the result would look something like: (1 Reply)
Discussion started by: Moe.Wilensky
1 Replies

5. Slackware

Context dependent symlinks

Ive got multiple PCs, sharing an NFS mounted home dir. For certain apps I would like to keep the config files host specific. Easy solution is to create symlinks to local folders for configs. Ideally I would still want the .config files to reside in the user home folder. Is it possible to... (2 Replies)
Discussion started by: agentrnge
2 Replies

6. Homework & Coursework Questions

Context-switching - vmstat

1. The problem statement, all variables and given/known data: Type `vmstat -s; vmstat -n 1 5; vmstat -n 1 5; vmstat -s` to your Ruby interpreter. Then terminate your Ruby session. Run the Unix com- mand vmstat -s; vmstat -n 1 5; vmstat -s in the same terminal window you had been using for... (2 Replies)
Discussion started by: snowboarder
2 Replies

7. UNIX for Advanced & Expert Users

context lost problem

there are several same servers(process) on more than one server(machine) providing the same service. we store session/context within the server(process), if the same client login, he will be directed to the very server service for him last time. But, if a server(machine or process) down, the... (1 Reply)
Discussion started by: zhongyj
1 Replies

8. Shell Programming and Scripting

keep context in awk

here is a data file. ------------------------------------- KSH, CSH, BASH, PERL, PHP, SED, AWK KSH, CSH, BASH, PERL, PHP, BASH, PERL, PHP, SED, AWK CSH, BASH, PERL, PHP, SED, KSH, CSH, BASH, PERL, PHP, SED, AWK ------------------------------------- My desired output is... (2 Replies)
Discussion started by: VTAWKVT
2 Replies

9. UNIX for Advanced & Expert Users

Context Switching

I know that this is a relative question but can someone give me an idea of what would be considered a high number of context switches? I am running vmstat and show a cs value of between 5000 and 6000 on a 4 processor system. How can I deduce if this number is high or not? Also, the timeslice... (2 Replies)
Discussion started by: keelba
2 Replies

10. Programming

Context Free Grammar

Hi Folks, I am trying to write a small compiler for syntax checking in C programs. I would like to attach a Context Free Grammar(CFC) to it. Does any one know as to how I should associate or map the Grammar with the C program???? A quick help would help me a lot...... Thanks, Nisha (4 Replies)
Discussion started by: Nisha
4 Replies
Login or Register to Ask a Question