Deleting string within a file that finishes with .log

Hello,

This is my first post. Nice forum!

I have a file trls.results
small exemple of content (actually the file can be very big):
./security/htaccess.htm ./security/ipcount.log ./adhoc/sql/datamod06.sql

So there is 3 paths to 3 different files... I want to remove every string that has a ".log" in it...

After manipulation, it should look like this:
./security/htaccess.htm ./adhoc/sql/datamod06.sql

End results: It's important to keep all inline with 1 space between since I will be adding them to array after...

TO do this... Should I use sed, tr, vi with search and replace, awk ? I want to keep it as simple as possible.

Any Ideas!?!?

The server is a HP-Unix.

Thanks
jake

perl -i -pe 's/ [^ ]+\.log//g' file
1 Like

Thanks !!
Works like a charm...

I will research this part 's/ [^ ]+\.log//g' in order to understand it..

Thanks

There is one issue with it though. It will not delete .log entry if it is first one in the line, like:

./security/ipcount.log ./security/htaccess.htm ./adhoc/sql/datamod06.sql

If you want, I can come up with a fix to that :wink:

sed -e 's/ [^ ]*\.log//g' -e 's/  / /g' -e 's/^ //' file

To modify file in place

sed -i -e 's/ [^ ]*\.log//g' -e 's/  / /g' -e 's/^ //' file

If -i option isn't available, you can redirect outpu to a file.

1 Like

both (perl and sed) works but like you said both won't delete .log entry if it is first one in the line.

It would be greatly appreciate it if you help me with a fix for that...

Thanks

perl -i -pe 's/ [^ ]+\.log//g;s/^[^ ]+\.log ?//' file
1 Like

Good!! Thanks.. works very well

---------- Post updated at 03:03 PM ---------- Previous update was at 02:07 PM ----------

If any one wants to understand the command please read the following:

#### Substitutes anything from space to .log and replace with nothing.
# -i: you can print it to the same file from which you are reading. When you use 'i' switch you will not see the output in STDOUT but the output will be printed to the reading file itself.
# -p: wraps your code in a loop and it has an advantage of printing the line automatically. So to print the file you can use
# -e: switch is for writing a one-liners program, it omits program file. Code can be written inside ' or ".
# s/: make substitutions based on those matches. Function which is designed to mimic the way substitution is done in the vi text editor.
# [^ ] This replaces anything except a space
# +: matches any non-empty string
# g: To make a global substitution the last slash is followed by a g.

# The second substitution does exactly the same thing except there's an another space which means it will remove the first entry.
###

:eek:

# [^ ] This replaces anything except a space
Above statement is not precise. [^ ] only matches characters that are not space. If you are going to replace them later depends in which regex command it is used in (m//, or s///).
Also if you want complete review of this code, you can mention the "?" after space, which indicates that preceding character is optional (this will allow successful deletion of ".log" entry even if there is no space after it, like when that entry is the only one in the line).

Did a mistake, it should be:

sed -e 's/[^ ]*\.log//g' -e 's/  / /g' -e 's/^ //' file

Without the space before [^ ].