Sed pattern space/looping conundrum


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed pattern space/looping conundrum
# 1  
Old 05-10-2011
Sed pattern space/looping conundrum

Although my sed skills are gradually developing, thanks in large part to this forum, I'm having a hard time dealing with pattern space and looping, which I suspect is what I'll need a better handle on to figure out my current issue, which is converting a multi line file like this:

12345678|ONE%TWO%THREE

into

12345678|ONE
12345678|TWO
12345678|THREE

The % delimited items to the right of the pipe is an unlimited range of elements. The left of the pipe will always contain an 8 digit number. My thought was to perhaps make two passes (maybe creating an intermediate file) that first just substituted the % delimiters with a pipe and carriage return, e.g.

Code:
 
s/%/\
|/g

Which would give me this...

12345678|ONE
|TWO
|THREE

Then on the next pass somehow grab just the number in the pattern space and prepend it to subsequent lines that do not begin with a number (the % delimited elements will always be alpha characters.)

And that's where I'm stuck. Any snippets or even just strategy suggestions would be greatly appreciated. Although this appears to be pretty low hanging fruit for the likes of perl, I'd like to hang with sed to the extent that it's possible... for the challenge if nothing else :-).

Thanks in advance,
Al
# 2  
Old 05-10-2011
How about:
Code:
$ cat data
12345678|ONE%TWO%THREE
$ cat sedder
#! /usr/bin/ksh
exec < data
sed ':l;s/\([0-9][0-9]*\)\(.*\)\(%\)\(.*\)/\1\2\n\1|\4/;t l'
$
$ ./sedder
12345678|ONE
12345678|TWO
12345678|THREE
$

My sed lets me do \n for a newline so I went with that.
This User Gave Thanks to Perderabo For This Post:
# 3  
Old 05-10-2011
Another approach, but with awk: Smilie
Code:
awk -F\| '{gsub("%","\n"$1"|")}1' file

This User Gave Thanks to Franklin52 For This Post:
# 4  
Old 05-10-2011
an awk version:

Code:
 
echo "12345678|ONE%TWO%THREE" | awk -F "[|%]" '{for(i=2;i<=NF;i++) { print $1,$i}}' OFS="|"

This User Gave Thanks to panyam For This Post:
# 5  
Old 05-10-2011
Thanks for the great responses guys - looking at various solutions is a great learning tool. I was further off from a solution than I thought :-)
Al
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Gawk --- produce the output in pattern space instead of END space

hi, I'm trying to calculate IP addresses and their respective calls to our apache Server. The standard format of the input is HOST IP DATE/TIME - - "GET/POST reuest" "User Agent" HOST IP DATE/TIME - - "GET/POST reuest" "User Agent" HOST IP DATE/TIME - - "GET/POST reuest" "User Agent" HOST... (2 Replies)
Discussion started by: busyboy
2 Replies

2. Shell Programming and Scripting

awk pattern match by looping through search patterns

Hi I am using Solaris 5.10 & ksh Wanted to loop through a pattern file by reading it and passing it to the awk to match that value present in column 1 of rawdata.txt , if so print column 1 & 2 in to Avlblpatterns.txt. Using the following code but it seems some mistakes and it is running for... (2 Replies)
Discussion started by: ananan
2 Replies

3. UNIX for Beginners Questions & Answers

Looping and sed

Hi, I've got text files (say file1 and file2) in a directory (say directory1) with several columns like this: a b c d a b c de f g h e f g hI need to add a header to the files in directory1 and the names of the columns must contain an identifier from another text file (say file3) like this:... (4 Replies)
Discussion started by: zajtat
4 Replies

4. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

5. Shell Programming and Scripting

sed - remove space and . for a pattern

Hi, I have file which contains following lines A| 965.|Mr.|35.45| 66. B| 33.| a456.| 77. The output should be A|965|Mr.|35.45|66 B|33| a456.|77 Basically if a Number has space in prefix and . in suffix remove both. So pattern could be if there is a | which has next two characters as... (1 Reply)
Discussion started by: wahi80
1 Replies

6. Shell Programming and Scripting

sed pattern and hold space issues

Good day. Trying to make a sed script to take text file in a certain format and turn it into mostly formatted html. I'm 95% there but this last bit is hurting my head finally. Here's a portion of the text- Budgeting and Debt: Consumer Credit Counseling of Western PA CareerLink 112... (5 Replies)
Discussion started by: fiendracer
5 Replies

7. Shell Programming and Scripting

help - sed - insert space between string of form XxxAxxBcx, without replacing the pattern

If the string is of the pattern XxxXyzAbc... The expected out put from sed has to be Xxx Xyz Abc ... eg: if the string is QcfEfQfs, then the expected output is Qcf Ef Efs. If i try to substitute the pattern with space then the sed will replace the character or pattern with space,... (1 Reply)
Discussion started by: frozensmilz
1 Replies

8. Shell Programming and Scripting

Duplicate pattern space (sed)

Hi, I'm new to sed and i'm having a few difficulties.. I need to append the current line to the pattern space, which already contains that same line, e.g.: current line : test pattern space : test|test I was able to do this using the hold space, but the problem is that in the next step of... (2 Replies)
Discussion started by: delucasvb
2 Replies

9. UNIX for Dummies Questions & Answers

blank space in regex pattern using sed

why does sed 's/.* //' show the last word in a line and sed 's/ .*//' show the first word in a line? How is that blank space before or after the ".*" being interpreted in the regex? i would think the first example would delete the first word and the next example would delete the second... (1 Reply)
Discussion started by: glev2005
1 Replies

10. Shell Programming and Scripting

Looping and using Sed

Hi guys I having som problem trying to use sed to get a file and insert inside another one. I'll try to explain better. I have a base.txt and using sed(having a array variables) I'm chaging some strings inside this file and saving as base1.txt. until here okay. Then, I have to get this... (4 Replies)
Discussion started by: digobh
4 Replies
Login or Register to Ask a Question