If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > sed / awk delete line, results as a variable

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-21-03, 13:46
Douglas Anderso Douglas Anderso is offline
Registered User
 
Join Date: Jul 2003
Posts: 6
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.

Thanks,
Doug.
Reply With Quote
  #2 (permalink)  
Old 07-21-03, 17:09
LKBrwn_DBA LKBrwn_DBA is offline
Registered User
 
Join Date: Jun 2003
Location: West Palm Beach, FL
Posts: 2,456
Try:

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.
Reply With Quote
  #3 (permalink)  
Old 07-22-03, 05:24
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
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...

e.g.

Pipe the string into sed...

echo "$varin" | sed '1d'

Redirect the string into sed...

sed '1d' << !!
$varin
!!

You can wrap the code above with $() or ``, or use 'read' if you want to assign the output to a variable.

Note:

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).

Try...

echo a b c
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.

HTH
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On