Please help: Build a sed command and execute it in a script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Please help: Build a sed command and execute it in a script
# 1  
Old 07-17-2008
Please help: Build a sed command and execute it in a script

I am using an array to store some data (keys e.g 47975081_1215781266128), it can be assumed that it is key to other data.

I want extract data from a file based on a couple of keys (range) and store the resulting data in a variable using the following command:
sed -n '/47975081_1215781266128/,/42628155_1215781428374/p' test1.txt

In my script I build a sed command and write it to a temp file using the code below:
Range1="${keys[$i]}"
Range2="${keys[$i+1]}"
a="sed -n '/"
b="/,/"
c="/p'"
d=" test1.txt"
echo "$a$Range1$b$Range2$c$d" > temp.txt

This works ok, it writes the following string to the temp file:
sed -n '/08465696_1215781522540/,/70225547_1215781581748/p' test1.txt

Then I grep this file and assign the contents to a variable
sedcmd=$(grep sed temp.txt)

However, the string that is extracted has other characters attached, the grep extracts:
sed -n ''\''/08465696_1215781522540/,/70225547_1215781581748/p'\''' test1.txt

It has added: ''\' before the '/08465.... and \''' after the ....48/p'
Can someone please tell me why? and how I can extract exactly what is in the temp.txt file and run it.

Basically, I want to build a command, run it and assign the resulting contents to an array.
# 2  
Old 07-17-2008
I'm not seeing that.
Here are the contents of temp.txt:
sed -n '/08465696_1215781522540/,/70225547_1215781581748/p' test1.txt

Here is my script, test:
Code:
#!/bin/ksh

sedcmd=$(grep sed temp.txt)

echo $sedcmd

And I get the contents of the file in $sedcmd:
Code:
$./test
sed -n '/08465696_1215781522540/,/70225547_1215781581748/p' test1.txt

Are you sure that temp.txt has the right contents?
# 3  
Old 07-18-2008
More help please

Thanksk, it was writing the quotes to the file. I have correct the problem. However, I am now faced with another for which there may be a simple explanation. When I run the command:

cat test1.txt | $($sedcmd) > temp2.txt

where sedcmd = sed -n '/08465696_1215781522540/,/70225547_1215781581748/p'

I don't get an output in the temp2.txt. When I run the script in debug it states 'No such file or directory'

The same message is displayed in debug even when I run:
sed -n '/08465696_1215781522540/,/70225547_1215781581748/p' test1.txt > temp2.txt


Any ideas why?
# 4  
Old 07-18-2008
It will not be executed without backticks or simple brackets. Chechk the underlined backticks below.
Code:
sedcmd = `sed -n '/08465696_1215781522540/,/70225547_1215781581748/p'`

# 5  
Old 07-18-2008
The command appears to execute OK. See below I have attached the output from debug:

+ Range1=70225547_1215781581748
+ Range2=
+ a='sed -n /'
+ b=/,/
+ c=/p
+ echo 'sed -n /70225547_1215781581748/,//p'
++ grep sed temp.txt
+ sedcmd='sed -n /70225547_1215781581748/,//p'
+ cat test1.txt
++ sed -n /70225547_1215781581748/,//p
+ '<_05_1:MessageIdentifier>ERR:70225547_1215781581748</_05_1:MessageIdentifier>
' '</_05:ReceiveRequest><?xml' 'version="1.0"' 'encoding="UTF-8"?><error:Excepti
on' 'xmlns:error="hhtp/messages/exception/2006-06"' 'xmlns
:xsi="http://www.w3.org/2001/XMLSchema-instance"' 'SchemaVersion="1.0"><error:Na
me>ABC</error:Name><error:Code>001</error:Code><errorSmilieetail>An' error occured
whilst trying to process a routing request, see attached exception list for 'det
ails</errorSmilieetail><_05_1:MessageIdentifier>ERR:70225547_1215781581748</_05_1:MessageIdentifier>: No such file or directory
# 6  
Old 07-18-2008
You have that input file text1.txt. Run the sed command on the shell onto it 1st without redirecting it to an outputfile. Just test this, then start redirecting it, if it works.
And please use the tags [ c o d e ] and [ / c o d e ] to have your code more eyefriendly for us, ty Smilie
# 7  
Old 07-18-2008
The code below works:

Range1="${keys[$i]}"
Range2="${keys[$i+1]}"
a="sed -n /"
b="/,/"
c="/p"
echo "$a$Range1$b$Range2$c" > temp1.txt
sedcmd=$(grep sed temp1.txt)
errordata=$($sedcmd < test1.txt)
echo "$errordata" > temp2.txt

However, I am having to echo the contents of the variable to a file which I process further.

What I would like to do is send the data from the $sedcmd < test1.txt to temp2.txt by using: cat test1.txt | $($sedcmd) > temp2.txt. However, this gives me 'No such file or directory' errorr Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Execute ssh command with additional terminal command to any remote user not working script

Hello i am having an issue with bash script and this is the code now=$(cat hosts1.txt | awk '{print $2;}') while read n ;do ssh root@$now 'useradd test1; echo -e "test1\ntest1" | passwd test1 && echo "test1 ALL=(ALL:ALL) ALL" >> /etc/sudoers' When i execute only part with cat, it... (8 Replies)
Discussion started by: tomislav91
8 Replies

2. Shell Programming and Scripting

sed working on command line but file unchanged when execute with Shell script

I have a simple task to replace unix line feed end of line characters with carriage returns. When I run the following “change file in place” sed instruction from the command line all the Line feeds are successfully replaced with Carriage returns. sed -i 's/$/\r/' lf_file.txt But that same... (1 Reply)
Discussion started by: hawkman2k
1 Replies

3. Shell Programming and Scripting

Build.xml invocation by Build Script

Hi I have a build.xml file and I can run it on Windows via cmd. Now I want to write a script to invoke the same. Is there a way to do this? (1 Reply)
Discussion started by: ankur328
1 Replies

4. Shell Programming and Scripting

Script runs but does not execute rm -rf command

Hi! First off I'm no bin/bash script writer! :( I can make heads and tales of it from the php experience I have and that's all. Now I managed to piece this script together to go look at directory and remove files that are +60 days. It's finding the files but its not removing them. I... (11 Replies)
Discussion started by: MrBiggz
11 Replies

5. Shell Programming and Scripting

When i am trying to execute export command within a shell script it is saying command not found.

I am running the export command within a view to use that value inside my build script. But while executing it it is saying "export command not found" My code is as follows: -------------------------- #!/bin/sh user="test" DIR="/bldtmp/"$user VIEW="test.view1" echo "TMPDIR before export... (4 Replies)
Discussion started by: dchoudhury
4 Replies

6. Shell Programming and Scripting

Execute sed altered script

Man...no one answered my last two posts...Oh well, so hey guys i have a script that i'm wanting to put a # on a certain line, and then execute the script but not write it to the script. So far I'm doing it the dirty way with: sed -i -e "s/^rm/\#rm/" -e "s/^tar/\#tar/" /path/to/file ... (3 Replies)
Discussion started by: DC Slick
3 Replies

7. Shell Programming and Scripting

exec a build command (adduser) in a script

Hi, With a awk script i create a "adduser line" $ cat /tmp/tmp.ldif | awk -f ldif2adduser.awk adduser --uid 1002 --gid 1000 --gecos "ROUSSIN Guy" --home /homeL/guy --shell /bin/bash --disabled-password guy If i cut and paste this line, all is fine. But in a shell script i get errors : ... (2 Replies)
Discussion started by: guyr
2 Replies

8. Shell Programming and Scripting

How to build a command in a script

Hi All I am trying to build a script that will take data from a tab separated file and use that to split up a quicktime file. So far the code is as follows #!/bin/sh #test parsing of data #fix excel file output returns cat $1 | tr "\r" "\n" > $1.fix printf "\n" >> $1.fix mv $1.fix $1 ... (3 Replies)
Discussion started by: babajuma
3 Replies

9. Shell Programming and Scripting

how to execute a sh command from a csh script

Hi everyone, I have a csh script that works fine but the output of an rsh command is different if I use boune shell instead cshell. Is there the possibility to execute only this command in bourne shell from a script declared cshell? Thanks Christian (2 Replies)
Discussion started by: bonovox
2 Replies

10. Shell Programming and Scripting

How to build a command into a string rather than execute the command

I'm trying to populate a command line into a variable. It appears to be executing, instead. Here's an example: mycmd='' if ...; then $mycmd='sudo ' fi $mycmd=$mycmd 'sed -i prev s/aaa/bbb/' $myfile res=`$mycmd` (I'm also not sure of the best way to execute the command from the... (1 Reply)
Discussion started by: littlejon
1 Replies
Login or Register to Ask a Question