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.

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.g:)

$ ruby -ne '( u=$_.scan(/-u\s*(.*?)-P\s*(.*?)\s+/)[0];print "user:#{u[0]}, pass:#{u[1]}\n" ) if /sqsh/' file

This could help..may be

grep sqsh | awk -F- '{print $2,$3}' inputfile
1 Like

i have one more doubt in the below command.

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

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.

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, PASSWD }
 } ' *.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.

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

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