Sponsored Content
Top Forums Shell Programming and Scripting Passing variables to a sed function Post 302908667 by richardsantink on Wednesday 9th of July 2014 12:18:12 PM
Old 07-09-2014
Passing variables to a sed function

Hi everyone,
I've re-written some of our scripts to use more functions and I've run into a situation where passing a variable to a sed function does not work. My function is a one-liner sed command as follows:

Code:
function StringSub()
{
sed -i "${1}/${2}/${3}/${4}" ${5}
}

Where ${1} through ${4} represent command/ pattern/ replacement/ flag/ respectively. ${5} is used to pass the filename.
It may seem like overkill but I am hoping I can take the five parameters from a .csv file to pass to the function, down the road once this is developed more.

Code:
echo "Enter the two-digit month, and the four-digit year for the report, then press enter."
read tidyMonth tidyYear
#use the values to create a date column, always set to the first of the month.
awk -v column=1 -v value="${tidyMonth}/01/${tidyYear}" '
BEGIN {
FS = OFS = ",";
}
{
for (i=NF +1; i > column; i-- ) {
$i = $(i-1);
}
$i = value;
print $0;
}
' originalSource.csv > mysource.csv
#Next, rename first row, first field to 'Date'
StringSub '1s' '${tidyMonth}\/01\/${tidyYear}' 'Date' 'g' 'mysource.csv'
StringSub '1s' 'Video' 'Product+Video' '' 'dateColumnAdded-02.csv'

The file mysource.csv takes the form:
Code:
06/01/2014,Video,Views
06/01/2014,video_title_1,34
06/01/2014,video_title_6,12
06/01/2014,video_title_3,102

Basically the StringSub function works, until I have a variable to pass that I suspect has slashes in it. Is it something I'm doing wrong in my expansion, or lack thereof? The second call of StringSub which renames 'Video' to 'Product+Video' works without any issue.
This is what I end up with:
Code:
06/01/2014,Product+Video,Views
06/01/2014,video_title_1,34
06/01/2014,video_title_6,12
06/01/2014,video_title_3,102


Last edited by Corona688; 07-09-2014 at 02:06 PM.. Reason: Use [code], not [icode] please
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing variables to sed

Hi folks, I'm looking for a solution to pass variables to a sed-command. I'm reading a lot of threats and also the q&a "How can I use a variable in sed?". None of these commands works. I'm using AIX 5.2. I want to do the following: NUMBER=` echo 38341` | sed -n '/$NUMBER/p' an obtained... (3 Replies)
Discussion started by: jfisch
3 Replies

2. Shell Programming and Scripting

passing variables to sed inside script

I am trying to pass a regular expression variable from a simple script to sed to remove entries from a text file e.g. a='aaaa bbbb cccc ...|...:' then executing sed from the script sed s'/"'$a"'//g <$FILE > $FILE"_"1 my output file is always the same as the input file !! any... (5 Replies)
Discussion started by: Daniel234
5 Replies

3. Shell Programming and Scripting

passing variables to sed function in a script ....

Hello , I have a script named testscript.sh wherein I have two variables $var and $final (both of which contain a number) I have a sed write function inside this script as follows: sed '1,2 w somefile.txt' fromfile.txt Now , in the above i want to pass $var and $final instead of... (2 Replies)
Discussion started by: shweta_d
2 Replies

4. Shell Programming and Scripting

Passing variables to sed

Hi Folks, How can I make the following to work from a korn shell? old="OLDSTRING" new="NEWSTRING" file="myfile.txt" sed -n 's/$old/$new/gp' $file Thanks in advance rogers42 (3 Replies)
Discussion started by: rogers42
3 Replies

5. Shell Programming and Scripting

Passing global variable to a function which is called by another function

Hi , I have three funcions f1, f2 and f3 . f1 calls f2 and f2 calls f3 . I have a global variable "period" which i want to pass to f3 . Can i pass the variable directly in the definition of f3 ? Pls help . sars (4 Replies)
Discussion started by: sars
4 Replies

6. Shell Programming and Scripting

sed pattern matching or passing variables

I need sed to add a "/>" to the end of a line that contains/starts with <meta. current line is <meta name="keywords" content="kayword 1, kwyword2"> and should be <meta name="keywords" content="kayword 1, kwyword2 " /> i need something like this? find . -name "*.html" -print0 | xargs... (6 Replies)
Discussion started by: sky_rivers
6 Replies

7. Shell Programming and Scripting

passing variables to sed in ksh

Hi, i need help passing variables to sed using ksh. My goal is to get particular data from log files. first i put a mark to the log files. echo "TEST_"`date + %m_%d_%Y_%T"` >markFile this will produce a 'markFile' which contain text like this TEST_06_01_2009_21:55:09 then i put the mark... (2 Replies)
Discussion started by: d.anggrianto
2 Replies

8. Shell Programming and Scripting

Passing two variables to function

HI ,I am a new in Bash and ,I dont know how to pass a second parameter to this fuction,if the name of the passed argument is num works fine,but if I try to pass secondNum,dont recognized it thanks function check() { if(($(echo ${#num}) == 0 )) then echo No arguments passed.Try... (6 Replies)
Discussion started by: lio123
6 Replies

9. Shell Programming and Scripting

Help with passing multiple variables into SED

Hello I am hoping you can help. I use ksh in Solaris9 I am trying to pass user imputed variables into SED but for some reason can only get SED to recognize one variable. I have experimented with te below command with putting ' ' and " " in different places but I cant seem to get it to... (8 Replies)
Discussion started by: lostincashe
8 Replies

10. Shell Programming and Scripting

Passing awk variables to bash variables

Trying to do so echo "111:222:333" |awk -F: '{system("export TESTO=" $2)}'But it doesn't work (2 Replies)
Discussion started by: urello
2 Replies
OCR4GAMERA(1)															     OCR4GAMERA(1)

NAME
ocr4gamera - OCR system using the Gamera framework USAGE
ocr4gamera -x <traindata> [options] <imagefile> OPTIONS
-v <int>, --verbosity=<int> Set verbosity level to <int>. Possible values are 0 (default): silent operation; 1: information on progress; >2: segmentation info is written to PNG files with prefix debug_. -h, --help Display help and exit. -d, --deskew Do a skew correction (recommended). -f, --filter Filter out very large (images) and very small components (noise). -a, --automatic-group Autogroup glyphs with classifier. -x <file>, --xmlfile=<file> Read training data from <file>. -o <xml>, --output=<xml> Write recognized text to file <xml> (otherwise it is written to stdout). -c <csv>, --extra_chars_csvfile=<csv> Read additional class name conversions from file <csv>. <csv> must contain one conversion per line. -R <rules>, --heuristic_rules=<rules> Apply heuristic rules <rules> for disambiguation of some chars. <rules> can be roman (default) or none (for no rules). -D, --dictionary-correction Correct words using a dictionary (requires aspell or ispell). -L <lang>, --dictionary-language=<lang> Use <lang> as language for aspell (when option -D is set). -e <int>, --edit-distance=<int> Correct words only when edit distance not more than <int>. OCR4GAMERA(1)
All times are GMT -4. The time now is 01:56 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy