Sponsored Content
Top Forums Shell Programming and Scripting using a another delimiter with sed? Post 302510230 by MastaG on Saturday 2nd of April 2011 07:29:41 AM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
unix2dos(1)						      General Commands Manual						       unix2dos(1)

NAME
unix2dos - UNIX to DOS text file format converter SYNOPSYS
unix2dos [options] [-c convmode] [-o file ...] [-n infile outfile ...] Options: [-hkqV] [--help] [--keepdate] [--quiet] [--version] DESCRIPTION
This manual page documents unix2dos, the program that converts text files in UNIX format to DOS format. OPTIONS
The following options are available: -h --help Print online help. -k --keepdate Keep the date stamp of output file same as input file. -q --quiet Quiet mode. Suppress all warning and messages. -V --version Prints version information. -c --convmode convmode Sets conversion mode. Simulates unix2dos under SunOS. -o --oldfile file ... Old file mode. Convert the file and write output to it. The program default to run in this mode. Wildcard names may be used. -n --newfile infile outfile ... New file mode. Convert the infile and write output to outfile. File names must be given in pairs and wildcard names should NOT be used or you WILL lost your files. EXAMPLES
Get input from stdin and write output to stdout. unix2dos Convert and replace a.txt. Convert and replace b.txt. unix2dos a.txt b.txt unix2dos -o a.txt b.txt Convert and replace a.txt in ASCII conversion mode. Convert and replace b.txt in ISO conversion mode. unix2dos a.txt -c iso b.txt unix2dos -c ascii a.txt -c iso b.txt Convert and replace a.txt while keeping original date stamp. unix2dos -k a.txt unix2dos -k -o a.txt Convert a.txt and write to e.txt. unix2dos -n a.txt e.txt Convert a.txt and write to e.txt, keep date stamp of e.txt same as a.txt. unix2dos -k -n a.txt e.txt Convert and replace a.txt. Convert b.txt and write to e.txt. unix2dos a.txt -n b.txt e.txt unix2dos -o a.txt -n b.txt e.txt Convert c.txt and write to e.txt. Convert and replace a.txt. Convert and replace b.txt. Convert d.txt and write to f.txt. unix2dos -n c.txt e.txt -o a.txt b.txt -n d.txt f.txt DIAGNOSTICS
BUGS
The program does not work properly under MSDOS in stdio processing mode. If you know why is that so, please tell me. AUTHOR
Benjamin Lin - ( blin@socs.uts.edu.au ) MISCELLANY
Tested environment: Linux 1.2.0 with GNU C 2.5.8 SunOS 4.1.3 with GNU C 2.6.3 MS-DOS 6.20 with Borland C++ 4.02 Suggestions and bug reports are welcome. SEE ALSO
dos2unix(1) 1995.03.31 unix2dos v2.2 unix2dos(1)
All times are GMT -4. The time now is 01:20 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy