awk slash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk slash
# 1  
Old 04-04-2008
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  
Old 04-04-2008
Code:
awk -F/ -v var=$mylog '$0 ~ var{print $1}' master.cnf

Regards
# 3  
Old 04-06-2008
Bug

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

Regards,
Naresh
# 4  
Old 04-06-2008
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  
Old 04-06-2008
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  
Old 04-09-2008
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  
Old 04-09-2008
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to remove slash

I got stuck in problem i need to remove slash but it is not working. current output /usr/sbin/httpd expected output usr sbin httpd (4 Replies)
Discussion started by: learnbash
4 Replies

2. Shell Programming and Scripting

Slash delimiter

I have a log file which on field $11 there is sth like: /A/B/C and I want to extract the last part which is C. for space delimiter I used: awk '{print $2 " " $5 ";" $7 ";" $9 ";" $11}' and had no problem. but dont know how to extract the 3rd part of the slash delimiter. That would be... (6 Replies)
Discussion started by: frhling
6 Replies

3. Shell Programming and Scripting

awk sub function "quote or slash"

I struggle to understand why its used slash / instead of doublequotes " in sub function of awk. This is taken from the manual of awksub(/USA/, "United States", "the USA and Canada") But this work alsosub("USA", "United States", "the USA and Canada") Is there any reason of why to select one... (2 Replies)
Discussion started by: Jotne
2 Replies

4. UNIX for Dummies Questions & Answers

Awk pattern with letters and forward slash

Hi, I have a tab delimited file "test.txt" like this: id1 342 C/T id2 7453 T/A/-/G/C id3 531 T/C id4 756 A/T/G id5 23 A/G id6 717 T/A/C id7 718 C/T/A And so on, with the possible choices for letters being A,C,T,G. I would like to exclude from my file all the lines that do not have... (3 Replies)
Discussion started by: francy.casa
3 Replies

5. Shell Programming and Scripting

AWK or SED to replace forward slash

hi hope somebody can help, there seems to be bit on the net about this, but still cant make it work the way i need. i have a file live this mm dd ff /dev/name1 mm dd ff /dev/name2 mm dd ff /dev/name3 mm dd ff /dev/name4 i need to update /dev/name1 etc to /newdev/new/name1 etc so... (5 Replies)
Discussion started by: dshakey
5 Replies

6. Shell Programming and Scripting

Awk: Searching for length of words between slash character

Dear UNIX Community, I have a set of file paths like the one below: \\folder name \ folder1 \ folder2 \ folder3 \ folder4 \\folder name \ very long folder name \ even longer name I would like to find the length of the characters (including space) between the \'s. However, I want... (6 Replies)
Discussion started by: vnayak
6 Replies

7. Shell Programming and Scripting

Using sed to append backward slash before forward slash

Hi all, I need to know way of inserting backward slash before forward slash. My problem is that i need to supply directory path as an argument while invoking cshell script. This argument is further used in script (i.e. sed is used to insert this path in some file). So i need to place \ in front... (2 Replies)
Discussion started by: sarbjit
2 Replies

8. UNIX for Advanced & Expert Users

Substitute single backward-slash with the double backward-slash

Hi, I have a path like this c:\test\sample\programs, i need to change thiis to c:\\test\\sample\\programs. How to perform this? I tried tr command but it didn't help me. Thanks Vijayan (3 Replies)
Discussion started by: mvictorvijayan
3 Replies

9. Shell Programming and Scripting

About \ (Back slash)

Hi All, print "path/executable_file parameters" \ > path/file1 print "path/executable_file parameters" \ > path/file2 print "path/executable_file parameters" \ > path/file3 chmod 775 path/file1 \ path/file2 \ ... (7 Replies)
Discussion started by: Arunprasad
7 Replies

10. Shell Programming and Scripting

Replace the last slash alone

Hi All, Can you please help me with the below issue. I want only the last slash to be replaced with space. 06/05/2008/EDED_FD_BDCD_ABCD_123 06/05/2008 EDED_FD_BDCD_ABCD_123 (3 Replies)
Discussion started by: christineida
3 Replies
Login or Register to Ask a Question