The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to execute a sh command from a csh script bonovox Shell Programming and Scripting 2 06-25-2008 05:24 AM
How to build a command into a string rather than execute the command littlejon Shell Programming and Scripting 1 05-29-2008 09:05 PM
shell script to execute user command redtiger Shell Programming and Scripting 1 04-30-2008 07:53 PM
script execute or no execute Kespinoza97 Shell Programming and Scripting 4 06-23-2007 10:27 AM
Plz Help : How to use write command to execute command on some other terminal Aashish UNIX for Dummies Questions & Answers 2 03-03-2006 12:25 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-17-2008
gugs gugs is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 44
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 (permalink)  
Old 07-17-2008
cooldude cooldude is offline
Registered User
  
 

Join Date: Mar 2006
Posts: 44
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 (permalink)  
Old 07-18-2008
gugs gugs is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 44
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 (permalink)  
Old 07-18-2008
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
Moderator
  
 

Join Date: Sep 2007
Location: Germany
Posts: 2,285
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 (permalink)  
Old 07-18-2008
gugs gugs is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 44
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><erroretail>An' error occured
whilst trying to process a routing request, see attached exception list for 'det
ails</erroretail><_05_1:MessageIdentifier>ERR:70225547_1215781581748</_05_1:MessageIdentifier>: No such file or directory
  #6 (permalink)  
Old 07-18-2008
zaxxon's Avatar
zaxxon zaxxon is offline Forum Staff  
Moderator
  
 

Join Date: Sep 2007
Location: Germany
Posts: 2,285
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
  #7 (permalink)  
Old 07-18-2008
gugs gugs is offline
Registered User
  
 

Join Date: Jul 2008
Posts: 44
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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:39 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0