Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2003

    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

    tried this:
    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.

    Any help would be appreciated.


  2. #2
    Join Date
    Jun 2003
    West Palm Beach, FL

    varout="$(echo $varin|sed -n '1d' )"

    Note: This will only work if the lines in 'varin' are actuallly separated by the new-line character '\n'.

    When you do something like:

    varin="$(cat filein)"

    the '\n's (new-lines) are transformed into spaces, therefore echo $varin will have only one record.

  3. #3
    Join Date
    Jun 2002
    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"
    echo "a

    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.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts