Unanswered: sed / awk delete line, results as a variable
Now I'm trying to delete the first line of a some text that is stored as a variable and store the output as a variable. I open a file and store the results in a file but can seem to make things work with variable in / out
sed -n '1d' infile > outfile - this works with actual files
sed -n '1d' $varin > $varout - doesn't work
I'm pretty new to scripting and am having issues with syntax etc...
I'd also like it if the commands were standard and worked with ksh.
Your problem here is that 'sed' is expecting a filename as an argument. The string you are providing ('$varin') does not happen to be a filename. If you want sed to work on your string (as opposed to a file whose name shares the value of your string), you can do a few things...
Pipe the string into sed...
echo "$varin" | sed '1d'
Redirect the string into sed...
sed '1d' << !!
You can wrap the code above with $() or ``, or use 'read' if you want to assign the output to a variable.
The second example will pass $varin into sed exactly as it is stored. The first example however needs "" (i.e. "$varin") to prevent 'echo' tokenizing the input string and outputting with a single space delimiter (which is why echo $varin is all on one line but echo "$varin" is not).
echo a b c
echo "a b c"
Furthermore, note that if you used single quotes ' ', you would output the literal '$varin' because single quotes would prevent the shell evaluating and expanding $varin to the value of the variable with that name.