![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
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 |
|
||||
|
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
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
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 |
|
||||
|
Code:
awk -F '|' '{print $2}'
|
|
||||
|
Quote:
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 > 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:
|
|
||||
|
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
|
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|