Extract filepath names between two strings


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract filepath names between two strings
# 1  
Old 05-25-2018
Extract filepath names between two strings

OS : Fedora Linux 26
Shell : bash

I have a file with around 5000 lines like below.

Code:
file /usr/share/icons/Papirus/16x16/actions/papirus-icon-theme-20180501-1.noarch conflicts with file ... 
file /usr/share/icons/Papirus/16x16/actions/align-horizontal-left-to-anchor.svg conflicts between .... 
file /usr/share/icons/Papirus/16x16/actions/align-horizontal-left.svg conflicts between ... 
file /usr/share/icons/Papirus/16x16/actions/align-horizontal-right-out.svg conflicts between ... 
file /usr/share/icons/Papirus/16x16/actions/align-horizontal-right-to-anchor.svg conflicts between ..

I want to extract the filepath between the words "file" and "conflicts"

So, the output would be like below without any leading and trailing empty spaces

Code:
/usr/share/icons/Papirus/16x16/actions/papirus-icon-theme-20180501-1.noarch  
/usr/share/icons/Papirus/16x16/actions/align-horizontal-left-to-anchor.svg
/usr/share/icons/Papirus/16x16/actions/align-horizontal-left.svg
/usr/share/icons/Papirus/16x16/actions/align-horizontal-right-out.svg 
/usr/share/icons/Papirus/16x16/actions/align-horizontal-right-to-anchor.svg

Anyway I could do this ?
# 2  
Old 05-25-2018
Any attempts / ideas / thoughts from your side?
# 3  
Old 05-26-2018
awk '{print $2}' <fileName> did the trick
I didn't know that a pathname like /usr/share/icons/Papirus/16x16/actions/address-book-new.svg will be considered a single column

So, i just got the second column printed using the above awk command
This User Gave Thanks to John K For This Post:
# 4  
Old 05-26-2018
Brilliant!
Yes, it will do as long as there are NO SPACES in the file names.
And, the file name is considered a single column as long as it doesn't have field separators in it, i.e. you don't define awk's FS to be (or contain) / .


There are of course several other approaches as well, e.g.:
Code:
cut -d" " -f2 file
 sed 's/^[^ ]* //; s/ .*$//' file
grep -o '/[^ ]*' file

Of course, if you need to fulfill your full-blown spec, this
Code:
sed 's/^file //; s/ conflict.*$//' file

or
Code:
sed 's/^file \| conflict.*$//g' file

were the way to go.

Last edited by RudiC; 05-26-2018 at 09:29 AM..
This User Gave Thanks to RudiC For This Post:
# 5  
Old 05-26-2018
Thank You Rudic
The 2 solutions you've provided for the full-blown spec have one minor issue. The word "file" is also printed as the shown the output
But, I just need the filepath name

sed 's/^file //; s/ conflict.*$//' <filename>

and

sed 's/^file \| conflict.*$//g' <filename>


---- Output of above commands -----
Code:
file /usr/share/icons/Papirus/24x24/actions/zoom-fit-drawing.svg
file /usr/share/icons/Papirus/24x24/actions/zoom-fit-height.svg
file /usr/share/icons/Papirus/24x24/actions/zoom-fit-page.svg
file /usr/share/icons/Papirus/24x24/actions/zoom-fit-selection.svg
.
.
.

# 6  
Old 05-26-2018
Not with the sed and file versions that I have:
Code:
sed 's/^file \| conflict.*$//g' file
/usr/share/icons/Papirus/16x16/actions/papirus-icon-theme-20180501-1.noarch
/usr/share/icons/Papirus/16x16/actions/align-horizontal-left-to-anchor.svg
/usr/share/icons/Papirus/16x16/actions/align-horizontal-left.svg
/usr/share/icons/Papirus/16x16/actions/align-horizontal-right-out.svg
/usr/share/icons/Papirus/16x16/actions/align-horizontal-right-to-anchor.svg


Any special chars (or e.g. a <TAB> in lieu of space) in your file? What be your sed version?
This User Gave Thanks to RudiC For This Post:
# 7  
Old 05-26-2018
Quote:
Originally Posted by John K
The word "file" is also printed as the shown the output
But, I just need the filepath name

sed 's/^file //; s/ conflict.*$//' <filename>
I think the same as obviously RudiC does: perhaps the empty spaces are not what they seem to be (tabs instead of spaces or the like). Modify your code above to:

Code:
sed 's/^file[[:space:]]*//; s/[[:space:]]*conflict.*$//' <filename>

to get some additional variability. Notice that the regexp "[[:space:]]*" will cover for tabs (or any other form of whitespace) instead of spaces and the asterisk will make sure that these are completely removed even if there are several (instead of the expected one) of them. i.e. this line:

Code:
file  /path/to/file  conflicts...
file /this/is/a/normal/line conflicts...

(notice the multiple spaces) would produce the output:

Code:
 /path/to/file 
/this/is/a/normal/line

with leading/trailing blanks without the asterisks.

I hope this helps.

bakunin
This User Gave Thanks to bakunin 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

Extract strings from output

I am having the following output when executing a dig command : dig @1.1.1.1 google.com +noall +answer +stats ; <<>> DiG 9.11.4-P1 <<>> @1.1.1.1 google.com +noall +answer +stats ; (1 server found) ;; global options: +cmd obodrm.prod.at.dmdsdp.com. 86154 IN A ... (1 Reply)
Discussion started by: liviusbr
1 Replies

2. UNIX for Dummies Questions & Answers

Issue when using egrep to extract strings (too many strings)

Dear all, I have a data like below (n of rows=400,000) and I want to extract the rows with certain strings. I use code below. It works if there is not too many strings for example n of strings <5000. while I have 90,000 strings to extract. If I use the egrep code below, I will get error: ... (3 Replies)
Discussion started by: forevertl
3 Replies

3. UNIX for Dummies Questions & Answers

Extract code between 2 strings.

Hi, Im having some problems with this. I have loaded a file with html code. All code is placed in the same line. I want to get everything between two given strings (including these strings and get only the first appearance). Example: File contains <html><body><a href='a.html'>abc</a><a... (5 Replies)
Discussion started by: ngb
5 Replies

4. Shell Programming and Scripting

Extract two strings from a file and create a new file with these strings

I have the following lines in a log file. It would be great if some one can help me to create a new file with the just entries in the below format. 66.150.161.195 HPSAC=Z05 66.150.161.196 HPSAC=A05 That is just extract the IP address and the string DPSAC=its value 66.150.161.195 -... (1 Reply)
Discussion started by: Tuxidow
1 Replies

5. Shell Programming and Scripting

Extract function names and directories from php files

I need a script that extracts function names from php files together with their location (path and file in which they are defined). The php files are located in several directories under a base directory. Ideally the output should be something like: "Path/FileName/FunctionName" for a... (2 Replies)
Discussion started by: bamse
2 Replies

6. Shell Programming and Scripting

Extract all proper names from string with awk

I want to extract the proper names with awk from a very long string, like: õ(k): &lt;/span&gt;<br /><a something="pls/pe/person.person?i_pers_id=3694&amp;i_topic_id=2&amp;i_city_id=3372&amp;i_county_id=-1" target="_blank"><b>Gary Oldman</b></a> (George Smiley)<br /><a... (12 Replies)
Discussion started by: lyp
12 Replies

7. Shell Programming and Scripting

Extract text between two strings

Hi I have something like this: EXAMPLE 1 CREATE UNIQUE INDEX "STRING_1"."STRING_2" ON "BOSNI_CAB_EVENTO" ("CD_EVENTO" , "CD_EJECUCION" ) PCTFREE 10 INITRANS 2 MAXTRANS 255 STORAGE(INITIAL 5242880 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT) TABLESPACE "DB1000_INDICES_512K"... (4 Replies)
Discussion started by: chrispaz81
4 Replies

8. UNIX for Dummies Questions & Answers

command to extract sub-string out of file names

I have these files in a directory. It may have more class than the sample below: DEPT_CHEM101LEC_D_20110301.DAT DEPT_CHEM101LAB_D_20110301.DAT DEPT_BIO105LEC_D_20110325.DAT DEPT_BIO105LAB_D_20110325.DAT DEPT_CSC308LEC_D_20110327.DAT DEPT_CSC308LAB_D_20110327.DAT Is there way to extract out... (5 Replies)
Discussion started by: lv99
5 Replies

9. Shell Programming and Scripting

How extract strings (perl)

Sample data: revision001 | some text | some text Comment: some comment Brief: 1) brief 2) brief ------------------------------------------ revision002 | some text | some text Brief: 1) brief 2) brief FIX: some fix ------------------------------------------ revision003 | some... (8 Replies)
Discussion started by: inotech
8 Replies

10. Shell Programming and Scripting

tar: extract single file to different filepath

Hi, This is my first post here - I'm hoping I can get some help! I have searched these forums and othersand not getting anything that works. I am trying to extract a single file from a tar archive to a diffierent location than it will default to. For example my tar log shows me ... a... (3 Replies)
Discussion started by: littleIdiot
3 Replies
Login or Register to Ask a Question