sed script to delete the last word after a last pattern match


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers sed script to delete the last word after a last pattern match
# 1  
Old 04-02-2019
sed script to delete the last word after a last pattern match

Hi Guys ,

I am having a file as stated below

File 1

Code:
sa0   --   i_core/i_core_apb/i_afe0_controller/U261/A
 sa0   --   i_core/i_core_apb/i_afe0_controller/U265/Z
 sa1   --   i_core/i_core_apb/i_afe0_controller/U265/A
 sa1   --   i_core/i_core_apb/i_afe0_controller/U268/Z
 sa1   --   i_core/i_core_apb/i_afe0_controller/U268/A
 sa1   --   i_core/i_core_apb/i_afe0_controller/U273/Z
sa0   --   i_core/i_core_ahb/i_hebi/i_hsdramc1/i_hsdramc1_ocms_scrambler/ext_key_regx63x/D
 sa1   AN   i_core/i_core_ahb/i_hebi/i_hsdramc1/i_hsdramc1_ocms_scrambler/ext_key_regx63x/SE

I want to search the last "/" in each and every line and delete the last word after that including last "/"

Output


Code:
sa0   --   i_core/i_core_apb/i_afe0_controller/U261
 sa0   --   i_core/i_core_apb/i_afe0_controller/U265
 sa1   --   i_core/i_core_apb/i_afe0_controller/U265
 sa1   --   i_core/i_core_apb/i_afe0_controller/U268
 sa1   --   i_core/i_core_apb/i_afe0_controller/U268
 sa1   --   i_core/i_core_apb/i_afe0_controller/U273
sa0   --   i_core/i_core_ahb/i_hebi/i_hsdramc1/i_hsdramc1_ocms_scrambler/ext_key_regx63x
 sa1   AN   i_core/i_core_ahb/i_hebi/i_hsdramc1/i_hsdramc1_ocms_scrambler/ext_key_regx63x

Could you help me out?
# 2  
Old 04-02-2019
Try:
Code:
sed 's|/[^/]*$||' "File 1"

But, please always tell us what operating system and shell you're using when you start a new thread so we'll be less likely to post suggestions that won't work in your environment.
# 3  
Old 04-02-2019
Hi Don

Thanks a lot for your reply! I am using Linux RH-6 .
If we need to match with the second last "/" and delete the lines from the second last "/
then how do we need to tweak the script. ?

Thanks
Kshitij
# 4  
Old 04-02-2019
Quote:
Originally Posted by kshitij
Hi Don

Thanks a lot for your reply! I am using Linux RH-6 .
If we need to match with the second last "/" and delete the lines from the second last "/
then how do we need to tweak the script. ?

Thanks
Kshitij
Hi Kshitij,
From your description above I have no idea which lines it is that you want to delete. In the first problem you presented in this thread you only wanted to remove characters from the end of each line. Now that you want to delete some lines, we need a clear specification of which lines you want to delete.

If you just want to remove another "/" character and the non-"/" characters from the ends of every line, I have already shown you how to do that. Why don't you show me how you would modify the code I suggested to delete the last two "/" characters and the non-"/" characters following them from the end of every line. Assuming that every line has at least two "/" characters (as in your example), there are at least three easy ways to do that.

If some lines might only contain one "/", you need to decide whether you want those lines to be changed at all (removing only the last "/" and the characters following it) or if you don't want to make any changes to a line if there aren't at least two "/" characters on the line. The way to do what you want to do depends on what it is that you want to do and on the data that you will be processing.
# 5  
Old 04-08-2019
Hi Don,


can you explain how this reg ex will parse and give the output.I have understand a little the $ represent end of a line and * is all lines, but i didn't get the other things.



Code:
sed 's|/[^/]*$||' "File 1"

# 6  
Old 04-08-2019
May I jump in as Don seems offline for a while now?'


sed operates on files line by line, it reads a line, performs the entire script on it, and reads the next. * is not "all lines" but a sort of mutiplier for the preceding "atom" (man regex).

Code:
s		substitute command
|		regex delimiter; any char allowed, see "man sed"
/[^/]*$		regex: slash, any number (*) non-slashes ([^/]), line end
|		separates regex from replacement
|		replacement terminator, empty replacement = delete regex

These 2 Users Gave Thanks to RudiC For This Post:
# 7  
Old 04-08-2019
Hi nag_sathi,
It looks like RudiC answered the question for you while I was sleeping last night. Do you still have questions about how that sed search command works?

Note that the ability to use any character as a field delimiter is not portable to all operating systems, but does work on Linux systems (and other systems using the GNU sed utility). If you want to write a script that will be portable to other systems, avoid using the <newline> character as a delimiter and avoid using the <null> byte as a delimiter. Even on Linux systems, some shells might not correctly pass your substitute command to sed correctly if you use a literal <null> byte as your delimiter.

Note that I do not know of any shells usually deployed with Linux systems that have this restriction, but the standards say that the input given to the shell is a text file with unlimited line lengths and <null> bytes are not allowed in text files.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed : match one pattern then the next consecutive second pattern not working

Ive used this snippet of code on a solaris box thousands of times. But it isnt working on the new linux box sed -n '/interface LoopBack0/{N;/ ip address /p;}' *.conf its driving me nuts !! Is there something Im missing ? (7 Replies)
Discussion started by: popeye
7 Replies

2. Shell Programming and Scripting

Sed delete blank lines upto first pattern match

Hi Im trying to do the following in sed. I want to delete any blank line at the start of a file until it matches a pattern and then stops. for example: Input output: I have got it to work within a range of two patterns with the following: sed '/1/,/pattern/{/^]*$/d}' The... (2 Replies)
Discussion started by: duonut
2 Replies

3. Shell Programming and Scripting

how to convert a shell script to a php script for displaying next word after pattern match

I have a shell script which I made with the help of this forum #!/bin/sh RuleNum=$1 cat bw_rules | sed 's/^.*-x //' | awk -v var=$RuleNum '$1==var {for(i=1;i<=NF;i++) {if($i=="-bwout") print $(i+3),$(i+1)}}' Basically I have a pages after pages of bandwidth rules and the script gives... (0 Replies)
Discussion started by: sb245
0 Replies

4. Shell Programming and Scripting

print word after pattern match in two instances

i have a file like below. how can i printout the digits followed by the pattern -bwout and -bwin. say i run the script by entering line number 145 (the fourth line), then the o/p should be like 5000000 1024000 8 test1 -ipprot erp -ppsout 500 -ppsin 500 -bwout 300000 -bwin 300000 -statsdevice... (7 Replies)
Discussion started by: sb245
7 Replies

5. Shell Programming and Scripting

Search word in a line and print earlier pattern match

Hi All, I have almost 1000+ files and I want to search specific pattern. Looking forwarded your input. Search for: word1.word2 (Which procedure contain this word, I need procedure name in output. Expected output: procedure test1 procedure test2 procedure test3 procedure test4 ... (7 Replies)
Discussion started by: susau_79
7 Replies

6. Shell Programming and Scripting

Replace 1 word after pattern match

Hi, Here is my pattern CREATE USER LZ IDENTIFIED BY VALUES 'A0144280ESD70' DEFAULT TABLESPACE USERS TEMPORARY TABLESPACE TEMP1 PROFILE DEVELOPER_D_1 ACCOUNT UNLOCK / The Sed command must look for the Line that contains TEMPORARY TABLESPACE and replace the immediate word... (4 Replies)
Discussion started by: rajan_san
4 Replies

7. Shell Programming and Scripting

delete a line that does not match the pattern

hi, i am parsing a file, in that searching for lines those contains "$threadNo.Received message:" , if that line contains the required fields write them into a separate file other wise ignore them. i am using the following code,but it is printing all the lines , i dont want to rpint , please help... (3 Replies)
Discussion started by: Satyak
3 Replies

8. Shell Programming and Scripting

Sed to delete exactly match pattern and print them in other file

Hi there, I need help about using sed. Iam using sed to delete and print lines that match the port number as listed in sedfile. I am using -d and -p command for delete match port and print them respectively. However, the output is not synchonize where the total deleted lines is not similar with... (3 Replies)
Discussion started by: new_buddy
3 Replies

9. Shell Programming and Scripting

SED: match pattern & delete matched lines

Hi all, I have the following data in a file x.csv: > ,this is some text here > ,,,,,,,,,,,,,,,,2006/11/16,0.23 > ,,,,,,,,,,,,,,,,2006/12/16,0.88 < ,,,,,,,,,,,,,,,,this shouldnt be deleted I need to use SED to match anything with a > in the line and delete that line, can someone help... (7 Replies)
Discussion started by: not4google
7 Replies

10. UNIX for Dummies Questions & Answers

How to delete lines do NOT match a pattern

On Unix, it is easy to get those lines that match a pattern, by grep pattern file or those lines that do not, by grep -v pattern file but I am editing a file on Windows with Ultraedit. Ultraedit support regular expression based search and replace. I can delete all the lines that match a... (1 Reply)
Discussion started by: JumboGeng
1 Replies
Login or Register to Ask a Question