Splitting a line with pattern in Sed - Unix


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Splitting a line with pattern in Sed - Unix
# 1  
Old 09-23-2011
Splitting a line with pattern in Sed - Unix

I have a file with single line, that line contains just like the following sample
Code:
00200100293^30^1^bla bla ...._______To:	zabell00200100293^30^3^aSub00200100293^30^4^ellaCc:	Sanders,De on my desk__________00200100293^30^4^___________________________________00A00ABC0293^30^1^something___To:	some text00A00ABC0293^30^3^aSub00A00ABC0293^30^4^ellaCc:	__________00A00ABC0293^30^4^___________________________________

I want to spilt up in to multiple line based on 00200100293^, 00A00ABC0293^ and etc...

00200100293^,00A00ABC0293^ - These numbers could be any thing but alpha-numeric the length is always fixed 11 digits followed by '^'. Basically the output should like the following.
Code:
00200100293^30^1^bla bla ...._______To:	zabell
00200100293^30^3^aSub
00200100293^30^4^ellaCc:	Sanders,De on my desk__________
00200100293^30^4^___________________________________
00A00ABC0293^30^1^something___To:	some text
00A00ABC0293^30^3^aSub
00A00ABC0293^30^4^ellaCc:	__________
00A00ABC0293^30^4^___________________________________

I am trying this one

Code:
cat inputfile |sed "s/^.\{11\}^/~/^.\{11\}^/g" |tr -s "~" '\n' >output

but it does not give me anything which I wanted, Can any one know how to resolve this. Much appreciated

Thanks

Moderator's Comments:
Mod Comment Video tutorial on how to use code tags in The UNIX and Linux Forums.
# 2  
Old 09-23-2011
Code:
$ sed 's,00200100293,|&,g;s,00A00ABC0293,|&,g' infile | tr '|' '\n'

# 3  
Old 09-23-2011
Thanks, but the 00200100293^ and 00A0ABC0293^ are not the only values are in the file. it contains many different string but the length is always same with 11 digits followed by "^"
# 4  
Old 09-23-2011
Code:
 
$ nawk -F^ '{for(i=1;i<=NF;i++){if(length($i)>11){printf("^%s\n%s",substr($i,1,length($i)-10),substr($i,length($i)-10,length($i)))}else{printf("^%s",$i)}}}' inputfile
^00200100293^30^1^bla bla ...._______To: zabell0
00200100293^30^3^aSub0
00200100293^30^4^ellaCc: Sanders,De on my desk__________0
00200100293^30^4^___________________________________00
0A00ABC0293^30^1^something___To: some text00
0A00ABC0293^30^3^aSub00
0A00ABC0293^30^4^ellaCc: __________00
0A00ABC0293^30^4^_________________________

# 5  
Old 09-23-2011
Reposting the correct input:

input:



00200100293^30^1^bla bla ...._______To: zabell00200100293^30^3^aSub00200100293^30^4^ellaCc: Sanders,De on my desk__________00200100293^30^4^___________________________________00A0ABC0293^30^1^something___To: some text00A0ABC0293^30^3^aSub00A0ABC0293^30^4^ellaCc: __________00A0ABC0293^30^4^___________________________________

...
...
etc...



output:


00200100293^30^1^bla bla ...._______To: zabell
00200100293^30^3^aSub
00200100293^30^4^ellaCc: Sanders,De on my desk__________
00200100293^30^4^___________________________________
00A0ABC0293^30^1^something___To: some text
00A0ABC0293^30^3^aSub
00A0ABC0293^30^4^ellaCc: __________
00A0ABC0293^30^4^___________________________________

...
# 6  
Old 09-23-2011
Code:
 
$ nawk -F^ '{for(i=1;i<=NF;i++){if(length($i)>11){printf("^%s\n%s",substr($i,1,length($i)-12),substr($i,length($i)-10,length($i)))}else{printf("^%s",$i)}}}' infile 
^00200100293^30^1^bla bla ...._______To: zabel
00200100293^30^3^aSu
00200100293^30^4^ellaCc: Sanders,De on my desk_________
00200100293^30^4^___________________________________
0A00ABC0293^30^1^something___To: some text
0A00ABC0293^30^3^aSub
0A00ABC0293^30^4^ellaCc: __________
0A00ABC0293^30^4^_______________________

# 7  
Old 09-23-2011
Thanks a lot. It works fine. Thanks again.

---------- Post updated at 05:27 AM ---------- Previous update was at 05:20 AM ----------

Thanks Kamaraj,

One more help.

Instead of returning 8 single line.. is it possible to get 2 single line for the above example.. something like.


00200100293^30^1^bla bla ...._______To: zabel aSu ellaCc: Sanders,De on my desk____________________________________________
0A00ABC0293^30^1^something___To: some text aSub ellaCc: _________________________________


Just wondering..


Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern

'Hi I'm using the following code to extract the lines(and redirect them to a txt file) after the pattern match. But the output is inclusive of the line with pattern match. Which option is to be used to exclude the line containing the pattern? sed -n '/Conn.*User/,$p' > consumers.txt (11 Replies)
Discussion started by: essem
11 Replies

2. Shell Programming and Scripting

Sed. Delete line before and after pattern.

Hi. In need to delete line before and after pattern in file. I came to following commands. Delete line before sed -ni '/pattern/{x;d;};1h;1!{x;p;};${x;p;}' /etc/testfile Delete line after sed -i '/pattern/{N;s/\n.*//;}' /etc/testfile Is it possible to merge it in single command? (3 Replies)
Discussion started by: urello
3 Replies

3. Shell Programming and Scripting

Splitting textfile based on pattern and name new file after pattern

Hi there, I am pretty new to those things, so I couldn't figure out how to solve this, and if it is actually that easy. just found that awk could help:(. so i have a textfile with strings and numbers (originally copy pasted from word, therefore some empty cells) in the following structure: SC... (9 Replies)
Discussion started by: luja
9 Replies

4. Shell Programming and Scripting

sed command to grep multiple pattern present in single line and delete that line

here is what i want to achieve.. i have a file with below contents cat fileName blah blah blah . .DROP this REJECT that . --sport 7800 -j REJECT --reject-with icmp-port-unreachable --dport 7800 -j REJECT --reject-with icmp-port-unreachable . . . more blah blah blah --dport 3306... (14 Replies)
Discussion started by: vivek d r
14 Replies

5. Shell Programming and Scripting

I need to know how to replace a line after a pattern match with an empty line using SED

Hi How Are you? I am doing fine! I need to go now? I will see you tomorrow! Basically I need to replace the entire line containing "doing" with a blank line: I need to the following output: Hi How Are you? I need to go now? I will see you tomorrow! Thanks in advance.... (1 Reply)
Discussion started by: sags007_99
1 Replies

6. Shell Programming and Scripting

Splitting large file into multiple files in unix based on pattern

I need to write a shell script for below scenario My input file has data in format: qwerty0101TWE 12345 01022005 01022005 datainala alanfernanded 26 qwerty0101mXZ 12349 01022005 06022008 datainalb johngalilo 28 qwerty0101TWE 12342 01022005 07022009 datainalc hitalbert 43 qwerty0101CFG 12345... (19 Replies)
Discussion started by: jimmy12
19 Replies

7. Shell Programming and Scripting

Need help in sed command [ printing a pattern + its line no or line no alone ]

Hello friends, Only very recently i started learning sed command...an i found that sed is faster in finding the patterns than some of my scripts that uses grep to check the patten inside a file using line by line search method which is time consuming. The below script... (4 Replies)
Discussion started by: frozensmilz
4 Replies

8. Shell Programming and Scripting

sed: Find start of pattern and extract text to end of line, including the pattern

This is my first post, please be nice. I have tried to google and read different tutorials. The task at hand is: Input file input.txt (example) abc123defhij-E-1234jslo 456ujs-W-abXjklp From this file the task is to grep the -E- and -W- strings that are unique and write a new file... (5 Replies)
Discussion started by: TestTomas
5 Replies

9. Shell Programming and Scripting

removing a line containing a pattern in sed

i need to use sed to remove an entire line containing a pattern stored in a variable say $var1 this var1 will be a URL and will therefore contain slashes any help would be greatly appreciated (1 Reply)
Discussion started by: Fire_Storm
1 Replies

10. Shell Programming and Scripting

sed - Replace Line which contains the Pattern match with a new line

I need to replace the line containing "STAGE_DB" with the line "STAGE_DB $DB # database that contains the table being loaded ($workingDB)" Here $DB is passed during the runtime. How can I do this? Thanks, Kousikan (2 Replies)
Discussion started by: kousikan
2 Replies
Login or Register to Ask a Question