Regex and variable assignment


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Regex and variable assignment
# 1  
Old 03-29-2012
Regex and variable assignment

Hi there,
I really didn't want to have to waste people's time and ask this, but after 2 hours and running out of my own time I've decided to ask..

Basically I have a file in the format:

word: other words; more words here; last word
word: more words here; last word
(etc) x 100

Where the occurrence of other words/more words can be zero or more.

My aim is to count the amount of characters that come after the ':'.
My logic was simple, use awk to create a regex that equals from : to $ (i.e. from : to end of file). And then print its length.

I couldn't work out how to do that, but figured I'd be able to print a variable's length quite easily.
So I've been trying to assign a variable to the regex, and then print its length.
But I'm having trouble assigning a variable to a regex.
I feel as if there is some basic key concept I'm missing...

How do I do this?

I currently have:

awk '$var ~ /:.*$/ {print (length)var}' a.txt

Also if I wanted to print both the regex and its length, would it be:

{print (length)var, var}

I'm sure there's some basic concept or syntax I'm missing here, can someone please explain to me if you see easily why I'm going wrong?

Thanks heaps,
# 2  
Old 03-29-2012
Code:
# awk -F":" '{print length($2)}' a.txt
40
3

Taking the colon as field separator. This works as long as there is only 1 colon per line. Blanks are included in the count in this example. Could delete all blanks before doing length().

Variables in awk are not written with a leading $. This is for specifying fields.
This User Gave Thanks to zaxxon For This Post:
# 3  
Old 03-29-2012
Thank you Zaxxon, can go from this :-)

Can you explain how I would have done my initial desire though, i.e. setting a variable to a regex?
Even if I remove the $ sign
awk 'var ~ /:.*$/ {print var}' a.txt didn't work

Thanks again,

Last edited by zaxxon; 03-29-2012 at 08:19 AM.. Reason: missing slash
# 4  
Old 03-29-2012
The note about the $ was just generally.
In your code, you test the variable var to match the reg exp. Since var is not defined, it can never match. So the command inside the curly brackets (print) will never be executed.
In my example you could for further use store the length into a variable like this:
Code:
# awk -F":" '{var=length($2); print var}' a.txt

This User Gave Thanks to zaxxon For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Variable Assignment

Hi I am facing a problem. export local_folder=/opt/app/ cd /opt/app/abc/ abcversion="abc*" (abcga5 is inside /opt/app/abc/) echo $abcversion (it echoes the correct version as abcga5 ) Now when I reuse the value of abcversion for a below path: export... (6 Replies)
Discussion started by: ankur328
6 Replies

2. Shell Programming and Scripting

Same Variable Assignment

Hi I have a strange problem: In my shell script I am performing a copy task: . prop.txt cp -r $dir/ $dir/archive $dir is fetched from a property file (prop.txt) which stores its value dir=/opt/data Now the problem is another dir1 comes into picture. I only want to add... (1 Reply)
Discussion started by: ankur328
1 Replies

3. Shell Programming and Scripting

Dynamic variable assignment

My Code : -------------------------------------------- #!/bin/bash for i in `echo server1 server2` do eval ${i}_name = "apache" echo ${i}_name done -------------------------------------------- Current output : >./test.sh ./test.sh: line 5: server1_name: command not found... (3 Replies)
Discussion started by: sameermohite
3 Replies

4. Shell Programming and Scripting

'eval' used in variable assignment

pattern1=book { x=1 eval echo \$pattern$x } book (this is the output) But when I assign a variable to the output of the eval it doesn't work unless I prefix 2 times backslash before $ as shown below. { a=`eval echo \\$pattern$x` echo $a } book Why here twice "\" has to be... (3 Replies)
Discussion started by: ravisingh
3 Replies

5. Shell Programming and Scripting

Help with variable assignment

Hi, In AIX I have a variable with , (coma) separated values assigned to it like shown below var1=apple,boy,chris i want to convert this to var1='apple','boy','chris' the number of values assigned to var1 might change and it could be from 1 to n any suggestions please? (3 Replies)
Discussion started by: rahul9909
3 Replies

6. Shell Programming and Scripting

Dynamic variable assignment

Hi all, I’m very new to UNIX programming. I have a question on dynamic variable 1. I’m having delimited file (only one row). First of all, I want to count number of columns based on delimiter. Then I want to create number of variables equal to number of fields. Say number of... (5 Replies)
Discussion started by: mkarthykeyan
5 Replies

7. Shell Programming and Scripting

Indirect variable assignment

Hi I have variable A_B=alpha also var1="A" var2="B" I want to retrieve the value alpha using var1 and var2 , somthing like echo ${${var1}_${var2}} that works. Obviously this is receiving syntax error (6 Replies)
Discussion started by: sumir
6 Replies

8. Shell Programming and Scripting

eval and variable assignment

Hi, i have an issue with eval and variable assignment. 1) i have a date value in a variable and that date is part of a filename, var1=20100331 file1=${var1}-D1-0092.xml.zip file2=${var2}-D2-0092.xml.zip file3=${var3}-D3-0092.xml.zip i am passing the above variables to a script via... (11 Replies)
Discussion started by: mohanpadamata
11 Replies

9. UNIX for Dummies Questions & Answers

Variable assignment problem

Hi, I am having a problem with assigning a value to a variable. The empname is looked for in the employees file and I am trying to assign it to the jobnum variable in the following statement jobnum= cat/etc/employees | grep $empname | cut -d : -f 5 It is getting the right answer but it is... (3 Replies)
Discussion started by: rodney08
3 Replies

10. UNIX for Dummies Questions & Answers

@ in a variable assignment

Hello Everybody, Does anyone know what the @ symbol means in a csh script, if used with a variable assignment as below @ line = 1 why not just use.... set line=1 Many thanks rkap (1 Reply)
Discussion started by: rkap
1 Replies
Login or Register to Ask a Question