How to use a varibale in sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to use a varibale in sed
# 1  
Old 05-30-2008
How to use a variable in sed

i wanna insert a row in a file.

I m using this command to insert a row in the 13th line.
sed '13i\
NewRow' MyFile > temp.txt
mv temp.txt to MyFile

this works fine.
now i wanna get the line no using grep into a variable and then use tht variable to insert a new row..
how to do this wid sed..
i tried this in vain

v1=13
sed -v var=$v1 '$vari\
NEWROW ' MyFile

Last edited by Yogesh Sawant; 05-30-2008 at 04:43 AM.. Reason: corrected the title
# 2  
Old 05-30-2008
sed can do all, and if you sed support -i you don't have to use the temp file.
Code:
sed -i "/pattern/ s/$/\nNEWROW/" file

# 3  
Old 05-30-2008
call sed with

Code:
echo "NEW LINES CONTENT" | sed ..your..stuff....

it echos the stuff and with the pipe it goes to sed, just like it came from a file.
but leave away the input file in the sed command ofcourse
# 4  
Old 05-30-2008
i got this err..

> sed -i "/pattern/ s/$/\nNew Row/" filesed: illegal option -- i
Usage: sed [-n] Script [File ...]
sed [-n] [-e Script] ... [-f Script_file] ... [File ...]

help me again pls..
# 5  
Old 05-30-2008
Remove the -i and use a temp file.
Code:
sed "/pattern/ s/$/\nNew Row/" file > temp && mv temp file

# 6  
Old 05-30-2008
thnx..

but 1 more thing.. '\n' doesnt work gives the new line data in the same line..

sed "/pattern/ s/$/\nNew Row/" file

output
old data1
old data2
old data3
old data4
old data5
old data6nNew Row
old data7
# 7  
Old 05-30-2008
guys rep pls!!!


i hv one more prob.. now this is another simple stuff tht i m stuck wid..

i wanna use grep in a if-else statement... but thrs a prob here

if [ !( grep -q pattern1 MyFile) -o !( grep -q pattern2 MyFile) ]
then
echo hi
else
echo hello
fi

now here i want the if part to execute only if pattern1 or 2 is not thr..

actu thrs no else part to this but i just wanted to chk wht was returned by the condition..

doing these checks individually is an option but i wanna put it in one condition only..

pls help in both cases!!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to pass IF condition via shell varibale in awk?

Hello, I have one query regarding passing IF condition shell variable inside awk. Here is the case- File content of keydefn.exp 201~2~LM Limit 02-current value~Limit 02 ~Limit02~Current~Value ~N~Y~S~0~9999999 201~3~LM Limit 03-current value~Limit... (2 Replies)
Discussion started by: anillambait
2 Replies

2. Shell Programming and Scripting

I am learning regular expression in sed,Please help me understand the use curly bracket in sed,

I am learning SED and just following the shell scripting book, i have trouble understanding the grep and sed statement, Question : 1 __________ /opt/oracle/work/antony>cat teledir.txt jai sharma 25853670 chanchal singhvi 9831545629 anil aggarwal 9830263298 shyam saksena 23217847 lalit... (7 Replies)
Discussion started by: Antony Ankrose
7 Replies

3. Shell Programming and Scripting

sed and awk giving error ./sample.sh: line 13: sed: command not found

Hi, I am running a script sample.sh in bash environment .In the script i am using sed and awk commands which when executed individually from terminal they are getting executed normally but when i give these sed and awk commands in the script it is giving the below errors :- ./sample.sh: line... (12 Replies)
Discussion started by: satishmallidi
12 Replies

4. UNIX for Dummies Questions & Answers

set varibale to be output of a command

Hi there! :) How to set varibale to be output of a command in csh. I was using set i="date+'%y%m%d'" but the output is date+'%y%m%d' and without quites and with a single quote the output is the same :wall: :eek: Thanks in advance (2 Replies)
Discussion started by: FUTURE_EINSTEIN
2 Replies

5. UNIX for Dummies Questions & Answers

SED: Can't Repeat Search Character in SED Output

I'm not sure if the problem I'm seeing is an artifact of sed or simply a beginner's mistake. Here's the problem: I want to add a zero-width space following each underscore between XML tags. For example, if I had the following xml: <MY_BIG_TAG>This_is_a_test</MY_BIG_TAG> It should look like... (8 Replies)
Discussion started by: rhetoric101
8 Replies

6. Shell Programming and Scripting

sed over writes my original file (using sed to remove leading spaces)

Hello and thx for reading this I'm using sed to remove only the leading spaces in a file bash-280R# cat foofile some text some text some text some text some text bash-280R# bash-280R# sed 's/^ *//' foofile > foofile.use bash-280R# cat foofile.use some text some text some text... (6 Replies)
Discussion started by: laser
6 Replies

7. Shell Programming and Scripting

Issue with a sed one liner variant - sed 's/ ; /|/g' $TMP1 > $TMP

Execution of the following segment is giving the error - Script extract:- OUT=$DATADIR/sol_rsult_orphn.bcp TMP1=${OUT}_tmp1 TMP=${OUT}_tmp ( isql -w 400 $dbConnect_OPR <<EOF select convert(char(10), s.lead_id) +'|' + s.pho_loc_type, ";", s.sol_rsult_cmnt, ";", +'|'+ s.del_ind... (3 Replies)
Discussion started by: kzmatam
3 Replies

8. Shell Programming and Scripting

passing of a varibale to subshell

Hi All, I need some info. Could you please tell me how to use the variable of a parent shell in the subshell. Also can we modify the variable in the subshell ? If yes, will the modified variable visible in the parent shell I am using two prg. a.sh #!/usr/bin/ksh temp_var="abhishek"... (3 Replies)
Discussion started by: AbhishekG
3 Replies

9. Shell Programming and Scripting

Varibale to NULL or ZERO

I have a variable called V_param1 and does it have some value. I want to check this varibale is NULL or Zero then exit else do How to incorported this in Script. any input (2 Replies)
Discussion started by: u263066
2 Replies

10. Shell Programming and Scripting

passing result of query to a varibale in ksh script

Hi, I have a scenario where in i have to extarct max of one column and pass it to a variable. i have tried to export the result as .dat file and read from that file.But my database is mainframe and it is not permitting me to export in .dat file.I have tried using .ixf file but reading from... (2 Replies)
Discussion started by: ammu
2 Replies
Login or Register to Ask a Question