LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 07-23-2013, 09:08 AM   #16
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444

Never used solaris, but looking at the man for it you need {}
http://docs.oracle.com/cd/E23823_01/...165/sed-1.html


I have no idea if this will work
Code:
sed '/^; Nodes/ {
a \
'"$variable"'
}' file.txt
 
Old 07-23-2013, 09:28 AM   #17
ravikushal
LQ Newbie
 
Registered: Apr 2010
Posts: 8

Original Poster
Rep: Reputation: 0
Hi druuna ,

Sory i didnt mention about the solaris.
Also confirmed that some functionality of sed is not working properly on Solaris .

Executed below :

bash-3.2# variable="this is my top node"
bash-3.2# echo $variable
this is my top node
bash-3.2# sed "s/^; Nodes/; Nodes\n$variable/" file.txt > new.file.txt
bash-3.2# mv new.file.txt file.txt
bash-3.2# cat file.txt
hello linux
; Nodes
this is my first node
this is second
this is third one
this is fourth
;
bash-3.2#
bash-3.2# cat new.file.txt
hello linux
; Nodes
this is my first node
this is second
this is third one
this is fourth
;
bash-3.2# mv new.file.txt file.txt
bash-3.2# cat file.txt
hello linux
; Nodes
this is my first node
this is second
this is third one
this is fourth
;
bash-3.2#
bash-3.2# cat new.file.txt
hello linux
; Nodes
this is my first node
this is second
this is third one
this is fourth
;

------------------------------------------
bash-3.2# /usr/xpg4/bin/sed "s/^; Nodes/; Nodes\n$variable/" file.txt > new.file.txt
bash-3.2#cat new.file.txt
hello linux
; Nodes
this is my first node
this is second
this is third one
this is fourth
;
bash-3.2# cat file.txt
hello linux
; Nodes
this is my first node
this is second
this is third one
this is fourth
;
bash-3.2#



Here , after " ; Nodes " the text "this is my top node" should come i think.
please correct me if i am wrong .
 
Old 07-23-2013, 09:29 AM   #18
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
Ugly Awk

Code:
awk '/^; Nodes/{printf "%s\n%s\n","; Nodes","'"${variable}"'"};!/^; Nodes/{print}' file.txt
NBo idea if that will work either D)
 
Old 07-23-2013, 09:56 AM   #19
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Try this awk solution:
Code:
/usr/xpg4/bin/awk -v variable="$variable" '{ print} /^; Nodes/ { print variable }' file.txt
You probably need to use the awk version in /usr/xpg4/bin, the standard awk version on Solaris might not work.

Scripted form:
Code:
#!/bin/bash

variable="this is my top node"

/usr/xpg4/bin/awk -v variable="$variable" '{ print} /^; Nodes/ { print variable }' file.txt > new.file.txt

# uncomment below line to make changes in original file
#mv new.file.txt > file.txt
 
  


Reply


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] Replace multi line pattern by text variable in sed XXLRay Linux - Software 2 11-22-2012 11:05 AM
Help with sed regex to match words via a pattern. logar0 Linux - Newbie 3 10-24-2010 05:33 PM
How to use sed to delete all lines before the first match of a pattern? C_Blade Linux - Newbie 9 05-01-2010 05:18 AM
simple pattern match with awk, sed alenD Linux - Newbie 10 03-10-2008 03:31 PM
sed script to append variable text gmartin Linux - General 4 12-27-2006 05:44 PM


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

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