I'm looking for a solution to pass variables to a sed-command. I'm reading a lot of threats and also the q&a "How can I use a variable in sed?". None of these commands works. I'm using AIX 5.2.
I want to do the following:
NUMBER=` echo 38341` | sed -n '/$NUMBER/p'
an obtained this:
+ + sed -n /$NUMBER/p
+ echo 38341
NUMBER=38341
If I try this command
NUMBER=` echo 38341` | sed -n "/$NUMBER/p"
I obtained this:
+ sed -n //p
+ sed: 0602-410 The first regular expression cannot be null.
+ echo 38341
NUMBER=38341
The problem I have is, I need the NUMBER as a line number to access these line in a second file.
WE can not use enviroment variables with sed externally. With pattern match we can use \1, \2. Using awk we can make your requirement easily.
To put line number in a file with sed then,
sed = filename | sed 'N;s/\n/ /g'
Plz. Can you explain on " I need the NUMBER as a line number to access these line in a second file." this with example, so that we can get the solution for that easily.
No need for the backquoted echo, you also need to change the sed syntax slightly, and also specify the file you're searching. Though the syntax you're using would work with the pipe, you're just piping "nothing" to sed (same as doing a "cat /dev/null | sed blah..." - pointless). Either put the commands on seperate lines, or change that pipe to a semicolon...
NUMBER=38341
sed -n "$NUMBER p" my_file_here
# or
NUMBER=38341; sed -n "$NUMBER p" my_file_here