LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-06-2012, 09:02 AM   #1
nwalsh88
LQ Newbie
 
Registered: Dec 2012
Posts: 8

Rep: Reputation: Disabled
sed command using variable


Hi All,

I am trying to run a sed command to add a variable to certain position on a line of a file.

file is below (trailerfile)

TOT_______________________________________________________________________________________________To tal EUR0000000000001568.50

i have a variable called total which is already defined as a numeric value (0000000000001582.18)

the command im using is:
sed 's:\(.\{107\}\)\(.\{19\}\):\1$total:' trailerfile

but its returning:
TOT_______________________________________________________________________________________________To tal EUR$total


any advice how to make 0000000000001582.18 appear instead of $total?

Last edited by nwalsh88; 12-06-2012 at 09:07 AM.
 
Old 12-06-2012, 09:11 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Using single quotes will tell the shell not to expand the variable.

2 solutions: use double quotes:
Code:
sed "s/\(.*EUR\).*/\1$total/" infile
or using single quotes:
Code:
sed 's/\(.*EUR\).*/\1'$total'/' infile
 
Old 12-06-2012, 01:53 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
1) Please use ***[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do not use quote tags, bolding, colors, "start/end" lines, or other creative techniques.

The long, unbroken lines in your post are forcing my screen to side-scroll. Edit it to enclose them, please.

2) Please try doing a search of past threads first. Questions like this tend to come up with good regularity.


In any case, this is a common example of how important it is to understand the shell's quoting behavior and argument parsing.

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes


3) You can avoid most of those backslashes if you enable the -r extended regular expressions option.

Code:
sed -r "s:(.{107})(.{19}):\1$total:" trailerfile
See the section on basic versus extended regex in the sed info page.
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] sed gives :sed: -e expression #1, char 1: unknown command: `'' samasat Linux - Newbie 10 06-09-2012 05:31 PM
[SOLVED] sed help to run sed command against multiple different file names bkone Programming 2 04-16-2012 12:27 PM
how to use variable in sed command ? anandg111 Linux - Newbie 3 01-11-2012 11:27 AM
sed substitution of variable with a variable ngyz86 Linux - Newbie 6 01-05-2011 07:44 AM
Sed search for variable and delete entire line, but variable contains forward /'s Passions Programming 2 11-10-2008 03:44 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:23 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration