How to write a script to extract strings from a file.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to write a script to extract strings from a file.
# 1  
Old 10-07-2010
How to write a script to extract strings from a file.

Hello fourm members,


I want to write a script to extarct paticular strings from the all type of files(.sh files,logfiles,txtfiles) and redirect into a log file.

example:
I have to find the line below in the script and extract the uname and Pwds.

Code:
sqsh -scia2007 -DD0011uw01 -uciadev -Psysba -w 100

the above lines are available in several files in my user.So I have to find and extract them into a log file.so please suggest me how to write a script file for the above issue.

Thanks in Advance
Rajkumar.gSmilie

Last edited by Scott; 10-16-2010 at 07:32 AM.. Reason: Code tags
# 2  
Old 10-07-2010
Code:
$ ruby -ne '( u=$_.scan(/-u\s*(.*?)-P\s*(.*?)\s+/)[0];print "user:#{u[0]}, pass:#{u[1]}\n" ) if /sqsh/' file

# 3  
Old 10-07-2010
This could help..may be

Code:
grep sqsh | awk -F- '{print $2,$3}' inputfile

This User Gave Thanks to michaelrozar17 For This Post:
# 4  
Old 10-07-2010
Thanks for quick reply

i have one more doubt in the below command.
Code:
grep sqsh | awk -F- '{printf $2,$3}' inputfile

inputfile means ,this strings occured in several files then how to write it.

Thanks Regards.
rajkumar g.Smilie

Last edited by Scott; 10-16-2010 at 07:33 AM.. Reason: Code tags
# 5  
Old 10-07-2010
Welcome to the forum.

In sqsh syntax, I think you must have "-U" for user name instead -u.
Also, if you are not sure about the order of the flags then you need to consider that case.

Code:
awk '
/^sqsh/ { 
    for (i=1;i<=NF;i++) {
	if ( $i ~ /^-U/ ) { 
	   gsub("-U","",$i)
	   USER[NR] = $i
	}
	if ( $i ~ /^-P/ ) { 
	   gsub("-P","",$i)
	   PASSWD[NR] = $i
	}
    }
}
END {
      for (i in USER) { print USER[i], PASSWD[i] }
 } ' *.txt

If you want to consider the files with other patterns also, you can first get the list of all the required files ( find can be useful ) and then read the files one by one.


there could be many other ways.
# 6  
Old 10-07-2010
Quote:
Originally Posted by michaelrozar17
This could help..may be

Code:
grep sqsh | awk -F- '{print $2,$3}' inputfile

That is a useless use of grep ,try this instead:

Code:
awk -F- '/sqsh/ {print $2,$3}' inputfile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match list of strings in File A and compare with File B, C and write to a output file in CSV format

Hi Friends, I'm a great fan of this forum... it has helped me tone my skills in shell scripting. I have a challenge here, which I'm sure you guys would help me in achieving... File A has a list of job ids and I need to compare this with the File B (*.log) and File C (extend *.log) and copy... (6 Replies)
Discussion started by: asnandhakumar
6 Replies

2. Shell Programming and Scripting

Extract two strings from a file and create a new file with these strings

I have the following lines in a log file. It would be great if some one can help me to create a new file with the just entries in the below format. 66.150.161.195 HPSAC=Z05 66.150.161.196 HPSAC=A05 That is just extract the IP address and the string DPSAC=its value 66.150.161.195 -... (1 Reply)
Discussion started by: Tuxidow
1 Replies

3. Shell Programming and Scripting

how to write bash script that will automatically extract zip file

i'm trying to write a bash script that that will automatically extract zip files after the download. i writed this script #!/bin/bash wget -c https://github.com/RonGokhle/kernel-downloader/zipball/master CURRENDIR=/home/kernel-downloader cd $CURRENDIR rm $CURRENDIR/zipfiles 2>/dev/null ... (2 Replies)
Discussion started by: ron gokhle
2 Replies

4. Shell Programming and Scripting

Extract expressions between two strings in html file

Hello guys, I'm trying to extract all the expressions between the following tags: <b></b> from a HTML file. This is how it looks: big lines containing several dozens expressions (made of 1,2,3,4,6 or even 7 words) I would like to extract: <b>bla ble</b>bla ble</td><tr valign="top"><td... (3 Replies)
Discussion started by: bobylapointe
3 Replies

5. Shell Programming and Scripting

Extract strings from file - Help

Hi, I have a file say with following lines (the lines could start from any column and there can be many many create statements in the file) create table table1....table definition... insert into table1 values..... create or replace view view1....view definition.... What i want is to... (2 Replies)
Discussion started by: whoami191
2 Replies

6. Shell Programming and Scripting

Extract strings within XML file between different delimiters

Good afternoon! I have an XML file from which I want to extract only certain elements contained within each line. The problem is that the format of each line is not exactly the same (though similiar). For example, oa_var will be in each line, however, there may be no value or other... (3 Replies)
Discussion started by: bab@faa
3 Replies

7. Shell Programming and Scripting

Concatenate strings retrieved from a file and write it into another file

Hi, I have a file files.txt containing data as below: abc;xyz uvw;pqr 123;456 I want to develop strings like below using the above data and write them into another file: www/xxx/abc/yyy/xyz www/xxx/uvw/yyy/pqr www/xxx/123/yyy/456 All this needs to be done through .sh file. ... (4 Replies)
Discussion started by: archana.n
4 Replies

8. Shell Programming and Scripting

Extract strings from multiple lines into one file -

input file Desired csv output gc_type, date/time, milli secs af, Mar 17 13:09:04 2011, 144.596 af, Mar 20 00:37:37 2011, 144.242 af, ar 20 21:30:59 2011, 108.518 Hi All, Any help in acheiving the above would be appreciated. I would like to parse through lines within one file and... (5 Replies)
Discussion started by: satish.vampire
5 Replies

9. Shell Programming and Scripting

how to write shell script to extract lines we want

hi i have a file which is very large . it contains lines in the format below: seed url, html url .... ... seed url, html url i have sort it already. 2010ÄÏ·ÇÊÀ½ç±*_¾º¼¼·ç±©_ÐÂÀËÍø ÕżªÁúרÀ¸£ºÊÀ½ç±*24ÄêµÄ»ØÒä ÆÚÅÎÑÇÖÞδÀ´ÍŽá_2010ÄÏ·ÇÊÀ½ç±*_¾º¼¼·ç±©_ÐÂÀËÍø 2010ÄÏ·ÇÊÀ½ç±*_¾º¼¼·ç±©_ÐÂÀËÍø ¹úÃ×Óë±´ÄáÌØ˹´ï³ÉÐ*Òé... (6 Replies)
Discussion started by: rainboisterous
6 Replies

10. UNIX for Dummies Questions & Answers

Write a script to extract information from a db

Hi I need to put together a script that will search certain tables in a db and send that data to a csv file. Basically I am importing data to a db and I want to write a script to check that all information was imported correctly. Thank you (1 Reply)
Discussion started by: ladyAnne
1 Replies
Login or Register to Ask a Question