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 > newbie question

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-13-04, 12:07
subwrc subwrc is offline
Registered User
 
Join Date: Jan 2004
Location: Greece
Posts: 23
newbie question

HI all,

How can i replace the nth line of a file with the value of a variable ?

which utility should i use?
Reply With Quote
  #2 (permalink)  
Old 01-13-04, 14:58
fla5do fla5do is offline
Registered User
 
Join Date: Oct 2003
Location: Germany
Posts: 138
Hi subwrc,

what is a "nth line".
Please give an example to us.
__________________
Greetings from germany
Peter F.
Reply With Quote
  #3 (permalink)  
Old 01-13-04, 17:15
ika ika is offline
Registered User
 
Join Date: Oct 2003
Location: Slovakia
Posts: 482
Quote:
Originally posted by fla5do
Hi subwrc,

what is a "nth line".
Please give an example to us.
I guess he means n-th line (where n means whatever number).
Reply With Quote
  #4 (permalink)  
Old 01-14-04, 03:14
chillies chillies is offline
Registered User
 
Join Date: Jul 2003
Location: Edinburgh
Posts: 35
Re: newbie question

A bit of quoting hell, but this works in bash:

n=2
VAR="a text variable"
sed $n's/^.*$/'"$VAR"'/' < $FILE
Reply With Quote
  #5 (permalink)  
Old 01-14-04, 04:04
subwrc subwrc is offline
Registered User
 
Join Date: Jan 2004
Location: Greece
Posts: 23
Quote:
Originally posted by fla5do
Hi subwrc,

what is a "nth line".
Please give an example to us.
Sorry fla5do,as Ika said i mean the n-nth line where n=is a number

Thanks Chillies I 'm going to try it and repost

Last edited by subwrc; 01-14-04 at 04:07.
Reply With Quote
  #6 (permalink)  
Old 01-14-04, 06:05
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
Re: newbie question

Quote:
Originally posted by chillies
A bit of quoting hell, but this works in bash:

n=2
VAR="a text variable"
sed $n's/^.*$/'"$VAR"'/' < $FILE
Or like this...

sed "${n}s/.*/${VAR}/" $FILE
Reply With Quote
  #7 (permalink)  
Old 01-14-04, 07:36
chillies chillies is offline
Registered User
 
Join Date: Jul 2003
Location: Edinburgh
Posts: 35
Re: newbie question

I was worried that the .* could, in pathological circumstatnces, get expanded by the shell to a filename.

Now I'm confused:

$ n=2
$ VAR=a
$ mkdir -p 2s/.foo/a
$ touch 2s/.foo/a/bar
$ ls ${n}s/.*/${VAR}/
bar
$ ls "${n}s/.*/${VAR}/"
ls: 2s/.*/a/: No such file or directory

I suppose only variables are expanded in double quotes ...
Reply With Quote
  #8 (permalink)  
Old 01-14-04, 08:23
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
Re: newbie question

Quote:
Originally posted by chillies
I suppose only variables are expanded in double quotes ...
Correct. For a simpler example try...

echo *
echo "*"
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