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.
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
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
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
Firstly, you're not going to get any output in temp2.txt because you are passing "-n" which suppresses output.
Secondly, I'm still trying to figure out why you can't run a command when the command is stored inside a variable. sed seems to be executed, but it can't make sense of what I pass in. If I run sed from the command line with the same arguments, it works fine.
Thats exactly what I get. When I run the command on the command line it works but not with the script. the /p at the end prints the range withinn. If you run the command it does produce an output.