LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Question regarding awk or sed to find and replace a word in Solaris for Scripting (https://www.linuxquestions.org/questions/linux-newbie-8/question-regarding-awk-or-sed-to-find-and-replace-a-word-in-solaris-for-scripting-4175456028/)

karthik1asr 03-29-2013 10:38 AM

Question regarding awk or sed to find and replace a word in Solaris for Scripting
 
Hi all,

This is my first question at this forum. I am a newbie to shell scripting. I have any issue in finding a string and replacing it in shell scripting.

So here is my Text in the Input file and I have only one line in the file:

^^file=2013-03-26 13:08:01.214 04:00||^^header=64||2||AUE_SYSTEMBOOT||na||solaris-bsm-team2||2013-03-26 13:07:41.014 -04:00^^text=booting kernel^^header=54||2||AUE_init_solaris||na||solaris-bsm-team2||2013-03-26 13:08:01.675 -04:00^^text=booted^^return=success||0^^header=97||2||AUE_login||||solaris-bsm-team2||2013-03-26 13:08:19.811 -04:00^^subject=master||master||staff||master||staff||1166||2386485855||0 0 solaris-bsm-team2^^return=success||0

Now I need to replace all the occurrences of the string "^^header" and replace it with "\nheader" where \n is new line.

Can someone please help how to write it, so that i can use it as a part of my script.

Thanks in advance.

Best Regards

shivaa 03-29-2013 10:48 AM

Just try following:
Code:

~$ awk '{gsub(/\^\^header/,"\nheader",$0); print $0}' infile.txt
OR
~$ sed -e 's/\^\^header/\nheader/g' infile.txt


karthik1asr 03-29-2013 10:55 AM

Hi Shivaa,

thank you so much for the reply. you saved my time. The awk command just worked like a beauty.

I was trying until now like this::

awk '{gsub(\^\^header/,\nheader,$0); print $0}'

It didnt work. Thanks again man!!

shivaa 03-29-2013 11:40 AM

You are welcome! :)

Please Mark the thread as solved (option is under Thread Tools on top menu), if you think it has so.


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