SED with a string
I can extract line one from a text file and place it into the string $LINE using:
Code:
LINE=$(sed -n 1p myfile.txt) I can't figure out how to enter a string holding a variable into this bit of code. I was expecting something like the line below to work but it fails. Code:
LINE=$(sed -n $LINENUMBER p myfile.txt) Many thanks for any help. |
Hi
You're close, but there will be a space between 1 and p. Try {} around the variable name, and remove the space. Code:
LINE=$(sed -n ${LINENUMBER}p myfile.txt) |
Quote:
Cheers |
However, if you want to iterate over all line in a file, it is not the best way to do it. Here's a much cleaner and faster way to iterate over all lines of a file:
Code:
while read line ; do |
Are you sure LINENUMBER has a value? Using ${LINENUMBER} just means that the variable name is $LINENUMBER and not $LINENUMBERp, so the result will be something like 1p and not 1 p.
Put the command "set -x" in the beginning of your script, and you will get debug information and can see what is happening. |
Quote:
Code:
LINE=$(sed -n $LINENUMBERp myfile.txt) #### WRONG So, in bash, this can be solved to put {} around LINENUMBER to indicate what exactly the variable name is. Are you using bash to run the script? Anyway, don't iterate over lines in a file this way. See my other post above. |
Quote:
If I use: Code:
LINE=$(sed -n {$LINENUMBER}p myfile.txt) Code:
LINE=$(sed -n ${$LINENUMBER}p myfile.txt) Code:
LINE=$(sed -n {LINENUMBER}p myfile.txt) |
Quote:
|
Quote:
If you are refering to: Code:
LINE=$(sed -n ${LINENUMBER}p myfile.txt) sh: 1: myfile.txtp: not found |
Quote:
It is correct. Guessing from the error message you posted, renaming your file to myfile.txtp might make it work though. If not, what does this output on your system? Code:
echo ${LINENUMBER} |
Quote:
Code:
echo ${LINENUMBER} |
Quote:
A number? |
Here's an entire script as a Proof Of Concept.
Copy-Paste into your editor. Save as poc.sh. Make executable: chmod +x poc.sh Run it: ./poc.sh Code:
#!/bin/bash Code:
LINENUMBER = 2 |
Quote:
Originally I had a loop from 1-64, as the file always has 64 lines in it. Then was trying to get each one of the lines extracted using sed. So I was passing this number to SED in the "sed -n $line p" which did not work. However I think at that point "sed -n ${line}p" would have worked. However I followed an earlier suggestion from Hko to use "while read line ; do" command. Now this command was sending the actual line to that value and so sed was receiving a line of text and not a number. Makes sense now. Thanks for your help. |
Each call to sed must open the file.
The smart alternative was stated in post#4: The file is opened once for the while-do-done block, and each call to read reads a new line into a variable. Code:
while read line The whole loop is redirected from the file. If you do not want a loop, you can redirect a { code block } like this Code:
{ Code:
{ Code:
exec 3< myfile.txt |
All times are GMT -5. The time now is 02:44 AM. |