Sponsored Content
Top Forums UNIX for Beginners Questions & Answers How to delete everything present on left (or right) of a substring? Post 303040951 by Sekhar419 on Sunday 10th of November 2019 03:32:37 PM
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!
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
GREP(1) 						      General Commands Manual							   GREP(1)

NAME
grep - search a file for lines containing a given pattern SYNOPSIS
grep [-elnsv] pattern [file] ... OPTIONS
-e -e pattern is the same as pattern -c Print a count of lines matched -i Ignore case -l Print file names, no lines -n Print line numbers -s Status only, no printed output -v Select lines that do not match EXAMPLES
grep mouse file # Find lines in file containing mouse grep [0-9] file # Print lines containing a digit DESCRIPTION
Grep searches one or more files (by default, stdin) and selects out all the lines that match the pattern. All the regular expressions accepted by ed and mined are allowed. In addition, + can be used instead of * to mean 1 or more occurrences, ? can be used to mean 0 or 1 occurrences, and | can be used between two regular expressions to mean either one of them. Parentheses can be used for grouping. If a match is found, exit status 0 is returned. If no match is found, exit status 1 is returned. If an error is detected, exit status 2 is returned. SEE ALSO
cgrep(1), fgrep(1), sed(1), awk(9). GREP(1)
All times are GMT -4. The time now is 12:47 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy