assigning variables


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers assigning variables
# 1  
Old 06-16-2005
assigning variables

Before I even attempt this, is it possible to grep for a pattern, maybe a partial sentence like "go to page 3", assign that to a variable and then use awk or something to pull out the 3 and assign it to a variable? So first I would have
Gotopg = "go to page 3"
then
page = 3
# 2  
Old 06-16-2005
Quote:
Originally Posted by k@ssidy
Before I even attempt this, is it possible to grep for a pattern, maybe a partial sentence like "go to page 3", assign that to a variable and then use awk or something to pull out the 3 and assign it to a variable? So first I would have
Gotopg = "go to page 3"
then
page = 3

Certainly, you can grep for strings having more than one word.

In your case, if you trying to find page numbers, you can sed for numbers and store it

Code:
grep -h -o 'go to page 3' *

will print only those instances of the above string.

You can collect the result, sed it for numbers and then get the numbers printed into the variable.


Vino
# 3  
Old 06-16-2005
How about this

Code:
grep -h -o 'go to page 3' kas.txt | sed -n -e 's/.*\([0-9]\).*/\1/p'


kas.txt contains

[~/temp]$ cat kas.txt
go to page 3
go to page 4
go to page 5
go to page 6


The above prints out 3

Vino.
# 4  
Old 06-16-2005
On second thoughts Smilie ,

why introduce grep and sed when you can do just with sed. Smilie

This is better

Code:
sed -n  -e 's/go to page.*\([0-9]*[^0-9]\)/\1/p' kas.txt

It will pick out numbers having more than 1 digit.

It prints out

sh-2.05b$ sed -n -e 's/go to page.*\([0-9]*[^0-9]\)/\1/p' kas.txt
3
4
5
16



Vino
# 5  
Old 06-16-2005
wouldn't this be easier and more efficient?

sed -n '/go to page [0-9]/s/.*go to page \([0-9][0-9]*\).*/\1/p' kas.txt

P.S. updated!

Last edited by vgersh99; 06-16-2005 at 11:08 AM..
# 6  
Old 06-16-2005
that is very interesting and helpful, thank you, I'm new to this and have been mainly working with grep and awk, the introduction to sed is welcomed, I'm trying to learn as much as I can. Thanks!
# 7  
Old 06-16-2005
Quote:
Originally Posted by vgersh99
wouldn't this be easier and more efficient?

sed '/go to page [0-9]/s/.*go to page \([0-9][0-9]*\).*/\1/' kas.txt

Interesting indeed! Our replies came about the same time.

Vino
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Assigning Variables

Hi, Can the below be clarified please. i just want to know what is the difference between the two ways of assigning variables as mentioned below. export SRC_TBL=${SRC_TBL-"MMA_COPAY_PLN_FACT_STG"} export SRC_TBL="MMA_COPAY_PLN_FACT_STG" thanks in advance :) Arun (1 Reply)
Discussion started by: Arun Mishra
1 Replies

2. Shell Programming and Scripting

Assigning variables

i have variables RECIPIENTS_DEVL,RECIPIENTS_UACC,RECIPIENTS_PROD i have a case statement to get the phase variable: case ${WMD_UPHASE1} in u) WMD_UPHASE4=UACC;; i) WMD_UPHASE4=DEVL;; p) WMD_UPHASE4=PROD;; d) WMD_UPHASE4=DEVL;; *) WMD_UPHASE4=DEVL;; esac I am unable to... (3 Replies)
Discussion started by: Arun Mishra
3 Replies

3. Shell Programming and Scripting

Help in assigning values to variables from the file

Hi! This might be a simple thing, but I'm struggling to assign values to variables from the file. I've the following values stored in the file.. It consists of only two rows.. 10 20 I want to assign the first row value to variable "n1" and the second row value to variable "n2".. That is ... (3 Replies)
Discussion started by: abk07
3 Replies

4. Shell Programming and Scripting

Help with reading and assigning variables

Hi Gurus, I have a file named log with 2 lines Each line is a file name. eg $ cat log monday tuesday I need to read log and assign each output(filename) to a different variable. The following doesn't work:- while read A B do echo " a is ${A} " echo " b is ${B} " done <... (6 Replies)
Discussion started by: wisdom
6 Replies

5. Shell Programming and Scripting

Assigning expression value to tcsh variables

Hi All, I have a tcsh script as: #!/usr/bin/csh -x set packsName=$(awk -F'' '/^execute.*=true/{print $2}' ExecutePacks.config) for var in $packsName do echo "printed $var" done I want to assign the value which is returned by awk function to the variable called packsName. How do I... (2 Replies)
Discussion started by: AB10
2 Replies

6. UNIX for Dummies Questions & Answers

Assigning variables using awk

Hi, I am having a line which is separated by - I need to extract each field and put into some variable. a=`echo "this-is-the-case" | awk -F- '{print $1}' ` b=`echo "this-is-the-case" | awk -F- '{print $2}' ` c=`echo "this-is-the-case" | awk -F- '{print $3}' ` d=`echo "this-is-the-case" | awk... (2 Replies)
Discussion started by: posix
2 Replies

7. Shell Programming and Scripting

variables not assigning in a function

Hi GUYS, I have function. I am assigning a line count to count variable. But it is throwing an error at this line. function_recur (){ #file being created in this function lenth = `wc -l function_outpu.dat`; echo $lenth; } this is the error i got rec.ksh: lenth: not found. ... (3 Replies)
Discussion started by: mac4rfree
3 Replies

8. UNIX for Advanced & Expert Users

assigning variables to their defaults

Hi, Is there any way to assign defaults values to the shell variables without reassigning them ( restarting the session) for example after login the value of ORACLE_HOME=/a/b/c i have changed this value from the console export ORACLE_HOME=/c/d now what if i want the value exported to... (1 Reply)
Discussion started by: clx
1 Replies

9. Shell Programming and Scripting

assigning variables in sed command

I need to assign a variable within a variable in a sed command. I tried doing the following in c shell. set left = 1 set right = 2 set segment = qwerty sed -n -e "/$segment{$left}/,/$segment{$right}/p" file.txt what is wrong with this syntax? (3 Replies)
Discussion started by: wxornot
3 Replies
Login or Register to Ask a Question