The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Help with SED and forward slash gseyforth Shell Programming and Scripting 3 05-26-2008 07:29 AM
ftp username with back slash sam99 Shell Programming and Scripting 1 04-29-2008 09:19 AM
grep for forward slash wxornot Shell Programming and Scripting 3 02-29-2008 10:01 PM
touching a file which contains slash char axes High Level Programming 1 09-25-2006 05:54 AM
echo omits slash - how to avoid tc.omkumar Shell Programming and Scripting 3 03-21-2006 04:53 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-04-2008
McLan McLan is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 21
awk slash

I have configuration file(master.cnf), that contents are:
VER1LOG /src/ver1/log
VER2LOG /src/ver2/log
APPLOG /src/sys/apps/log
APPCONF /src/sys/apps/conf
APPBIN /src/sys/apps/bin

my shell script is as below
mylog=sys/apps

awk "/$mylog/" master.cnf
awk: syntax error Context is:
>>> /sys/apps <<<

in the above example I want to print First field that maches with $mylog
here I used variable mylog, the value of mylog contains "/" charcter, this is same as char used in awk
to specify PATTERN.When I try as above it thors an error
so my requirement is i need to use variable, the value of varibale can contain the "/" char.
and how can I get the desired result?
Regards
  #2 (permalink)  
Old 04-04-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,293
Code:
awk -F/ -v var=$mylog '$0 ~ var{print $1}' master.cnf
Regards
  #3 (permalink)  
Old 04-06-2008
McLan McLan is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 21
Smile

Thanks it works for me,
What does -F/ -v var=$mylog '$0 ~ var{print $1} do?

Regards,
Naresh
  #4 (permalink)  
Old 04-06-2008
McLan McLan is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 21
awk issue with slash, multiple FS

Now the requirement is different with different input file(master.cnf)
>cat master.cnf
/usr| location for usr|5
/src/ver1| version 1 |10
/src/ver2/log| ver 2 log |25
/src/sys/apps/log| Application log for sys|36
/src/sys/apps/conf| configuration location for app|45
/src/sys/apps/bin| binary location app|55
Code:
my shell script is as below:
mylog=/usr
awk -F/ -v var=$mylog '$0 ~ var{print $2}' master.cnf
result is :
usr| location for usr|5
I need only second filed separated by | should be displayed. i.e “location for usr” should be displayed

Code:
Another script is :
mylog=/src/ver1
awk -F/ -v var=$mylog '$0 ~ var{print $2}' master.cnf
result is: 
src
but I need “  version 1 “ to be displayed
to summary the requirements:
1. should be able to use variable, the value of variable can contains multiple “/” chars
2. need to print the second filed separated by “|”
Thanks
  #5 (permalink)  
Old 04-06-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Code:
awk -F '|' '{print $2}'
-F indicates the separator. The more elaborate trick was a clever workaround to avoid the need to backslash-escape the separator chararcter (which would be the plain-jane solution when the separator character is also a special character).
  #6 (permalink)  
Old 04-09-2008
McLan McLan is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 21
Quote:
thanks for the response
now i want to try to search with different field.

the data file is as below:
Code:
> cat master.cnf
/usr| location for usr|5
/src/ver1| version 1 |10
/src/ver2/log| ver 2 log |25
/src/sys/apps/log| Application log for sys|36
/src/sys/apps/conf| configuration location for app|45
/src/sys/apps/bin| binary location app|55
my script is as below:
>
Code:
desc='Application log for sys'
>echo $desc
Application log for sys
> awk -F '|' -v var=$desc '$0 ~ var{print $3}' master.cnf
Code:
output is 
input file "for"input file "sys"input file "$0 ~ var{print $3}"
/usr| location for usr|5
/src/ver1| version 1 |10
/src/ver2/log| ver 2 log |25
/src/sys/apps/log| Application log for sys|36
/src/sys/apps/conf| configuration location for app|45
/src/sys/apps/bin| binary location app|55
Quote:
I need to print field 3 separated by "|" and I want to use varibale to seach , the value of variable can contin the space char.
Please suggest me
  #7 (permalink)  
Old 04-09-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
The only change you need is to quote the variable (to cope with spaces).

Code:
awk -F'|' -v var="$desc" '$0 ~ var{print $3}' master.cnf
If you get error messages you do not understand, post them here; they do mean something, and help us see what's wrong.
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:23 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0