using a another delimiter with sed?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting using a another delimiter with sed?
# 1  
Old 04-02-2011
using a another delimiter with sed?

Hi there,

After lots of reading I figured out how to use sed to parse my file.
This file is called services.txt:

Code:
00a1:ffff0000:0018:01f4:1:477   
BravaNL                         
00a2:ffff0000:0018:01f4:1:471   
MAX                             
00a3:ffff0000:000b:01f4:1:390   
HaberTürk
00a6:ffff0000:0019:01f4:25:203  
Veronica/Disney XD

I have a text-file with on each line a service-name in it, called names.txt:
Code:
Max
BravaNL
Veronica/Disney XD

I want to use a bash script that uses a while loop to read each line from names.txt and then print out the serivice-id from the services.txt file.
The service-id is the line -before- the name in services.txt.
so for example:
The service-id of "BravaNL" would be 00a1:ffff0000:0018:01f4:1:477

After lots of searching on this board, I almost got it right using this script:

Code:
while read line
do
    result=`sed -n -e "/^$line$/{x;p;q;};h" services.txt`
    if [ "$result" = "" ]
    then
        echo "Not found"
        echo ""
    else
        echo "$result"
     fi
done <names.txt

I have the following: sed -n -e "/^$line$/{x;p;q;};h"
Where $line would be the service-name from names.txt.

The following rules apply:
Sed must print the line before the search-result: done
Sed must only find complete matches for the service-names: done
If sed find multiple matches it should only process the first: done
Sed should be able to handle service-names with a slash "/" in it: FAIL

It works for all names except for the ones with a / in them:
Code:
00a6:ffff0000:0019:01f4:25:203  
Veronica/Disney XD

I cannot put a break "\" in the $line.
So I would like to use another delimiter and make sed ignore the slashes.
Is this possible?

Last edited by rbatte1; 01-13-2017 at 10:12 AM.. Reason: Added ICODE tags
# 2  
Old 04-02-2011
Yes it's possible. I often use use equal signs because path names contain slashes.
See: https://www.unix.com/unix-dummies-que...variables.html
This User Gave Thanks to Perderabo For This Post:
# 3  
Old 04-02-2011
possible

Code:
result=`sed -n -e "!^$line$!{x;p;q;};h" services.txt`

regards,
Ahamed

Last edited by Franklin52; 04-02-2011 at 02:59 PM.. Reason: Please use code tags, thank you
# 4  
Old 04-02-2011
Well I tried to use another delimiter but it seems to be unsupported.
The / works:
Code:
sed -n -e "/^Nederland 1$/{x;p;q;};h" services 
1f44:ffff0000:0008:01f4:1:331

However using ! will not:
Code:
sed -n -e "!^Nederland 1$!{x;p;q;};h" services
sed: Unsupported command ^

The sed program comes with busybox, maybe it differs from gnu-sed in a way that it doesn't support other delimiters?
# 5  
Old 04-02-2011
Quote:
Originally Posted by ahamed101
Code:
result=`sed -n -e "!^$line$!{x;p;q;};h" services.txt`


Quote:
Originally Posted by MastaG
Well I tried to use another delimiter but it seems to be unsupported.
The / works:
Code:
sed -n -e "/^Nederland 1$/{x;p;q;};h" services 
1f44:ffff0000:0008:01f4:1:331

However using ! will not:
Code:
sed -n -e "!^Nederland 1$!{x;p;q;};h" services
sed: Unsupported command ^

The sed program comes with busybox, maybe it differs from gnu-sed in a way that it doesn't support other delimiters?

That's not how alternative delimiters work for addresses. You must precede the opening delimiter with a backslash.

Code:
sed -n '\!reg_ex_here!p'

As opposed to its usage in a substitution command which does not use the preceding backslash at the beginning:
Code:
sed -n 's!reg_ex_here!&!p'

Also, you may want to keep in mind that the exclamation point introduces history expansion in some shells, even when in double quotes.

Regards,
Alister

Last edited by alister; 04-02-2011 at 10:57 PM..
This User Gave Thanks to alister For This Post:
# 6  
Old 04-03-2011
Thanks a million!
Well since many service names contain a ! % or @ the best delimiter would be =.
Code:
sed -n -e "\=^Veronica/Disney XD$={x;p;q;};h" services
00a6:ffff0000:0019:01f4:25:203

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

AIX sed use space as delimiter

I am trying to do this with one small tweak. I would also like to use a space as a delimiter. sed 's/ */\ /g' file This is what my file looks like. server1, server2, server3 server4 server5 server6 I would like it to look like this. server1 server2 server3 server4 ... (6 Replies)
Discussion started by: cokedude
6 Replies

2. UNIX for Beginners Questions & Answers

How to identify delimiter to find and replace a string with sed?

I need to find and replace a date format in a SQL script with sed. The original lines are like this: ep.begin_date, ep.end_date, ep.facility_code, AND ep.begin_date <= '01-JUL-2019' ep.begin_date, ep.end_date, ep.facility_code, AND ... (15 Replies)
Discussion started by: duke0001
15 Replies

3. Shell Programming and Scripting

Join the line on delimiter using sed/awk in UNIX

I've input as , abcd| ef 123456| 78| 90 Desired output as, abcdef 1234567890 Anyone please give the solution. (5 Replies)
Discussion started by: jinixvimal
5 Replies

4. Shell Programming and Scripting

Perl Code to change file delimiter (passed as argument) to bar delimiter

Hi, Extremely new to Perl scripting, but need a quick fix without using TEXT::CSV I need to read in a file, pass any delimiter as an argument, and convert it to bar delimited on the output. In addition, enclose fields within double quotes in case of any embedded delimiters. Any help would... (2 Replies)
Discussion started by: JPB1977
2 Replies

5. Shell Programming and Scripting

sed delimiter error

Hi, I'm having a bit of trouble replacing sed's delimiter from a slash to a pipe. This works... sed '/INSERT INTO/s/\${TD_LOAD_DB}/NUC_PL_LOAD/g' sql_file.sql But this doesn't sed "|INSERT INTO|s|\${TD_LOAD_DB}|NUC_PL_LOAD|g" sql_file.sql (2 Replies)
Discussion started by: c19h28O2
2 Replies

6. Shell Programming and Scripting

sed delimiter

HI all,, I hve defined something like set data = /data/2012/text while using sed 's/$data//g' I am getting error as:I understand this is due to / slash,but is there any way out of it.... sed: -e expression #1, char 12: unknown option to `s' Thakns in adv. (6 Replies)
Discussion started by: Indra2011
6 Replies

7. Shell Programming and Scripting

Help with sed to add delimiter to send HEX with netcat

Hello, I want to send tcpflow dump to a TCP port in HEX data, to send with netcat i need to convert to HEX and add \\x before each HEX bytes, to do this i use this line: tcpflow -i -C dst port | xxd -p | sed 's/../&\\\\x/g;s/ $//' | nc the output on the listening end:... (3 Replies)
Discussion started by: patx
3 Replies

8. Shell Programming and Scripting

Implement in one line sed or awk having no delimiter and file size is huge

I have file which contains around 5000 lines. The lines are fixed legth but having no delimiter.Each line line contains nearly 3000 characters. I want to delete the lines a> if it starts with 1 and if 576th postion is a digit i,e 0-9 or b> if it starts with 0 or 9(i,e header and footer) ... (4 Replies)
Discussion started by: millan
4 Replies

9. Shell Programming and Scripting

help with sed to add delimiter and new field to each row

I have a file with millions of rows that I need to add a delimiter and a new field with a zero to the end of each row. (its too big to open and do a find and replace regex) I'm looking for the next line '\n' and need to replace it with a Unit Separator (hex \037) 0 \n. I've tried the... (2 Replies)
Discussion started by: kmac
2 Replies

10. Shell Programming and Scripting

awk,nawk,sed, delimiter |~|

RECORD=NEW|~|VENDORN=LUCENT|~|VENDORM=CBX500_REAR|~|NETWORK=ATM|~|SUBNETWORK=N/A|~|SITE=CIL|~|REGION=KN|~|COUNTRY=PS|~|SWITCH=SWITCH1|~|E THERNET=N/A|~|LOOPBACK=N/A|~|SHELF=N/A|~|SLOT=14|~|SUBSLOT=N/A|~|STSCHAN=N/A|~|PORT=S14|~|DS1SLOT=N/A|~|LINE=N/A|~|LPORTID=N/A|~|CARDDESC=N/A|~|CARDTYPE=BAC2RT0... (7 Replies)
Discussion started by: knijjar
7 Replies
Login or Register to Ask a Question