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
Storing commands in $variables. Paulw0t Shell Programming and Scripting 7 08-08-2008 06:42 PM
Using variables in sed commands hcclnoodles Shell Programming and Scripting 2 01-02-2007 08:56 AM
Referencing variables in commands mharley Shell Programming and Scripting 3 03-31-2005 02:37 AM
Subtracting Variables which are commands Astudent UNIX for Dummies Questions & Answers 2 03-30-2001 10:55 AM
Subing Variables with commands. Astudent UNIX for Dummies Questions & Answers 1 02-26-2001 05:02 AM

Closed Thread
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 01-03-2009
mustaine85 mustaine85 is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 5
Using variables within awk/sed commands

Can I use my own variables within awk and sed for example:
I've written a while loop with a counter $i and I want to use the value of $i within sed and awk to edit certain lines of text within a data file.

I want to use :
sed '1s/$/texthere/g' data.csv
Like this:
sed '$is/$/$age/g' data.csv
but it only displays the name of the variable not the value it contains.

$age is the result of a calculation created within the while loop that I would like to append to the end of each line.
Any help is greatly appreciated
  #2 (permalink)  
Old 01-03-2009
Christoph Spohr Christoph Spohr is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 205
Hi,

shell variables inside '...' are not expanded. You'll have to
use "...". Further use ${i}s instead of $is.

HTH Chris
  #3 (permalink)  
Old 01-03-2009
mustaine85 mustaine85 is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 5

Code:
sed "${i}s/$/${age}/g" data.csv

Works great thank you very much
  #4 (permalink)  
Old 01-08-2009
Nicol Nicol is offline
Registered User
  
 

Join Date: Sep 2003
Location: Paris
Posts: 128
Hi ,

i used with success the sed command : ( in the date value the slash is protected by \ )

cat crontab-test|sed -e "s/Modifie le : 20\/06\/08/Modifie le : 11\/22\/33/g"

but when i want to put the values in variables

j_modif_old="20/06/08"
j_modif_new="11/22/33"

cat crontab-test|sed -e "s/Modifie le : ${j_modif_old}/Modifie le : ${j_modif_new}/g"

i have the following error :
sed: Function s/Modifie le : 20/06/08/Modifie le : 11/22/33/g cannot be parsed.

how can i do this , protecting the "/" when parsing ?

thanks in advance
Christian
  #5 (permalink)  
Old 01-08-2009
otheus's Avatar
otheus otheus is offline Forum Staff  
Moderator ala Mode
  
 

Join Date: Feb 2007
Location: Innsbruck, Austria
Posts: 1,893
Instead of using s/.../.../ try s^...^...^ or another character.
  #6 (permalink)  
Old 01-08-2009
Nicol Nicol is offline
Registered User
  
 

Join Date: Sep 2003
Location: Paris
Posts: 128
Thank you very very much !

bye
Christian
Closed Thread

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 02:38 PM.


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