Append variable value with sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Append variable value with sed
# 1  
Old 02-23-2014
Append variable value with sed

I have a requirement where I need to add a variable value based on a pattern match. When I try this it works:

Code:
sed '/IS_ETL_DEV/a\
ABCD' File1.txt > File1.tmp

Now I want to replace IS _ETL_DEV with a variable like $Pattern and replace the values ABCD with a variable $Var the value "$$XYZ=1". So, I replaced the code like this:

Code:
sed '/${Pattern}/a\
${Var}' File1.txt > File1.tmp

So, when the pattern is found, it will add a line $$XYZ=1 after it. I am using Solaris 10.
# 2  
Old 02-23-2014
Hi guy,
the $'s special mean would not be effect in single quote, so you may try this.
Code:
sed '/'"${Pattern}"'/a\
'"${Var}"'' File1.txt > File1.tmp

and let Var='$$XYZ=1'
# 3  
Old 02-23-2014
Or only use double quotes and escape special characters:
Code:
sed "/${Pattern}/a\\
${Var}" file

# 4  
Old 02-23-2014
Thank you. Both the solutions work. But I have another question. Why do we have to give the substitution part in the 2nd line. Cant I give the whole command in one line? I tried and it did not work. Appreciate your inputs.
# 5  
Old 02-23-2014
For the append command you need two lines with regular sed. You can try with GNU sed (gsed):
Code:
sed "/${Pattern}/a${Var}" file

Code:
sed "/${Pattern}/s/\$/\\n${Var}/" file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash - proper way to append variable to stderr

Hello, Can you please if the bellow is the proper way of appending a variable to the stderr: The easiest way to test this,I was able to imagine, was by touching 5 files and afterwards looping trough to the results: -rw-r--r-- 1 ab owner 0 Sep 14 13:45 file1 -rw-r--r-- 1 ab owner 0 Sep... (7 Replies)
Discussion started by: alex2005
7 Replies

2. Shell Programming and Scripting

How to Append data to the content at the beginning of a variable?

Hi Guys, IF i have to append data to a variable, i simply use the following. What can i do to append data to the beginning? VAR+="data" (6 Replies)
Discussion started by: srkmish
6 Replies

3. Programming

C program to read n files and append it to a variable.

I am struck in the code of handling multiple files reading and appending it into a variable. My First file's entire content I have concatenated using strcat(<char pointer>,char array) In the Second file I extract using strtstr() function and below is the code. if( fp2 != NULL){ ... (2 Replies)
Discussion started by: gameboy87
2 Replies

4. Shell Programming and Scripting

Append 0 to a variable

Hi, I have a variable... it can take a value from 1 to 99... now when it is 1 to 9 i want to save it as 01 to 09 in the same variable... can this be done using sed or awk??? a=1 i want it as a=01 Thanks in advance... (8 Replies)
Discussion started by: Nithz
8 Replies

5. Shell Programming and Scripting

append to same string variable in loop

I want to append values to same string variable inside a recursive function that I have .. I do not want to write to any file but use a variable.. Can anyone please help with it? Thanks in advance. (6 Replies)
Discussion started by: Prev
6 Replies

6. UNIX for Dummies Questions & Answers

how do I append new lines to awk variable?

I want to build an array using awk, consisting only of a subset of lines of a file. I know how to have awk assess whether a key phrase is in a particular line, but I can't find anywhere how to then append the line containing that phrase to an array that has previously-found lines also containing... (9 Replies)
Discussion started by: pts2
9 Replies

7. Shell Programming and Scripting

Append zeros before a value as per variable

Hello- I have a variable which contains a number, I need to populate number of zeros before another value as per this variable value. for example: I have variable X whose content is 5, variable Y whose content is 123 Now append number of zeros as per variable X before varible 'Y'... (4 Replies)
Discussion started by: pasupuleti81
4 Replies

8. Shell Programming and Scripting

How to append using sed

Hello all, I want to use sed to append a variable stored in $i, how do i do that? $i contains a path to a directory and i want to append it before the result i get from awk statement, before affecting awk results. rite now out put i get from code below is:- The path is:... (2 Replies)
Discussion started by: asirohi
2 Replies

9. Shell Programming and Scripting

Append variable to only certain lines

Hi There! I'm trying to write a shell script which generates a random hexadecimal number and then appends it to the end of lines XX onwards of a certain file. XX is determined by a certain string on the previous line. Thus for example, if the input file is I search for the string... (3 Replies)
Discussion started by: orno
3 Replies

10. Shell Programming and Scripting

Extract Variable and Append it to the file

I have a file named xyz_extract_file_20091201.dat and I need to strip out 20091201 from the file name and append this value as a first field in all the records in the file. Can you please help? Thanks. Eg. Input File: xyz_extract_file_20091201.dat ------------ Campaign1,A2347,Grp_id1... (1 Reply)
Discussion started by: shekharaj
1 Replies
Login or Register to Ask a Question