Taking sed result in a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Taking sed result in a variable
# 1  
Old 11-25-2009
Taking sed result in a variable

Hi All,

How can i take the following code having three seds into a variable :

Code:
echo "$DateFileFormat" | sed 's/\./\\\\./g' | sed 's/\$/[0-9]+/g' | sed 's/\#/'$job_date'/g'

I want to get the result stored in a script variable

i tried
Code:
var2=`echo "$DateFileFormat" | sed 's/\./\\\\./g' | sed 's/\$/[0-9]+/g' | sed 's/\#/'$job_date'/g'`


but didnt work
# 2  
Old 11-25-2009
Define 'did not work'
Code:
export myvar=""

echo "$DateFileFormat" | sed 's/\./\\\\./g' | sed 's/\$/[0-9]+/g' | sed 's/\#/'$job_date'/g' | read myvar

echo "$myvar"

# 3  
Old 11-25-2009
Hi ,

This is not working

---------- Post updated at 10:43 AM ---------- Previous update was at 10:32 AM ----------

Hi All,

If i do

Code:
echo "$DateFileFormat" | sed 's/\./\\\\./g' | sed 's/\$/[0-9]+/g' | sed 's/\#/'$job_date'/g'

i get the right format in the out put that is

EGZAFU01\\.DOXR\\.UK\\.01\\.25112009\\.S001\\.V1\\.D[0-9]+\\.data\\.txt


but if i use the code which gets the value in a variable that is :

Code:
var2=`echo "$DateFileFormat" | sed 's/\./\\\\./g' | sed 's/\$/[0-9]+/g' | sed 's/\#/'$job_date'/g'`

the var2 variable gets following value :

EGZAFU01\.DOXR\.UK\.01\.25112009\.S001\.V1\.D$\.data\.txt[0-9]+


As you can see the $ has not been replaced with [0-9]+ and the "\\" have changed to '\
# 4  
Old 11-25-2009
first, you can do that in a single sed command, usig the -e flag :
Code:
VAR=`echo "$DateFileFormat" | sed -e 's/\./\\\\./g' -e 's/\$/[0-9]+/g' -e 's/\#/'$job_date'/g'`

Now, your problem comes probably from posix parameter set to on in your shell, type a man sed to see the regex possibilities.
try "--regexp-extended" or "-r", maybe it works.
# 5  
Old 11-25-2009
Code:
var2=$(echo "$DateFileFormat" | sed "s/\./\\\\\\\\./g;s/\\\$/[0-9]+/g;s/\#/$job_date/g")

Code:
$> echo $var2
EGZAFU01\\.DOXR\\.UK\\.01\\.25112009\\.S001\\.V1\\.D[0-9]+\\.data\\.txt




---------- Post updated at 23:25 ---------- Previous update was at 23:16 ----------
ksh/bash:
Code:
var=${DateFileFormat//\./\\\\.}
var=${var//$/[0-9]+}
var=${var//#/$job_date}

Code:
$> echo $var
EGZAFU01\\.DOXR\\.UK\\.01\\.25112009\\.S001\\.V1\\.D[0-9]+\\.data\\.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed not taking variable value

Hi All, Could you please help me, why sed is not able to take variable value when I try to replace using sed. I want to replace 2nd column (time) and keeping intact others. cur="09:30" CODE="VL" new="09:35" sed s/'\(.*def.monitor."${CODE}".qStart.*\)"${cur}"\(.*read\)/\1"${new}"\2'/g... (3 Replies)
Discussion started by: sdosanjh
3 Replies

2. Shell Programming and Scripting

Help with taking variable of for loop in textfile

Hi, I am new to unix/linux scripting. I have a text file, listlib.txt where the content: lib1_23 lib34_a ab_li_lab I need to generate a file (.log) of each cell. I am planning to create a (.csh) script that will have for loop with variable taken from listlib.txt. As for now, i have no... (4 Replies)
Discussion started by: mmaz
4 Replies

3. UNIX for Dummies Questions & Answers

I save the result in a variable

I have friends that this command worked perfectly, but I would like to save the result in a variable, which I have not achieved var=prueba.txt echo $var | cut -d "." -f 1 prueba I need to do this but does not work me salida=echo $var | cut -d "." -f 1 echo "result:$salida" ... (8 Replies)
Discussion started by: tricampeon81
8 Replies

4. Shell Programming and Scripting

Result of 'cut' into variable

Hello, I would like to have a result of a cut-command in a variable. The file "t.dat" has the following content: Company 001.239879123.OB1X.672W12.STS UNOLD.001.02 My bash-script: Header="" Header=$(cut -c1-160 t.dat | head -1) echo $Header ... (9 Replies)
Discussion started by: API
9 Replies

5. Shell Programming and Scripting

How to combine two variable result?

Hi, i have two variables i.e lck_ckm_customer=ckm_customer and(present in some script A) table=ckm_customer(present in script B) in script B i am executing this part if ; then --- ---- ---- fi Now, while comparing in my log file i am getting this result if but i... (2 Replies)
Discussion started by: gnnsprapa
2 Replies

6. UNIX for Dummies Questions & Answers

SED taking too much time

Hi I am trying to remove some characters from my data file. The data file has huge number of records say 90000 records. I am using sed for this purpose. eg : cat FILENAME|sed 's/;//g' (to remove semi colon ';') However as the data file is too huge , it is taking too much time to give... (3 Replies)
Discussion started by: dashing201
3 Replies

7. Shell Programming and Scripting

result in variable

Hi all, I'll try to get a result from a search with "awk" into a variable. It works for those examples: findfirstline=`awk 'BEGIN{ mycount = 1 } { if(mycount == 1 && /^Video/){ row=NR; print row; mycount = 0; }}' ${i}` findlastline=`awk '/^Video/ {row=NR} END{print row}' ${i}` But it... (6 Replies)
Discussion started by: tempestas
6 Replies

8. Shell Programming and Scripting

Assign result to variable

Hi friends, firstly, i can run following expression and i took 100 value. sqlplus -s username/password@TTTEST @umt.sql umt.sql exists "select t.deger from parametre t where t.id=30". result of this query =100 i need to assign this value(100) to variable(for example x... (2 Replies)
Discussion started by: temhem
2 Replies

9. Shell Programming and Scripting

assign subst|grep|sed command result to a variable

Hi, I'm quite new to scripting and I want to modify following line of an existing script: MYVAR=`subst |grep 'L:\\\:' | sed -e 's/.*\\\//'`; What I have to do is to use the content of a variable instead of the constant expression 'L:\\\:' as the grep string to be matched. Assuming I already... (5 Replies)
Discussion started by: snowbiker99
5 Replies

10. Shell Programming and Scripting

taking every variable and executing the command

Hi, I am trying to export some 50 tables and i want to write a loop and execute the script for every table. I did for one table and its running. Can any one help me for setting a loop and running the script for all the tables thanks (6 Replies)
Discussion started by: srichunduru
6 Replies
Login or Register to Ask a Question