Replace with a variable in sed command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace with a variable in sed command
# 1  
Old 01-05-2011
Replace with a variable in sed command

Hello,

I have this command and it works fine.
My question is that how can we replace the N by a variable, to print for instance a big number of lines. It means if I want 100 lines after an expression, to not put "N" 100 times in the sed.



Code:
$ sed -n '/aaa/{n;N;N;s/[ \n]//g;s/;/; /g;p;}' ttt2123 $ cat ttt20aaa12345678910
Thx & Regs,
Rany.
# 2  
Old 01-05-2011
On linux
Code:
sed -n '/aaa/,+100p' ttt2123

OR
Code:
a=100
sed -n '/aaa/,+${a}p' ttt2123

will print next 100 lines after pattern "aaa" match.
OR use awk to get same output:
Code:
awk '/aaa/{nr=NR}{if(nr && NR<nr+100) print}' ORS=" " ttt2123

OR
Code:
awk '/aaa/{nr=NR}{if(nr && NR<nr+a) print}' a=100 ORS=" " ttt2123


Last edited by anurag.singh; 01-05-2011 at 07:45 AM..
# 3  
Old 01-05-2011
ok, thx Singh
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sed/replace using variable

Hello everyone, In one of my shell script I am doing sed/replace using a variable to find a string in a file & replace it with another string in same file. The challenge which I am facing is that, the variable which I am using with "sed" is having one of character as "/" Here is below how... (4 Replies)
Discussion started by: gr8_usk
4 Replies

2. Shell Programming and Scripting

find and replace with variable -sed

Hi, I have a ksh script where I am trying to mask the password in the log files. $loc - is my directory $PGUIDE_DB_USER_PSW - is a variable that holds the password I am looking for find $loc/logs -type f -exec sed -i "s/$PGUIDE_DB_USER_PSW/*****/"g {} \; I get an error: ... (2 Replies)
Discussion started by: amitlib
2 Replies

3. Shell Programming and Scripting

Using sed command replace date variable in unix

I need to use a shell script, using sed command how to replace date variable value in following format. 04/18/2012 11:38:55 Because the sed is treating the '/' as a parameter instead of the value of a variable, and hence there is the message as sed: command garbled: s/insert/04/18/2012... (9 Replies)
Discussion started by: jannusuresh
9 Replies

4. Red Hat

How to pass value of pwd as variable in SED to replace variable in a script file

Hi all, Hereby wish to have your advise for below: Main concept is I intend to get current directory of my script file. This script file will be copied to /etc/init.d. A string in this copy will be replaced with current directory value. Below is original script file: ... (6 Replies)
Discussion started by: cielle
6 Replies

5. Shell Programming and Scripting

Using SED with variable to replace strings in while loop

Hi guys, Hi have this input (Menu.xml)<?xml version="1.0" encoding="ISO-8859-1"?> <breakfast_menu> <food> <name>Berry-Berry Belgian Waffles</name> <price>$8.95</price> <calories>900</calories> </food> <food> <name>French Toast</name> ... (6 Replies)
Discussion started by: cgkmal
6 Replies

6. Shell Programming and Scripting

using file-and-replace sed command with the use of a variable?

Ok, so, let's say I have the variable $GMAILID....How can I use it with sed command so to replace a string in a file? e.g.: sed -i 's/$GMAILID/test@gmail.com/' /home/$USER/Desktop/sendmail (4 Replies)
Discussion started by: hakermania
4 Replies

7. Shell Programming and Scripting

Loop with sed command to replace line with sed command in it

Okay, title is kind of confusion, but basically, I have a lot of scripts on a server that I need to replace a ps command, however, the new ps command I'm trying to replace the current one with pipes to sed at one point. So now I am attempting to create another script that replaces that line. ... (1 Reply)
Discussion started by: cbo0485
1 Replies

8. Shell Programming and Scripting

Insert a line including Variable & Carriage Return / sed command as Variable

I want to instert Category:XXXXX into the 2. line something like this should work, but I have somewhere the wrong sytanx. something with the linebreak goes wrong: sed "2i\\${n}Category:$cat\n" Sample: Titel Blahh Blahh abllk sdhsd sjdhf Blahh Blah Blahh Blahh Should look like... (2 Replies)
Discussion started by: lowmaster
2 Replies

9. UNIX for Dummies Questions & Answers

sed to replace text with a variable

I'm trying to replace text in a file with text from a variable I have the following in my script, but its not working: #!/bin/ksh echo "Enter the path to load scripts" read x echo "updating the templates" sed "s/CHANGE_ME_TO_LOAD_PATH/"$x"/g" LoadFiles.sh > LoadFiles2.sh I thought... (1 Reply)
Discussion started by: orahi001
1 Replies

10. Shell Programming and Scripting

replace with value of variable using SED

Hello everyone, I need to replace a pattern with "value of a variable" using sed but no luck. i tried the below ABC=STR2 sed 's/STR1/$ABC/g' <file> (also tried \ , $ and " in combination but failed) Appreciate any help. Thanks Prvn (2 Replies)
Discussion started by: prvnrk
2 Replies
Login or Register to Ask a Question