Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 12-06-2012, 10:02 AM   #1
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 10:07 AM.
Old 12-06-2012, 10:11 AM   #2
LQ Veteran
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

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

2 solutions: use double quotes:
sed "s/\(.*EUR\).*/\1$total/" infile
or using single quotes:
sed 's/\(.*EUR\).*/\1'$total'/' infile
Old 12-06-2012, 02:53 PM   #3
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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.

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

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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 06:31 PM
[SOLVED] sed help to run sed command against multiple different file names bkone Programming 2 04-16-2012 01:27 PM
how to use variable in sed command ? anandg111 Linux - Newbie 3 01-11-2012 12:27 PM
sed substitution of variable with a variable ngyz86 Linux - Newbie 6 01-05-2011 08:44 AM
Sed search for variable and delete entire line, but variable contains forward /'s Passions Programming 2 11-10-2008 04:44 PM

All times are GMT -5. The time now is 08:09 AM.

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