sed help with underscore problems


 
Thread Tools Search this Thread
Operating Systems HP-UX sed help with underscore problems
# 1  
Old 03-04-2014
sed help with underscore problems

Hello,

I have spent a couple of hours trying to answer this myself, so forgive me if the answer is simple but I have tried.

I have a text file generated from svn log output which contains a list of files.

Two regexps im using are
Code:
[a-zA-Z0-9]*

and
Code:
[a-zA-Z0-9_]*

They both work but some lines has a mixture of both formats. I have tried sed -e and other patterns, some produce the same results (simpler patterns) and some dont at all. This is to be expected.

Problem is the two example patterns are like this
Code:
a12_fdgdfg/proga
a12/progb
a11_dsfsdf/progc
a11/progd

I need to extract out from a text list as above the following
Code:
proga
progb
progc
progd

Im looking for one regexp that would do both in the context of sed

Im sure this must be simple enough for some one who ise familair with regexp, but I just started today and need some help.

Many thanks in advance

Last edited by Franklin52; 03-04-2014 at 09:26 AM.. Reason: Please use code tags
# 2  
Old 03-04-2014
Hello,

It's a request please use the code tags while posting commands and code as per forum rules. Following may help you in same.

Code:
awk 'gsub(/.*\/+/,X) 1' file_name

Output will be as follows.

Code:
proga
progb
progc
progd


EDIT: One more solution for same with sed.

Code:
 sed 's/\(.*\/\)\(.*\)/\2/g'  file_name


Output will be as follows.

Code:
proga
progb
progc
progd


EDIT: one more solution by basename.

Code:
while read line
do
basename $line
done < "file_name"


Thanks,
R. Singh

Last edited by RavinderSingh13; 03-04-2014 at 09:50 AM.. Reason: Adding a sed solution to same.
# 3  
Old 03-04-2014
Many thanks for the reply. I will use code tags in future.
# 4  
Old 03-04-2014
Code:
awk -F/ '{print $NF}' file

or
Code:
sed 's/.*\///' file

# 5  
Old 03-04-2014
Actually based on your reply using the () and no 2 etc, I think I should have used a fuller example. My mistake, I had thought the answer to my simplified question would have been enough.

I have a script

Code:
Pattern='   [A-Z] \/Group\/Subgroup\/[.]*\/'

# $interim is generated by a special svn log command 
cat $Interim | sed "s/$Pattern/.\//g" | sed "s/.src//g"  | sort | uniq -u >$Unique

I am happy to use a fully in-line style as follows
Code:
cat $Interim | sed "s/   [A-Z] \/Group\/Subgroup\/[.]*\//.\//g" | sed "s/.src//g"  | sort | uniq -u >$Unique


I'm trying to see how I can integrate the provided solution into my wider example. Wish I had shown the fuller answer, was hoping I could apply it to my issue.

Many thanks

---------- Post updated at 10:13 AM ---------- Previous update was at 08:42 AM ----------

Hello Franklin52 and everyone else.

One thing that confuses me is that the following pattern
Code:
[a-zA-Z0-9_]*

Doesn't seem to work correctly, I thought the * meant that any number or none of the characters were matched. So why would hhh_bbb be processed and hhh be ignored?

Im still having problems integrating the solutions into the fuller example I gave. I am unable to use php as it's not on the server. I could use perl but would really like to understand why the underscore is making things so tricky. I realise HP-UX sed isn't normally as fully featured as other versions.

Thanks

---------- Post updated at 10:22 AM ---------- Previous update was at 10:13 AM ----------

Let me share a full problem so you can see more clearly;

Example file to process
Code:
   A /Branch/Subbranch/x9_llll/something/xx/SourceA
   M /Branch/Subbranch/x23_llll/else/dir/SourceB
   M /Branch/Subbranch/x49/else/dir/subdir/SourceC
   M /Branch/Subbranch/x1/else/dir/subdir/SourceD

As far as I can tell the pattern
Code:
[a-zA-Z0-9_]*

Should cope with both x11_lll and x11

When I remove the underscore I see a mixture of stripped down programs only and unprocessed lines as follows;

Code:
   M /Branch/Subbranch/x24_lll/else/dir/SourceH
   M /Branch/Subbranch/x24_lll/something/dir/SourceJ
./something/dir/subdir/SourceX
./else/dir/subdir/SourceY

When I keep the underscore the resultant file looks okay but misses all the lines that had x11 only (as opposed to x11_lll). In the last code fragment the last two lines are correct in final output im looking for.
# 6  
Old 03-04-2014
Have you tried using [[:alnum:]_]* instead of [a-zA-Z0-9_]*?
# 7  
Old 03-04-2014
In your post #5, [.]* means zero or more literal dots, which is not what you intend to match. .* in turn would be a greedy match, removing too many characters. Try instead
Code:
sed "s/   [A-Z ] \/Branch\/Subbranch\/[^/]*\//.\//g" file
                                        ^--- any number of non-slash chars

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Execution Problems with sed

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Okay so I am taking bash scripting this semester and we are now working on the stream editor sed. For our book we... (4 Replies)
Discussion started by: aggie6970
4 Replies

2. UNIX for Advanced & Expert Users

problems with sed

$ echo "a,0,0,0,b,0" | sed 's/,0,/,1,/g' gives output as $ a,1,0,1,b,0 rather than as a,1,1,1,b,0 how can i solve this problem Thanks a lot in advance.... Use code tags. (4 Replies)
Discussion started by: manishma71
4 Replies

3. Shell Programming and Scripting

2 problems... sed and sort

Hi everyone! I have a file like that: And I would it like that: I don't know how to keep the first field and sort the second one. I have a second question with sed... to put the variable $VAR at the beginning of the file... But I have an output like this: snork... (3 Replies)
Discussion started by: Castelior
3 Replies

4. Shell Programming and Scripting

problems using sed

i have a file acc.sh which has about 10 lines and then i have defined $var which has a line number in it (say 5). i want to extarct from line 5 to the end of the file and put the output into another file. I have used sed -n $var,'$p' acc.sh | tee abc.sh but at times it does'nt work and gives an... (6 Replies)
Discussion started by: lassimanji
6 Replies

5. Shell Programming and Scripting

Regex/sed - matching any char,space,underscore between : and /

trying to remove the portion in red: Data: mds_ar/bin/uedw92wp.ksh: $AI_SQL/wkly.sql mds_ar/bin/uedw92wp.ksh: $EDW_TMP/wkly.sql output to be: mds_ar/bin/uedw92wp.ksh: wkly.sql mds_ar/bin/uedw92wp.ksh: wkly.sql SED i'm trying to use: sed 's/:+\//: /g' input_file.dat >... (11 Replies)
Discussion started by: danmauer
11 Replies

6. Solaris

Sed problems on Solaris 10

Hi, The config file: # Port(s) for accepting client connections RTSPPort=554 bash-3.00# awk -F"=" -v gr="888" '/RTSPPort/{$2=gr;}1' OFS="=" server.ini awk: syntax error near line 1 awk: bailing out near line 1 Can you help me on why this doesn't work. The next one neighter. Dosn't... (0 Replies)
Discussion started by: potro
0 Replies

7. Shell Programming and Scripting

Having problems with sed: can't replace $1

At the moment, I'm trying to do the following: 1. Have sed read the first line of a file Example (file1.x): 5 2. Replace that first line with a new first line, which would read 5=newvariable 3. Have that information placed into file2.y Unfortunately, I'm having a problem. Sed... (5 Replies)
Discussion started by: Agent-X
5 Replies

8. Shell Programming and Scripting

Problems with SED

I have a group of xml files and I need to insert 3 parameters just after this line in each file: ---------------Pattern to be searched for------------------------- <!--The following configuration is a test configuration--> ---------------Parameters to be added---------------------------... (11 Replies)
Discussion started by: collern2
11 Replies

9. UNIX for Dummies Questions & Answers

Problems with sed

Hi, I'm trying to use the sed command but I'm not sure how to use it properly. I've read the man pages for the sed command but I'm still unsure on how to use it. Basically I have a file with the words male and female written multiple times. I want to swap the word male for female and... (4 Replies)
Discussion started by: tugade
4 Replies

10. UNIX for Dummies Questions & Answers

sed file problems

when i am running a sed command i want to get rid of all of the backslashes in the lin but it is taking this as being a command how do i delete backslashes????? sed -e "s/\/g" Anyn ideas????????? (7 Replies)
Discussion started by: johnnynolegs
7 Replies
Login or Register to Ask a Question