LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-01-2010, 09:12 AM   #1
sstavdal
LQ Newbie
 
Registered: Jul 2010
Posts: 7

Rep: Reputation: 0
sed strange behaviour with line including backslash.


Hello,

I have a small script that takes input from one file, and uses this input to do a find/replace (with sed) to make an executable snmp trap.

The input file could look something like this :
JOHBURG SOUTH AFRICA
OSLO NORWAY
...
etc

The script takes the input from this file, and preserves all words in one line as a variable (so that the variables are "JOHBURG SOUTH AFRICA" and "OSLO NORWAY", instead of JOHBURG, SOUTH, AFRICA, OSLO, NORWAY.
This is working ok (in the given example, I get two variables).

sed is supposed to take this variable, and use it to substitute "HITME" with the new variable.


#!/bin/bash

N=0
cat tutticodes | while read LINE
do
N=$((N+1))
echo "Line $N = $LINE"
sed -e "s/HITME/${LINE}/g" template2 | sed -e 's/TERMINATE/\\/g'
done


The template2 file, looks like this :

/usr/bin/snmptrap -v 1 -c public 10.10.10.10 1.3.6.1.4.1.2378.1.1.1.1.4 10.10.10.11 0 2 ' ' \
1.3.6.1.4.1.2378.1.1.1.1.4.2.1 i 5 \
1.3.6.1.4.1.2378.1.1.1.1.4.2.2 i 6 \
1.3.6.1.4.1.2378.1.1.1.1.4.2.3 i 7 \
1.3.6.1.4.1.2378.1.1.1.1.4.3.2 i IINNTT \
1.3.6.1.4.1.2378.1.1.1.1.4.3.3 s HITME \
1.3.6.1.4.1.2378.1.1.1.1.4.3.4 i 5 \
1.3.6.1.4.1.2378.1.1.1.1.4.3.5 s "Some text" \
1.3.6.1.4.1.2378.1.1.1.1.4.2.4 s "timestamp"


When I run the script, against the template file, sed returns :
...snip...

1.3.6.1.4.1.2378.1.1.1.1.4.2.3 i 7 \
1.3.6.1.4.1.2378.1.1.1.1.4.3.2 i IINNTT \
\3.6.1.4.1.2378.1.1.1.1.4.3.3 s ATPC ALM
1.3.6.1.4.1.2378.1.1.1.1.4.3.4 i 5 \
...snip...

As you can see, it has made the replacement, but the trailing "\" disappears (or rather, moves to the start of the same output line).
To me it appears irrational, and I cannot figure out why it does.

Any ideas?

Cheers,
Simon.

Last edited by sstavdal; 07-01-2010 at 12:44 PM.
 
Old 07-01-2010, 09:45 AM   #2
rical
LQ Newbie
 
Registered: Jun 2010
Posts: 13

Rep: Reputation: 1
I'm finding it hard to figure out what you are trying to do. Maybe its because I'm not that into snmp.

Your code looks a bit inconsistent and its not the "standard way" of accessing and manipulating files.

Where did ATPC come from? and whats in the file tutticodes?

Post a snippet of all files with there names, the result you are getting and the result you want. Then I can hopefully help you.

Last edited by rical; 07-01-2010 at 09:48 AM.
 
Old 07-01-2010, 10:02 AM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,503

Rep: Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893Reputation: 1893
Are there any unusual characters in "tutticodes"?
Also the second sed seems to also make no sense as neither file, based on current examples, would have this word.

You might need to explain a little further?
 
Old 07-01-2010, 10:05 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
I too have trouble following this. For example in this statement:
sed -e 's/TERMINATE/\\/g'
Where does "TERMINATE" exist anywhere?

First, try the problem substitution by itself---without any of the other stuff---and show us the before and after
 
Old 07-01-2010, 12:44 PM   #5
sstavdal
LQ Newbie
 
Registered: Jul 2010
Posts: 7

Original Poster
Rep: Reputation: 0
Ok, I realise I put this together a bit too fast...

tuttifiles is my input file.
Containing text strings (consisting of words), for example, "JOHBURG SOUTH*AFRICA",*"OSLO*NORWAY",*"ATPC*ALM" etc.
(I realise I should have given ATPC ALM in the example too, otherwise, it wouldn“t make sense...)

The piped sed shouldn“t be there (it was just one of my many attempts of trying to work around the problem).
So, the script should read ONLY:
sed -e "s/HITME/${LINE}/g" template2

Running the script, it is supposed to read tutticodes, take each line as one input, and substitute HITME with its value.
The problem is the trailing backslash (which I am not trying to substitute, but rather leave for line continuation in the end result).

What I expect as a result is :
...snip...
1.3.6.1.4.1.2378.1.1.1.1.4.2.3 i 7 \
1.3.6.1.4.1.2378.1.1.1.1.4.3.2 i IINNTT \
1.3.6.1.4.1.2378.1.1.1.1.4.3.3 s ATPC ALM \
1.3.6.1.4.1.2378.1.1.1.1.4.3.4 i 5 \
...snip

but, there are two "problems" with the output, namely :
(This is the output that I get)
\3.6.1.4.1.2378.1.1.1.1.4.3.3 s ATPC ALM

The leading text is malformed (the 1. is missing, replaced by the very backslash that“s missing as the trailing one I had in my template file...

Hope that makes things a bit more clear, and apologies for not including enough info.
by copy :
to rical, never mind it“s SNMP, it is just text that happens to be an SNMP script.
to grail, there are no unusual characters in the tutticodes file, only words and spaces.

Cheers, and thanks for the response so far !
Simon.
 
Old 07-01-2010, 01:43 PM   #6
rical
LQ Newbie
 
Registered: Jun 2010
Posts: 13

Rep: Reputation: 1
Still don't make much sense to me.
Please give us example files like

input.txt
JOHBURG SOUTH
FOO BAR
BAR BAZ

script.sh
#!bin/bash
....

My result of command: ./script.sh input.txt
1.3.6.1.4.1.2378.1.1.1.1.4.2.3 i 7 \
1.3.6.1.4.1.2378.1.1.1.1.4.3.2 i IINNTT \
\1.3.6.1.4.1.2378.1.1.1.1.4.3.3 s ATPC ALM
1.3.6.1.4.1.2378.1.1.1.1.4.3.4 i 5 \

expected result
1.3.6.1.4.1.2378.1.1.1.1.4.2.3 i 7 \
1.3.6.1.4.1.2378.1.1.1.1.4.3.2 i IINNTT \
1.3.6.1.4.1.2378.1.1.1.1.4.3.3 s ATPC ALM \
1.3.6.1.4.1.2378.1.1.1.1.4.3.4 i 5 \


If you do this, it takes us 2 seconds to copy-paste it into files and try it ourself.

The fist post suggest the tuttifiles file is formated as:
JOHBURG SOUTH AFRICA
OSLO NORWAY

And you last post suggest:
SOUTH*AFRICA",*"OSLO*NORWAY",*"ATPC*ALM

Is this a regexp explanation of the files structure or is it the actual file? There is a huge difference.

I would love to help, but its still to unclear for me to easily follow what your trying to do, sorry.
 
Old 07-01-2010, 03:40 PM   #7
sstavdal
LQ Newbie
 
Registered: Jul 2010
Posts: 7

Original Poster
Rep: Reputation: 0
Ok,

Here we go :
########
script.sh
########
#!/bin/bash

N=0
cat tutticodes | while read LINE
do
N=$((N+1))
echo "Line $N = $LINE"
sed -e "s/HITME/${LINE}/g" template2 # Only writes to screen, will be redirected to file once working
done


########
tutticodes (input file) save as tutticodes
########
NEW YORK
OSLO NORWAY
ONE TWO THREE
SOME OTHER WORDS


########
template2 (template file) save as template2
########
/usr/bin/snmptrap -v 1 -c public 10.10.10.10 1.3.6.1.4.1.2378.1.1.1.1.4 10.10.10.11 0 2 ' ' \
1.3.6.1.4.1.2378.1.1.1.1.4.2.1 i 5 \
1.3.6.1.4.1.2378.1.1.1.1.4.2.2 i 6 \
1.3.6.1.4.1.2378.1.1.1.1.4.2.3 i 7 \
1.3.6.1.4.1.2378.1.1.1.1.4.3.2 i IINNTT \
1.3.6.1.4.1.2378.1.1.1.1.4.3.3 s HITME \
1.3.6.1.4.1.2378.1.1.1.1.4.3.4 i 5 \
1.3.6.1.4.1.2378.1.1.1.1.4.3.5 s "Some text" \
1.3.6.1.4.1.2378.1.1.1.1.4.2.4 s "timestamp"

########
My result
########
...snip...
1.3.6.1.4.1.2378.1.1.1.1.4.2.3 i 7 \
1.3.6.1.4.1.2378.1.1.1.1.4.3.2 i IINNTT \
\3.6.1.4.1.2378.1.1.1.1.4.3.3 s NEW YORK
1.3.6.1.4.1.2378.1.1.1.1.4.3.4 i 5 \
...snip...

########
Expected result
########
...snip...
1.3.6.1.4.1.2378.1.1.1.1.4.2.3 i 7 \
1.3.6.1.4.1.2378.1.1.1.1.4.3.2 i IINNTT \
1.3.6.1.4.1.2378.1.1.1.1.4.3.3 s NEW YORK \
1.3.6.1.4.1.2378.1.1.1.1.4.3.4 i 5 \
...snip...

Making these three files should make it reproducible
script.sh, tutticodes and template2
Place all in same directory, and execute script.sh

I believe (from memory) that sed is version 4.1.5

Cheers,
Simon.
 
Old 07-01-2010, 04:22 PM   #8
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Hi,

please use code tags when posting code. Makes it a lot easier to read.

I copy+pasted your samples and this is the output I got:
Code:
Line 1 = NEW YORK
/usr/bin/snmptrap -v 1 -c public 10.10.10.10 1.3.6.1.4.1.2378.1.1.1.1.4 10.10.10.11 0 2 ' ' \
1.3.6.1.4.1.2378.1.1.1.1.4.2.1 i 5 \
1.3.6.1.4.1.2378.1.1.1.1.4.2.2 i 6 \
1.3.6.1.4.1.2378.1.1.1.1.4.2.3 i 7 \
1.3.6.1.4.1.2378.1.1.1.1.4.3.2 i IINNTT \
1.3.6.1.4.1.2378.1.1.1.1.4.3.3 s NEW YORK \
1.3.6.1.4.1.2378.1.1.1.1.4.3.4 i 5 \
1.3.6.1.4.1.2378.1.1.1.1.4.3.5 s "Some text" \
1.3.6.1.4.1.2378.1.1.1.1.4.2.4 s "timestamp"
Line 2 = OSLO NORWAY
/usr/bin/snmptrap -v 1 -c public 10.10.10.10 1.3.6.1.4.1.2378.1.1.1.1.4 10.10.10.11 0 2 ' ' \
1.3.6.1.4.1.2378.1.1.1.1.4.2.1 i 5 \
1.3.6.1.4.1.2378.1.1.1.1.4.2.2 i 6 \
1.3.6.1.4.1.2378.1.1.1.1.4.2.3 i 7 \
1.3.6.1.4.1.2378.1.1.1.1.4.3.2 i IINNTT \
1.3.6.1.4.1.2378.1.1.1.1.4.3.3 s OSLO NORWAY \
1.3.6.1.4.1.2378.1.1.1.1.4.3.4 i 5 \
1.3.6.1.4.1.2378.1.1.1.1.4.3.5 s "Some text" \
1.3.6.1.4.1.2378.1.1.1.1.4.2.4 s "timestamp"
Line 3 = ONE TWO THREE
/usr/bin/snmptrap -v 1 -c public 10.10.10.10 1.3.6.1.4.1.2378.1.1.1.1.4 10.10.10.11 0 2 ' ' \
1.3.6.1.4.1.2378.1.1.1.1.4.2.1 i 5 \
1.3.6.1.4.1.2378.1.1.1.1.4.2.2 i 6 \
1.3.6.1.4.1.2378.1.1.1.1.4.2.3 i 7 \
1.3.6.1.4.1.2378.1.1.1.1.4.3.2 i IINNTT \
1.3.6.1.4.1.2378.1.1.1.1.4.3.3 s ONE TWO THREE \
1.3.6.1.4.1.2378.1.1.1.1.4.3.4 i 5 \
1.3.6.1.4.1.2378.1.1.1.1.4.3.5 s "Some text" \
1.3.6.1.4.1.2378.1.1.1.1.4.2.4 s "timestamp"
Line 4 = SOME OTHER WORDS
/usr/bin/snmptrap -v 1 -c public 10.10.10.10 1.3.6.1.4.1.2378.1.1.1.1.4 10.10.10.11 0 2 ' ' \
1.3.6.1.4.1.2378.1.1.1.1.4.2.1 i 5 \
1.3.6.1.4.1.2378.1.1.1.1.4.2.2 i 6 \
1.3.6.1.4.1.2378.1.1.1.1.4.2.3 i 7 \
1.3.6.1.4.1.2378.1.1.1.1.4.3.2 i IINNTT \
1.3.6.1.4.1.2378.1.1.1.1.4.3.3 s SOME OTHER WORDS \
1.3.6.1.4.1.2378.1.1.1.1.4.3.4 i 5 \
1.3.6.1.4.1.2378.1.1.1.1.4.3.5 s "Some text" \
1.3.6.1.4.1.2378.1.1.1.1.4.2.4 s "timestamp"
Looks like what you were expecting.
 
1 members found this post helpful.
Old 07-01-2010, 05:05 PM   #9
sstavdal
LQ Newbie
 
Registered: Jul 2010
Posts: 7

Original Poster
Rep: Reputation: 0
Hello,

ok, that confirms I'm still sane, but I get a different output. Can you please state your version of sed?

I am running exacly the same code, but output is malformed...

Cheers,
Simon.
 
Old 07-01-2010, 05:29 PM   #10
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Quote:
Originally Posted by sstavdal View Post
Hello,

ok, that confirms I'm still sane, but I get a different output. Can you please state your version of sed?

I am running exacly the same code, but output is malformed...

Cheers,
Simon.
GNU sed version 4.2.1. Do you get only malformed output on screen or also when you redirect it to a file?
 
Old 07-02-2010, 01:44 AM   #11
rical
LQ Newbie
 
Registered: Jun 2010
Posts: 13

Rep: Reputation: 1
I have tried this in GNU sed version 4.2.1 and 4.1.5 and I get the "correct" output:

Code:
Line 1 = NEW YORK
/usr/bin/snmptrap -v 1 -c public 10.10.10.10 1.3.6.1.4.1.2378.1.1.1.1.4
158.112.8.26 0 2 ' ' \
1.3.6.1.4.1.2378.1.1.1.1.4.2.1 i 5 \
1.3.6.1.4.1.2378.1.1.1.1.4.2.2 i 6 \
1.3.6.1.4.1.2378.1.1.1.1.4.2.3 i 7 \
1.3.6.1.4.1.2378.1.1.1.1.4.3.2 i IINNTT \
1.3.6.1.4.1.2378.1.1.1.1.4.3.3 s NEW YORK \
1.3.6.1.4.1.2378.1.1.1.1.4.3.4 i 5 \
1.3.6.1.4.1.2378.1.1.1.1.4.3.5 s "Some text" \
1.3.6.1.4.1.2378.1.1.1.1.4.2.4 s "timestamp"
Line 2 = OSLO NORWAY
/usr/bin/snmptrap -v 1 -c public 10.10.10.10 1.3.6.1.4.1.2378.1.1.1.1.4
158.112.8.26 0 2 ' ' \
1.3.6.1.4.1.2378.1.1.1.1.4.2.1 i 5 \
1.3.6.1.4.1.2378.1.1.1.1.4.2.2 i 6 \
1.3.6.1.4.1.2378.1.1.1.1.4.2.3 i 7 \
1.3.6.1.4.1.2378.1.1.1.1.4.3.2 i IINNTT \
1.3.6.1.4.1.2378.1.1.1.1.4.3.3 s OSLO NORWAY \
1.3.6.1.4.1.2378.1.1.1.1.4.3.4 i 5 \
1.3.6.1.4.1.2378.1.1.1.1.4.3.5 s "Some text" \
1.3.6.1.4.1.2378.1.1.1.1.4.2.4 s "timestamp"
Line 3 = ONE TWO THREE
/usr/bin/snmptrap -v 1 -c public 10.10.10.10 1.3.6.1.4.1.2378.1.1.1.1.4
158.112.8.26 0 2 ' ' \
1.3.6.1.4.1.2378.1.1.1.1.4.2.1 i 5 \
1.3.6.1.4.1.2378.1.1.1.1.4.2.2 i 6 \
1.3.6.1.4.1.2378.1.1.1.1.4.2.3 i 7 \
1.3.6.1.4.1.2378.1.1.1.1.4.3.2 i IINNTT \
1.3.6.1.4.1.2378.1.1.1.1.4.3.3 s ONE TWO THREE \
1.3.6.1.4.1.2378.1.1.1.1.4.3.4 i 5 \
1.3.6.1.4.1.2378.1.1.1.1.4.3.5 s "Some text" \
1.3.6.1.4.1.2378.1.1.1.1.4.2.4 s "timestamp"
Line 4 = SOME OTHER WORDS
/usr/bin/snmptrap -v 1 -c public 10.10.10.10 1.3.6.1.4.1.2378.1.1.1.1.4
158.112.8.26 0 2 ' ' \
1.3.6.1.4.1.2378.1.1.1.1.4.2.1 i 5 \
1.3.6.1.4.1.2378.1.1.1.1.4.2.2 i 6 \
1.3.6.1.4.1.2378.1.1.1.1.4.2.3 i 7 \
1.3.6.1.4.1.2378.1.1.1.1.4.3.2 i IINNTT \
1.3.6.1.4.1.2378.1.1.1.1.4.3.3 s SOME OTHER WORDS \
1.3.6.1.4.1.2378.1.1.1.1.4.3.4 i 5 \
1.3.6.1.4.1.2378.1.1.1.1.4.3.5 s "Some text" \
1.3.6.1.4.1.2378.1.1.1.1.4.2.4 s "timestamp"
What os and version of sed are you running?
 
Old 07-02-2010, 02:10 AM   #12
sstavdal
LQ Newbie
 
Registered: Jul 2010
Posts: 7

Original Poster
Rep: Reputation: 0
I found out what the problem was!
tutticodes came from a windows box, ran dos2unix on the file, and volią!
(I know vi interprets DOS coding (8bit) ending with "^M").
That must have confused sed.

Silly, but I couldn't see the forest for the trees.
Thanks for all the contributions

Simon
 
  


Reply

Tags
escape, sed


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
sed command to replace slash with backslash stelmed Slackware 13 08-19-2010 05:01 AM
sed including ± symbol Alkass Programming 4 04-22-2010 02:59 AM
sed backslash question DrStrangepork Linux - Newbie 1 09-29-2009 09:47 AM
How to type backslash and the vertical line in the terminal? WillingToLikeLinux Linux - Newbie 10 06-11-2009 12:24 PM
converting Dos link backslash to Unix like backslash barunparichha Linux - Software 1 05-14-2009 09:54 AM


All times are GMT -5. The time now is 06:46 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration