How to delete everything present on left (or right) of a substring?


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers How to delete everything present on left (or right) of a substring?
# 1  
Old 11-10-2019
How to delete everything present on left (or right) of a substring?

Hello,

I have list of lines from which i am trying to pick a sub string and want to put that into a csv file, the sub string i want to extract is at the middle of the line, i was wondering how can i delete everything that is present left/right of a sub string. I have tried sed, cut and awk, but i couldn't get desired results.

Below are few lines, from which i am trying to pick PXCNUMBER_1 and append them to a csv file.

Code:
> grep -hrs "PROTOBUF_DIRS" --include=*.{spec,mk} $REPOROOT | grep PXC | sort | uniq                              
PROTOBUF_DIRS += $(GIT_TOP)/AIS_MSGS_PXC2010286_1/inc
PROTOBUF_DIRS += $(GIT_TOP)/C2RCI_PXC1106956_1/ifModel
PROTOBUF_DIRS += $(GIT_TOP)/FRUPLI_PXC1107046_1/ifModel
PROTOBUF_DIRS += $(GIT_TOP)/GPBEXTENSIONS_PXC2010263_1/ifModel
PROTOBUF_DIRS += $(GIT_TOP)/ICEUI_PXC2010238_1/ifModel
PROTOBUF_DIRS += $(GIT_TOP)/L1PMI_PXC1107130_1/inc
PROTOBUF_DIRS += $(GIT_TOP)/UHLI_PXC2010327_1/ifModel
PROTOBUF_DIRS += $(GIT_TOP)/URI_PXC2010247_1/ifModel

using awk and cut i got this result, due to some inconsistency in the lines i failed to pick the PXCNUMBERS_1, precisely as you could see there is MSGS_ in the last line before PXCNUMBERS_1 .

Code:
> grep -hrs "PROTOBUF_DIRS" --include=*.{spec,mk} $REPOROOT | grep PXC | awk -F '/' '{print $2}' | cut -d"_" -f2- | sort | uniq
PXC1106956_1
PXC1107046_1
PXC1107130_1
PXC2010238_1
PXC2010247_1
PXC2010263_1
PXC2010327_1
MSGS_PXC2010286_1

Desired results are
Code:
PXC1106956_1
PXC1107046_1
PXC1107130_1
PXC2010238_1
PXC2010247_1
PXC2010263_1
PXC2010327_1
PXC2010286_1

I know i could use sed to replace MSGS in the last line to empty, but there are more lines than i have showed above, and i was also curious to know if there is any command to cut everything left/right of a sub string. any pointers would be great help Smilie

Linux distrubution: SUSE
Shell: Bash

Thank you!
# 2  
Old 11-10-2019
How about
Code:
awk '/PROTOBUF_DIRS/ && match ($0, /PXC[^_]*_1/) {print substr ($0, RSTART, RLENGTH)}' *.spec *.mk

awk unfortunately lacks both the -r (--recursive) and the --include options that grep provides, but find could help in either case.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 11-12-2019
@Rudic

Thank you it worked.

Code:
find . -type f \( -name \*.spec -o -name \*.mk \) | xargs grep -E 'PROTOBUF_DIRS|PXC' |awk '/PROTOBUF_DIRS/ && match ($0, /PXC[^_]*_1/) {print substr ($0, RSTART, RLENGTH)}' | sort | uniq

can you please explain ($0, /PXC[^_]*_1/) this part in the command above, how it found the sub string of interest briefly.
# 4  
Old 11-12-2019
man awk:
Quote:
match(s,r)
Returns the index of the first longest match of regular expression r in string s. Returns 0 if no match. As a side effect, RSTART is set to the return value. RLENGTH is set to the length of the match or -1 if no match. If the empty string is matched, RLENGTH is set to 0, and 1 is returned if the match is at the front, and length(s)+1 is returned if the match is at the back.
The match searches $0 for your target string PCX..._1 (the ellipsis replaced by [^_]*, i.e. non-"_" characters), and, if found, sets RSTART and RLENGTH accordingly for immediate use by the substr function.


BTW, that awk scriptlet doesn't need the upfront grep (as it checks lines for PROTOBUF_DIRS already and on its own), and it can be adapted to also make the uniq redundant.

Last edited by RudiC; 11-13-2019 at 01:51 PM..
These 2 Users Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

How much resources is left on the P7?

I know that it is possible to login into the HMC console and view all the specs like, how much CPU/RAM every LPAR has. But how can I check how much the whole P7 has in total and how much is left to creat a new LPAR:wall: (5 Replies)
Discussion started by: DiViN3
5 Replies

2. 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

3. Shell Programming and Scripting

left join using awk

Hi guys, I need to use awk to join 2 files file_1 A 001 B 002 C 003 file_2 A XX1 B XX2 output desired A 001 XX1 B 002 missing C 003 XX2 thank you! (2 Replies)
Discussion started by: g1org1o
2 Replies

4. Shell Programming and Scripting

Need unix commands to delete records from one file if the same record present in another file...

Need unix commands to delete records from one file if the same record present in another file... just like join ... if the record present in both files.. delete from first file or delete the particular record and write the unmatched records to new file.. tried with grep and while... (6 Replies)
Discussion started by: msathees
6 Replies

5. Shell Programming and Scripting

Delete a pattern present in file 2 from file 1 if found in file 1.

I have two files File1 ==== 1|2000-00-00|2010-02-02|| 2| 00:00:00|2012-02-24|| 3|2000-00-00|2011-02-02|| File2 ==== 2000-00-00 00:00:00 I want the delete the patterns which are found in file 2 from file 1, Expected output: File1 ==== (5 Replies)
Discussion started by: machomaddy
5 Replies

6. Shell Programming and Scripting

Left padding in Unix

I am passing input string,length, and the pad character. input string=123 Pad char=# Length=6 then the output should be: ###123 How we can do this? Thanks (5 Replies)
Discussion started by: pandeesh
5 Replies

7. Shell Programming and Scripting

Delete Strings that are present in another file

HI, if a String is present in file1.txt, i want to delete that String from file2.txt. How can i do this?? I am sure that the file1.txt is a subset of file2.txt. (2 Replies)
Discussion started by: jathin12
2 Replies

8. Solaris

No space left on device

Hi all, A very strange problem I have this morning with my Solaris 8. I have a FS full, I deleted some files but the system doesn't seems to reallocate the free space (I'm using Veritas): df -k : /dev/vx/dsk/dlds02vg/dlds02oralv 4194304 4194304 0 100% /dlds02/lds/oracle ... (4 Replies)
Discussion started by: unclefab
4 Replies

9. UNIX for Dummies Questions & Answers

indenting lines to the left

hi i have a file which is like this : aab ghj ghj lsklk lklkl; ashjd kjs alskj How do i remove the spaces from the beginning of a line so that all lines are indented to the left? (1 Reply)
Discussion started by: napolayan
1 Replies

10. Shell Programming and Scripting

what left of the pattern

I have a script which loop through a directory then report any file matches the given pattern, say, the pattern is "a2006", this file would be returned a20061101.txt I would like to know how can I get the remaining of the filename, so a20061101txt - a2006 = 1101.txt Can anybody help? Thank... (2 Replies)
Discussion started by: mpang_
2 Replies
Login or Register to Ask a Question