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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

search a string in a file

Status
Not open for further replies.

alortimor

Programmer
Jan 14, 2010
2
GB
I have a file, service_stream_mapping.dat, made of two columns, which looks something like this:
PROCESS_A DAILY_UPDATE
PROCESS_B DAILY_UPDATE
PROCESS_C WEEKLY_UPDATE
PROCESS_D MONTHLY_UPDATE
PROCESS_E WEEKLY_UPDATE

I would like to search the file using awk with a string that occurs somewhere in the 1st column and obtain the string in the 2nd column.

For example, if I search PROCESS_E, I would like the result to be WEEKLY_UPDATE.

I have tried the following so far in the korn shell;

export $JOB_STREAM="DAILY_UPDATE"

#1
$SERVICE_NAME=$(awk '/$x/ {print $2}' x='${JOB_STREAM}' service_stream_mapping.dat)
echo $JOB_STREAM

#2
$SERVICE_NAME=`awk '/$x/ {print $2}' x='${JOB_STREAM}' service_stream_mapping.dat`
echo $JOB_STREAM

#3
$SERVICE_NAME=$(awk '/'${JOB_STREAM}'/ {print$2}' service_stream_mapping.dat)
echo $JOB_STREAM

#4
$SERVICE_NAME="$(awk -v VAR='$JOB_STREAM' '/VAR/ {print $2}' service_stream_mapping.dat)"
echo $JOB_STREAM

but none of the above four results in anything!

Any help would be much appreciated!
 
Hi

alortimor said:
export [red]$[/red]JOB_STREAM="DAILY_UPDATE"
[ul]
[li]Wrong, because no dollar sign ( $ ) should be used in assignment[/li]
[li]Wrong, because you wrote that you are trying to search in column 1, but DAILY_UPDATE is in column 2[/li]
[/ul]
Code:
[COLOR=chocolate]export[/color] [navy]JOB_STREAM[/navy][teal]=[/teal][green][i]"PROCESS_E"[/i][/green]
alortimor said:
$SERVICE_NAME=$(awk '[red]/$[/red]x[red]/[/red] {print $2}' x=[red]'[/red]${JOB_STREAM}[red]'[/red] service_stream_mapping.dat)
$SERVICE_NAME=`awk '[red]/$[/red]x[red]/[/red] {print $2}' x=[red]'[/red]${JOB_STREAM}[red]'[/red] service_stream_mapping.dat`
$SERVICE_NAME="$(awk -v VAR=[red]'[/red]$JOB_STREAM[red]'[/red] '[red]/[/red]VAR[red]/[/red] {print $2}' service_stream_mapping.dat)"
[ul]
[li]Wrong, because no variable expansion is performed between single quotes ( ' )[/li]
[li]Wrong because $x means xth field[/li]
[li]Wrong because you can match against a variable only with [tt]~[/tt] operator ( anyway, no need for regular expression, as I understand that you want equality )[/li]
[/ul]
Code:
[navy]SERVICE_NAME[/navy][teal]=[/teal][navy]$([/navy]awk [green][i]'$1==x {print $2}'[/i][/green] [navy]x[/navy][teal]=[/teal][green][i]"${JOB_STREAM}"[/i][/green] service_stream_mapping[teal].[/teal]dat[teal])[/teal]
[navy]SERVICE_NAME[/navy][teal]=[/teal]`awk [green][i]'$1==x {print $2}'[/i][/green] [navy]x[/navy][teal]=[/teal][green][i]"${JOB_STREAM}"[/i][/green] service_stream_mapping[teal].[/teal]dat`
[navy]SERVICE_NAME[/navy][teal]=[/teal][green][i]"$(awk -v VAR="[/i][/green][navy]$JOB_STREAM[/navy][green][i]" '$1==VAR {print $2}' service_stream_mapping.dat)"[/i][/green]
alortimor said:
echo $JOB_STREAM
[ul]
[li]Wrong, because your result is in $SERVICE_NAME[/li]
[/ul]
Code:
echo [navy]$SERVICE_NAME[/navy]


Feherke.
 

thx a stack Feherke,

This works!

export JOB_STREAM="SCC_DAILY_UPDATE"
export SERVICE_NAME=""
SERVICE_NAME=$(awk '$1==x {print $2}' x="${JOB_STREAM}" service_stream_mapping.dat)
echo $SERVICE_NAME


I'm a PL/SQL programmer and dislike doing anything in unix shell (may have to change due to circumstances!), so getting help from you chaps is reall helpful!!!

 
Hi

Well, when I had to check SQL table field vs. XML file tag differences of a dozen tables each with 20~30 fields ( fields and tags were not in the same order, certain extra field were Ok to be missing in XML, SQL field names were underscore_separated_words truncated to Oracle's allowed maximum, XML tags were camelCaseWords ), my general addiction for everything that is script helped me a lot. Of course, I solved it with PL/SQL and AWK. ;-)

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top