Need assistance with sed


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Need assistance with sed
# 8  
Old 04-12-2011
Hi Dendany83,

Another option if the text you want to replace always contains 2 lines:
Code:
cat inputfile
begin this is
this is ...................
that is.................
begin this is
-------------------------------------------------------------------------------------------------
text1="Device          |            IPMB0-A              |             IPMB0-B"
text2="Board      Address      |Sent      SentErr      %Errr      |Sent      SentErr      %Errr"
 
sed "s/^begin.*/$text1\n$text2/" inputfile
Device          |            IPMB0-A              |             IPMB0-B
Board      Address      |Sent      SentErr      %Errr      |Sent      SentErr      %Errr
this is ...................
that is.................
Device          |            IPMB0-A              |             IPMB0-B
Board      Address      |Sent      SentErr      %Errr      |Sent      SentErr      %Errr


Hope this helps,

Regards
# 9  
Old 04-12-2011
Quote:
Originally Posted by cgkmal
Hi Dendany83,

Another option if the text you want to replace always contains 2 lines:
Code:
cat inputfile
begin this is
this is ...................
that is.................
begin this is
-------------------------------------------------------------------------------------------------
text1="Device          |            IPMB0-A              |             IPMB0-B"
text2="Board      Address      |Sent      SentErr      %Errr      |Sent      SentErr      %Errr"
 
sed "s/^begin.*/$text1\n$text2/" inputfile
Device          |            IPMB0-A              |             IPMB0-B
Board      Address      |Sent      SentErr      %Errr      |Sent      SentErr      %Errr
this is ...................
that is.................
Device          |            IPMB0-A              |             IPMB0-B
Board      Address      |Sent      SentErr      %Errr      |Sent      SentErr      %Errr


Hope this helps,

Regards
It is giving an error:
Code:
Label too long: text1="        Device          |             IPMB0-A              |             IPMB0-B              "

Code:
#!/bin/sed -f

text1="        Device          |             IPMB0-A              |             IPMB0-B              "
text2="Board      Address      |Sent      SentErr      %Errr      |Sent      SentErr      %Errr      "
text3="=============================================================================================="

s/^$/$text1\n$text2\n$text3/ 2.log

where 2.log is:
Code:
                                                                                            
           9a            60        36           60.0       60        0            0.0
           96            60        35           58.3       55        0            0.0
           92            60        60           100.0      60        60           100.0
           8e            60        35           58.3       60        0            0.0
                                                                                             
           9a            60        19           31.6       60        0            0.0
           96            60        20           33.3       54        0            0.0
           92            60        60           100.0      60        60           100.0
           8e            60        20           33.3       60        0            0.0

The idea is that i want to add this header in place of each blank line.
# 10  
Old 04-13-2011
Quote:
Originally Posted by Dendany83
Sorry but this command is giving me an error:

Code:
#sed -e :a -e '$! s/$/\\n/;' -e 'N;s/\n//g' -e 'ta' header

Code:
Unrecognized command: $! s/$/\\n/;

i think you run in solaris or aix or other Unix variants.
if we suppose your system as solaris or other , sed has some limits unfortunately and does not understand what is newline in its..Smilie
if we coold run successfully this, results are useless again.because do not make sense for SED in some systems which i mentioned above.
Code:
#sed -e :a -e '$! s/$/\\n/;' -e 'N;s/\n//g' -e 'ta' header

if you try this in spite of everything for learning this command's output you can try like this (is same but no error Smilie )
Code:
# sed -e ':a' -e 'N;$!ba' -e 's/\n/\\n/g' header

However we must a find a solution for Sed Lovers Smilie

I use test four lines in text (as your header) file..

Code:
# cat text
       Device          |            IPMB0-A              |             IPMB0-B
Board      Address      |Sent      SentErr      %Errr      |Sent      SentErr      %Errr
Board1111      Address      |Sent      SentErr      %Errr      |Sent      SentErr      %Errr
Board1212121      Address      |Sent      SentErr      %Errr      |Sent      SentErr      %Errr

Code:
# cat infile
begin this is
this is ...................
that is.................
begin this is

I use the file named text that has contents to add in our infile
main code is below..if you want you can use for as in script file.
Code:
xdel=() ; xdel=($(sed -n '/^begin/=' infile))
y=0 ; x=() ; while IFS='\n' read -r line; do x=("${x[@]}" "$line"); ((y++)) ; done <text
cp infile infileorg ; loop=notok;loopinc=notok;wh=1;for i in ${xdel[@]} ; do for j in "${x[@]}" ;do if [ $wh = 1 ] ; then c=c ; else c=a ; fi ; 
if (( $wh > 2 )) ; then ((i++)); fi;if [ $loop = ok ] && [ $loopinc != ok ] ; then (( i = i + y - 1 )) ;loopinc=ok; fi ; sed ''$i' '$c'\
>'"$j"' ' infile>infiletmp ;mv infiletmp infile;((wh++)); done ; wh=1 ;loop=ok ; done; 
echo -e "\n===== Org File(infileorg) =====\n\n$(cat infileorg)\n\n ===== Output File(infile) =====\n\n$(cat infile)\n"

===== Org File(infileorg) =====

begin this is
this is ...................
that is.................
begin this is

 ===== Output File(infile) =====

       Device          |            IPMB0-A              |             IPMB0-B
Board      Address      |Sent      SentErr      %Errr      |Sent      SentErr      %Errr
Board1111      Address      |Sent      SentErr      %Errr      |Sent      SentErr      %Errr
Board1212121      Address      |Sent      SentErr      %Errr      |Sent      SentErr      %Errr
this is ...................
that is.................
       Device          |            IPMB0-A              |             IPMB0-B
Board      Address      |Sent      SentErr      %Errr      |Sent      SentErr      %Errr
Board1111      Address      |Sent      SentErr      %Errr      |Sent      SentErr      %Errr
Board1212121      Address      |Sent      SentErr      %Errr      |Sent      SentErr      %Errr

regards
ygemici
# 11  
Old 04-15-2011
[QUOTE=ygemici;302513572]i think you run in solaris or aix or other Unix variants.
if we suppose your system as solaris or other , sed has some limits unfortunately and does not understand what is newline in its..Smilie
if we coold run successfully this, results are useless again.because do not make sense for SED in some systems which i mentioned above.
Code:
#sed -e :a -e '$! s/$/\\n/;' -e 'N;s/\n//g' -e 'ta' header

if you try this in spite of everything for learning this command's output you can try like this (is same but no error Smilie )
Code:
# sed -e ':a' -e 'N;$!ba' -e 's/\n/\\n/g' header

However we must a find a solution for Sed Lovers Smilie

Hi ygemici,

Yes, you are absolutely right, it is working fine on Cygwin but not on Solaris! Now i'm doing my tests on Linux platform (target platform for my project) and it is showing the below result (no errors but the o/p is not fully OK):

Code:
# sed "s/^$/$(sed -e :a -e '$! s/$/\\n/;' -e 'N;s/\n//g' -e 'ta' header)/" file1.log

Code:
        Device          |             IPMB0-A              |             IPMB0-BnBoard      Address      |Sent      SentErr      %Errr      |Sent      SentErr      %Errr      n============================================================================================
Board1;9a;60;36;60.0;60;0;0.0
Board2;96;60;35;58.3;55;0;0.0
Board3;92;60;60;100.0;60;60;100.0
Board4;8e;60;35;58.3;60;0;0.0
        Device          |             IPMB0-A              |             IPMB0-BnBoard      Address      |Sent      SentErr      %Errr      |Sent      SentErr      %Errr      n============================================================================================
Board1;9a;60;19;31.6;60;0;0.0
Board2;96;60;20;33.3;54;0;0.0
Board3;92;60;60;100.0;60;60;100.0
Board4;8e;60;20;33.3;60;0;0.0

Code:
#sed "s/^$/$(sed -e ':a' -e 'N;$!ba' -e 's/\n/\\n/g' header)/" file1.log

Code:
        Device          |             IPMB0-A              |             IPMB0-BnBoard      Address      |Sent      SentErr      %Errr      |Sent      SentErr      %Errr      n============================================================================================
Board1;9a;60;36;60.0;60;0;0.0
Board2;96;60;35;58.3;55;0;0.0
Board3;92;60;60;100.0;60;60;100.0
Board4;8e;60;35;58.3;60;0;0.0
        Device          |             IPMB0-A              |             IPMB0-BnBoard      Address      |Sent      SentErr      %Errr      |Sent      SentErr      %Errr      n============================================================================================
Board1;9a;60;19;31.6;60;0;0.0
Board2;96;60;20;33.3;54;0;0.0
Board3;92;60;60;100.0;60;60;100.0
Board4;8e;60;20;33.3;60;0;0.0

is there any WA? or it is the same problem of \n? this is the only blocking problem to finalize my script Smilie

As for the last code you have mentioned, this is completely far from my knowledge Smilie it will take time for me to forget that Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Assistance required to decode sed search using /1

Hi, I am trying to extract line number (first number), as well as everything from TSVal onwards. 4 1.474005 172.18.124.142 -> 74.125.228.46 TCP 2450940617 74 44021 > https Seq=0 Win=5840 Len=0 MSS=1380 SACK_PERM=1 TSval=2450940617 TSecr=0 WS=64 6 1.488149 172.18.124.142 ->... (1 Reply)
Discussion started by: sand1234
1 Replies

2. Shell Programming and Scripting

sed assistance

Hello everyone. I am trying to replace sprintf(buffer, "{\"id\":1,\"method\":\"mining.update_block\",\"params\":}\n", coinid, blockhash); with sprintf(buffer, "{\"id\":1,\"method\":\"mining.update_block\",\"params\":}\n", coinid, blockhash); this is the code I was trying but is... (9 Replies)
Discussion started by: crombiecrunch
9 Replies

3. UNIX for Dummies Questions & Answers

Sed/command assistance

Hello, I'm attempting to play with sed commands again... I have a file named test1 with numbers...ex:5551234567 I run this sed on the file... cat test1 | sed 's/^/homeDnModify "/g' | sed 's/$/" "" 3/g' >test2 Im hoping it will look like this... homeDnModify "551235" "4567" ""... (5 Replies)
Discussion started by: jay11789
5 Replies

4. UNIX for Dummies Questions & Answers

Sed/command assistance

Hello all, I need some help and education creating a script. Basically I have a file with a list of numbers.. 2125554444 2124445555 I need to put them into a format that looks like this.... UQ-V8.1,2125554444,hdaudio UQ-V8.1,2124445555,hdaudio Any help would be greatly... (6 Replies)
Discussion started by: jay11789
6 Replies

5. Shell Programming and Scripting

sed newbie scripting assistance

Howdy folks, I'm trying to craft a log file summarisation tool for an application that creates a lot of duplicate entries with only a different suffix to indicate point of execution. I thought I'd gotten close but I'm clearly missing something. Here's a genericized version: A text_file... (3 Replies)
Discussion started by: mthespian
3 Replies

6. Shell Programming and Scripting

Need assistance with sed

Hi All, I need your assistance, I would like to replace all lines beginning with the word "begin" with the below text: Device | IPMB0-A | IPMB0-B Board Address |Sent SentErr %Errr |Sent SentErr ... (9 Replies)
Discussion started by: Dendany83
9 Replies

7. Shell Programming and Scripting

sed search and replace word assistance...

Hi, I am trying to write a shell script designed to take input line by line by line from a file with a word on each line for editing with sed. Example file: 1.ejverything 2.bllown 3.maikling 4.manegement 5.existjing 6.systems My design currently takes input from the user, and... (2 Replies)
Discussion started by: mkfitzwilliams
2 Replies

8. Shell Programming and Scripting

Need assistance with appending strings using sed and variables

HI, Can't seem to find anything on the forums to fix this. I have a file, one line within this will not have a specific string at the end. I have the string, but need to append it to the specific line which has it missing. I need to use a variable for this, $string - I am using double... (13 Replies)
Discussion started by: mandriver
13 Replies

9. UNIX for Advanced & Expert Users

assistance requested (sed related)

I gotta write a command to change the accounts in /etc/passwd that use a shell other than the bash to bash shell. those accounts that dont use a shell shouldnt get modified. assuming all the shell programs end in sh and other programs dont. and the result should go into /etc/passwd.rev any hint? (4 Replies)
Discussion started by: metalwarrior
4 Replies

10. UNIX for Advanced & Expert Users

need assistance: sed and repeating patterns

hi, I need to write a command with sed to find all the lines in a file that contain patterns of three or more characters that repeat once and put them inside perenthezes. I cannot tell sed what pattern to look for. it should find repeated patterns automatically. example:... (1 Reply)
Discussion started by: metalwarrior
1 Replies
Login or Register to Ask a Question