AWK: pattern not properly stored in variable?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK: pattern not properly stored in variable?
# 1  
Old 08-30-2009
AWK: pattern not properly stored in variable?

Hey there,

I have a table of contents file of the form

Code:
1 Title1
1.1 Subtitle1
1.1.1 Subsubtitle1
1.1.2 Subsubtitle2
...

and want to count the number of dots in the first field to find out the level of the section.
I use the gsub function for the job, which works if I pass the pattern directly.
The awk program

Code:
/.*/ {
count = gsub(/\./, "&", $1)
print count
}

correctly prints out the number of dots, output of the first four lines (see sample file above) is

Code:
0
1
1
2

If I try to store the dot-pattern in a variable things mess up. The program

Code:
/.*/ {
separator = /\./
count = gsub(separator, "&", $1)
print count
}

outputs

Code:
0
2
3
2

Can anyone explain to me what is happening and supply a solution on how to store the substitution pattern in a variable?


Thanks in advance,

Simon

---------- Post updated at 11:31 AM ---------- Previous update was at 11:13 AM ----------

Solved, sorry for giving up that early and asking you. As variables only store string values, the pattern has to be saved as

Code:
separator = "\."

and can then be used, e.g. as

Code:
count = gsub(separator, "&", $1)

Damn, that was easy :-)
# 2  
Old 08-30-2009
That's interesting because "\." doesn't work on my awk!

I have to use "\\."
# 3  
Old 08-30-2009
Hi Scott,

I initially also typed "\\." but it doesn't seem necessary with my interpreter (mawk on Debian Lenny).


Regards,

Simon
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk -Search pattern through Variable

Hello, We have wrote shell script for multiple file name search pattern. file format: <numner>_<20180809>.txt starting with single number and ending with 8 digits number Command: awk -v string="12_1234" -v serch="^+_+$" "BEGIN{ if (string ~/serch$/) print string }" If sting matches... (4 Replies)
Discussion started by: koti_rama
4 Replies

2. Shell Programming and Scripting

awk to lookup stored variable in file and print matching line

The bash bash below extracts the oldest folder from a directory and stores it in filename That result will match a line in bold in input. In the matching line there is an_xxx digit in italics that (once the leading zero is removed) will match a line in link. That is the lint to print in output.... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. Shell Programming and Scripting

awk - Multi-line data to be stored in variable

Greetings Experts, As part of automating the sql generation, I have the source table name, target table name, join condition stored in a file join_conditions.txt which is a delimited file (I can edit the file if for any reason). The reason I needed to store is I have built SELECT list without... (5 Replies)
Discussion started by: chill3chee
5 Replies

4. Shell Programming and Scripting

How to search a filename stored in a variable using a pattern?

hi, i have a variable which contains some file names delimited by a single space. FNAME="s1.txt s2.lst s3.cvs s4.lst" i have another variable that contains a pattern FILE_PATTERN="*.lst" i want to take the filenames from FNAME variable and assign each file name in to an array say for... (8 Replies)
Discussion started by: Little
8 Replies

5. Shell Programming and Scripting

Variable%pattern operation within awk

Hello; I have: ll | grep -v ^d | awk '{print $9}' rcx_access_report_fid.txt rcx_access_report_hsi.txt rcx_access_report_mmm.txt rcx_access_report_qsc.txt I want to get: rcx_access_report_fid.txt rcx_access_report_hsi rcx_access_report_mmm rcx_access_report_qsc But when I try: (9 Replies)
Discussion started by: delphys
9 Replies

6. Programming

take input from a variable as pattern to awk

Hi everyone, Can anyone tell me how to take contents of a variable as a pattern for awk command. Am doing as below, but doesnt get any output: $c = "Tue Dec"; $log = ` awk '/ \$c /' in.txt`; print $log; (7 Replies)
Discussion started by: anandrec
7 Replies

7. Shell Programming and Scripting

PERL : pattern matching a string stored in a variable

I have two variables, my $filename = "abc_yyyy_mm_dd.txt"; my $filename1 = " abc_2011_11_07.txt"; I need to perform some operations after checking if $filename has $filename1 in it i have used the below code, if($filename =~ /^$filename1/) { ---- -- } (2 Replies)
Discussion started by: irudayaraj
2 Replies

8. Shell Programming and Scripting

Variable not found error for a variable which is returned from stored procedure

can anyone please help me with this: i have written a shell script and a stored procedure which has one OUT parameter. now i want to use that out parameter as an input to the unix script but i am getting an error as variable not found. below are the unix scripts and stored procedure... ... (4 Replies)
Discussion started by: swap21783
4 Replies

9. Shell Programming and Scripting

Insert external variable in a AWK pattern

Dear all, ¿How can i insert a variable in a AWK pattern? I have almost succeeded in solving a puzzle with AWK but now i want to make a script. Let me explain. cat file.txt | awk 'BEGIN {RS="\\n\\n"} /tux/ { print "\n"$0 }' I know that this command makes right what i want to do, but mi... (8 Replies)
Discussion started by: antuan
8 Replies

10. Shell Programming and Scripting

How to define a variable with variable definition is stored in a variable?

Hi all, I have a variable say var1 (output from somewhere, which I can't change)which store something like this: echo $var1 name=fred age=25 address="123 abc" password=pass1234 how can I make the variable $name, $age, $address and $password contain the info? I mean do this in a... (1 Reply)
Discussion started by: freddy1228
1 Replies
Login or Register to Ask a Question