The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Replacing in a variable belfastbelle UNIX for Dummies Questions & Answers 3 06-26-2009 10:08 AM
Replacing a pattern using variable? shivarajM Shell Programming and Scripting 3 04-30-2009 04:30 AM
Help in replacing text with the value of a variable lotto_123 Shell Programming and Scripting 4 06-13-2008 08:39 AM
Replacing $ in variable ashish_uiit UNIX for Dummies Questions & Answers 10 02-29-2008 09:29 AM
Replacing set of characters with a value of a variable prekida Shell Programming and Scripting 1 07-26-2005 09:14 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-16-2009
sideways sideways is offline
Registered User
  
 

Join Date: Jul 2009
Posts: 2
replacing line with variable

All I want to do is replace the 2nd line in a file with a variable, eg,

var=xxx

the file 'test' containing:
aaa
bbb
ccc

replace bbb with xxx

aaa
xxx
ccc

I had it working with sed on a redhat machine, but it doesn't work on a mac machine.
  #2 (permalink)  
Old 07-17-2009
vidyadhar85's Avatar
vidyadhar85 vidyadhar85 is offline Forum Staff  
Moderator(The Tutor)
  
 

Join Date: Jun 2008
Location: INDIA
Posts: 1,423
you can use sed or awk...

Code:
awk -v var="$your_var" 'NR==2{gsub($0,var)}{print}' filename

  #3 (permalink)  
Old 07-17-2009
sideways sideways is offline
Registered User
  
 

Join Date: Jul 2009
Posts: 2
Quote:
Originally Posted by vidyadhar85 View Post
you can use sed or awk...

Code:
awk -v var="$your_var" 'NR==2{gsub($0,var)}{print}' filename
That is just writing to standard out, not editing the actual file. I could redirect and mv it, but the files are large and I'd like to avoid having to do that.
  #4 (permalink)  
Old 07-17-2009
rakeshawasthi rakeshawasthi is offline
Registered User
  
 

Join Date: Aug 2004
Location: India
Posts: 379
Quote:
Originally Posted by sideways View Post
That is just writing to standard out, not editing the actual file. I could redirect and mv it, but the files are large and I'd like to avoid having to do that.
can be achieved using a tee

Code:
awk '{.....}' filename | tee filename

  #5 (permalink)  
Old 07-17-2009
panyam panyam is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2008
Posts: 474
I don't have much idea on "perl"..check for more options on editing on a particular line .

This might help you for inline coding


Code:
perl -i -pe 's/bbb/xxx/i;' file_name.txt



---------- Post updated at 10:07 PM ---------- Previous update was at 10:04 PM ----------

Good Rakesh ... I was thinking there will be some alternative to "perl"..since i prefer to do it in awk
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:05 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0