Visit Our UNIX and Linux User Community


Segregate file content using sed backreference


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Segregate file content using sed backreference
# 1  
Old 01-15-2016
Wrench Segregate file content using sed backreference

I have some text like
Code:
EU1BTDAT:ASSGNDD filename='$SEQFILES/SUNIA.PJ008202.CARDLIB/DATECARD'
EU1BTDATEST:ASSGNDD filename='$SEQFILES/SUNIA.PJ008202.CARDLIB/DATECARD'
EU1CLOSEDATES:ASSGNDD filename='$SEQFILES/SUNIA.PJ008202.CARDLIB/DATECARD'
EU1DATED:ASSGNDD filename='$SEQFILES/SUNIA.PJ008202.CARDLIB/DATECARD'
EU1DATED.022415:ASSGNDD filename='$SEQFILES/SUNIA.PJ008202.CARDLIB/DATECARD'
EU1DATEM:ASSGNDD filename='$SEQFILES/SUNIA.PJ008202.CARDLIB/DATECARDM'
EU1DATENYSE:ASSGNDD filename='$SEQFILES/SUNIA.PJ008202.CARDLIB/DATECARD'
EU1DATEQTRLY:ASSGNDD filename='$SEQFILES/SUNIA.PJ008202.CARDLIB/DATECARDQ'
EU1DATEQTRLY:ASSGNDD filename='$SEQFILES/SUNIA.PJ008202.CARDLIB/DATECARD'
EU1DATEQTRLY.010416:ASSGNDD filename='$SEQFILES/SUNIA.PJ008202.CARDLIB/DATECARDM'
EU1DATEQTRLY.010416:ASSGNDD filename='$SEQFILES/SUNIA.PJ008202.CARDLIB/DATECARD'
EU1DATEQTRLY.orig:ASSGNDD filename='$SEQFILES/SUNIA.PJ008202.CARDLIB/DATECARDM'

i want to fetch the 1st word before ":" and the last word after "=".
I believe it can be used using sed back reference.

I have tried using
Code:
sed -e "s/([a-z]*)(:)(*)(=)(*)/\1 \4/g" backreftext

but it does not work
also i have tried by adding escape characters.. but veil.
if anyone can help and suggest where i am going wrong.

The output expected as :
Code:
EU1BTDAT $SEQFILES/SUNIA.PJ008202.CARDLIB/DATECARD
EU1BTDATEST $SEQFILES/SUNIA.PJ008202.CARDLIB/DATECARD
EU1DATEQTRLY $SEQFILES/SUNIA.PJ008202.CARDLIB/DATECARDQ


Last edited by gotamp; 01-15-2016 at 04:13 AM.. Reason: formatting in proper way
# 2  
Old 01-15-2016
Try it (GNU sed):
Code:
sed -E 's/(^[^:]+):[^=]+=(.*)/\1 \2/'

Your expression is a mess.
# 3  
Old 01-15-2016
Hello gotamp,

Please use code tags as per forum rules for your Inupts/codes shown in your posts not HTML codes. You haven't shown the sample output to us, so based on my assumptions, could you please try following and let me know if this helps you.
Code:
awk -vs1="'" -F"=" '{sub(/:.*=/,"=",$0);n=split($2, A,".");sub(s1,X,A[n]);print $1 OFS A[n]}'  Input_file

Output will be as follows.
Code:
EU1BTDAT CARDLIB/DATECARD
EU1BTDATEST CARDLIB/DATECARD
EU1CLOSEDATES CARDLIB/DATECARD
EU1DATED CARDLIB/DATECARD
EU1DATED.022415 CARDLIB/DATECARD
EU1DATEM CARDLIB/DATECARDM
EU1DATENYSE CARDLIB/DATECARD
EU1DATEQTRLY CARDLIB/DATECARDQ
EU1DATEQTRLY CARDLIB/DATECARD
EU1DATEQTRLY.010416 CARDLIB/DATECARDM
EU1DATEQTRLY.010416 CARDLIB/DATECARD
EU1DATEQTRLY.orig CARDLIB/DATECARDM

I you do have any other conditions or differenet output then please show us with
sample output and with all conditions too.
EDIT: Seems you have shown your expected output now by editing your post, then following may help you in same.
Code:
awk -vs1="'" '{sub(/:.*=/,OFS,$0);gsub(s1,X,$0);print}'   Input_file

Output will be as follows.
Code:
EU1BTDAT $SEQFILES/SUNIA.PJ008202.CARDLIB/DATECARD
EU1BTDATEST $SEQFILES/SUNIA.PJ008202.CARDLIB/DATECARD
EU1CLOSEDATES $SEQFILES/SUNIA.PJ008202.CARDLIB/DATECARD
EU1DATED $SEQFILES/SUNIA.PJ008202.CARDLIB/DATECARD
EU1DATED.022415 $SEQFILES/SUNIA.PJ008202.CARDLIB/DATECARD
EU1DATEM $SEQFILES/SUNIA.PJ008202.CARDLIB/DATECARDM
EU1DATENYSE $SEQFILES/SUNIA.PJ008202.CARDLIB/DATECARD
EU1DATEQTRLY $SEQFILES/SUNIA.PJ008202.CARDLIB/DATECARDQ
EU1DATEQTRLY $SEQFILES/SUNIA.PJ008202.CARDLIB/DATECARD
EU1DATEQTRLY.010416 $SEQFILES/SUNIA.PJ008202.CARDLIB/DATECARDM
EU1DATEQTRLY.010416 $SEQFILES/SUNIA.PJ008202.CARDLIB/DATECARD
EU1DATEQTRLY.orig $SEQFILES/SUNIA.PJ008202.CARDLIB/DATECARDM

Thanks,
R. Singh

Last edited by RavinderSingh13; 01-15-2016 at 04:11 AM.. Reason: Adding one more solution as user has shown expected output now in user's post.
# 4  
Old 01-15-2016
Extremely sorry for the inconvenience caused as the expression was a mess due to improper code tags.
I have corrected them now.

@ yazu..
the above code did not work . it says
Code:
sed -e 's/(^[^:]+):[^=]+=(.*)/\1 \2/' backreftext
sed: command garbled: s/(^[^:]+):[^=]+=(.*)/\1 \2/

another information : i am using sun solaris system.
Code:
uname -a
SunOS simsun2 5.10 Generic_150400-13 sun4u sparc SUNW,SPARC-Enterprise

@RavinderSingh13
the given awk code did not work
Code:
awk -vs1="'" -F"=" '{sub(/:.*=/,"=",$0);n=split($2, A,".");sub(s1,X,A[n]);print $1 OFS A[n]}' backreftext
awk: syntax error near line 1
awk: illegal statement near line 1
awk: syntax error near line 1
awk: illegal statement near line 1

May be i am missing something, as i have directly copied and pasted the text.
# 5  
Old 01-15-2016
GNU sed with "-E" (or -r - they are the same) option

For solaris this should work

Code:
sed  's!^\([^:]*\):[^=]*=\(.*\)!\1 \2!'

This User Gave Thanks to yazu For This Post:
# 6  
Old 01-15-2016
many thanks Yazu. it worked now.
however, I am still struggling to understand the logic.
I am sure i will understand, but it will take little time.
I would appreciate if you can point out any URL / Bookname to learn back reference.
Many thanks once again !!!
# 7  
Old 01-15-2016
I learnt sed here (but I read something before - this is good.)
This User Gave Thanks to yazu For This Post:

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to segregate a section from big file?

Hello, I need to know all IP range (ip_prefix), associated with us-west-2 region only from this link - https://ip-ranges.amazonaws.com/ip-ranges.json (it can be opened in wordpad for better visibility) Please suggest, how would I do it. If vi, awk or sed is needed, I have downloaded it on my... (7 Replies)
Discussion started by: solaris_1977
7 Replies

2. Shell Programming and Scripting

Help with sed backreference please

Hi, I'm using /bin/sh I would appreciate if someone could help me with SED syntax for a "simple" line. Here is where I Got to: I have these strings that are returned by my(Examples) (naturally "FullPath" is always changing don't hardcode this lol) FullPath/AAA.framework... (3 Replies)
Discussion started by: Herrick
3 Replies

3. Shell Programming and Scripting

Segregate by suffixed file names using Korn Shell

I have following files at /dir1 a.csv.20131201 b.csv.20131201 c.csv.20131201 d.csv.20131201 a.csv.20131202 b.csv.20131202 c.csv.20131202 d.csv.20131202 ....................... ....................... ....................... ....................... I need to move these files to... (4 Replies)
Discussion started by: JaisonJ
4 Replies

4. Shell Programming and Scripting

Regex and backreference to replace in binary file

Hello to all, I have this sed script that replaces hex strins within a binary file. As you can see, I want to replace all bytes 4X with 2X (where X could take values 0 to F). sed -e 's/\x40/\x20/g' -e 's/\x41/\x21/g' -e 's/\x42/\x22/g' -e 's/\x43/\x23/g' -e 's/\x44/\x24/g' -e... (7 Replies)
Discussion started by: Ophiuchus
7 Replies

5. Shell Programming and Scripting

Sed: replace content from file with the content from file

Hi, I am having trouble while using 'sed' with reading files. Please help. I have 3 files. File A, file B and file C. I want to find content of file B in file A and replace it by content in file C. Thanks a lot!! Here is a sample of my question. e.g. (file A: a.txt; file B: b.txt; file... (3 Replies)
Discussion started by: dirkaulo
3 Replies

6. Shell Programming and Scripting

sed regex backreference replacement

Hello, I want to rename multiple files and catch some points about backreference within sed and regex. Here is a part of my file list. Input: S92A.fa S92B.fa ... S96Z.fa S921.fa S922.fa ... S997.fa Note: The file names are not necessarily continuous from A~Z or 921 ~ 997, as some of the... (3 Replies)
Discussion started by: yifangt
3 Replies

7. Shell Programming and Scripting

Unix script to segregate dynamic and static content of a web application

I need to deploy a JAVA application on two separate servers: 1. Web server (IBM HTTP Web Servers (IHS)) 2. Application Server (WebSphere Application Server WAS7.0) The static content will have to be deployed and handled on Web server. These would include GIFs, HTML, CSS, etc files.... (0 Replies)
Discussion started by: chani27
0 Replies

8. Shell Programming and Scripting

SED to replace file content

Hi, I want to replace _F* by _F in a xml file. what is the sed command. I have tried sed "s/_F$/_F/g" or sed "s/_F*/_F/g" , but it does not work. thx file content <TAG>KC_FOU</TAG> <TAG>KC_FABC</TAG> <TAG>KC_FABCDG</TAG> desire output <TAG>KC_F</TAG> <TAG>KC_F</TAG> <TAG>KC_F</TAG> (6 Replies)
Discussion started by: godfreyyip
6 Replies

9. Shell Programming and Scripting

segregate the file based on matching patterns

print 'test' SETUSER 'dbo' go create proc abc as /Some code here/ go SETUSER go print 'test1' SETUSER 'dbo' go Create Procedure xyz as /some code here/ go SETUSER go print 'test2' SETUSER 'dbo' (2 Replies)
Discussion started by: mad_man12
2 Replies

10. Shell Programming and Scripting

egrep vs. sed backreference

My egrep outputs this: $ cat html.out|sed -n '/bluetext/s/&nbsp;/ /gp'|egrep '{5}' <span class="bluetext"><b> Lexington Park, MD 20653</b></span> But my backreference \1 is empty. I dont understand why. Can someone clarify? $ cat html.out|sed -n '/bluetext/s/&nbsp;/ /gp'|sed -n... (1 Reply)
Discussion started by: r0sc0
1 Replies

Featured Tech Videos