Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

searching for string at column positon

Status
Not open for further replies.

cgswong

MIS
Nov 27, 2000
202
US
Hi,

I'm trying to search for a series of strings in a file (say search.txt has the search strings, one per line and filetst.txt is the file to search in), but I only want to check if the string is in columns 41-51 and if so, write the entire line to a new file. I can't seem to wrap my mind as to how to do this, so anybody have any ideas or code, please let me know. I'd really appreciate it.

Thanks.

Snr Unix System Admin/DBA
Digicel Jamaica (
 
You may consider an awk script reading filetst.txt:
in the BEGIN section load an associative array from search.txt
for each record, print $0 to the newfile if substr($0,41,11) exist in the array.

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Code:
#/bin/bash
#
#
patternlist="$(cat search.txt)"
for pattern in $patternlist ; 
do
	# (use 41 dots, instead of 6)
	sed -n '/^......'$pattern'/p' filetst.txt >> outfile
done

I guess there is a shorter expression than 41 dots, something like '.{41}', but it must be somehow different :)


seeking a job as java-programmer in Berlin:
 
Try egrep...
[tt]
egrep -f search.txt filetst.txt
[/tt]
...where search.txt contains...
[tt]
^.{40}string1
^.{40}string2[/tt]
etc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top