Ommit the numbers or any characters only at 8th columns after the dot (.).


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Ommit the numbers or any characters only at 8th columns after the dot (.).
# 8  
Old 05-08-2017
If you cannot find a way of telling ls to filter the output a bit different, then you may try the following:

Code:
perl -pe 's/\.\d{9}\W+\d{4}//' example.output

Output:
Code:
-rw-r--r--. 1 user1   domain users           619 2017-04-13 16:16:50  aa
drwxr-xr-x. 2 root    root             6 2017-05-08 12:40:33 aaa
-rw-r--r--. 1 root    root         13883 2017-03-31 17:07:35 aa.sh
-rw-r--r--. 1 root    root             0 2017-05-08 12:40:36 ab

Code:
s/regex// # substitute regex for empty (delete)
\. # match the period
\d{9} # match nine digits (284598383)
\W+ # match white spaces and plus symbol
\d{4} # match four more digits (0000)

# 9  
Old 05-08-2017
Quote:
Originally Posted by invinzin21
Can you please explain this letter per letter word per word. So next time I will do it on my own?
Of course, but we would really appreciate it if you could obey the forum rules and post code (any code, data and output) in CODE-tags or - if they appear in running text, like commands - in ICODE-tags. For instance write the command ls --full-time -rt |sed 's/\(.*\)\(\..*\+.*\) *\(.*\)$/\1 \3/g' like this.

Back to your question:

Code:
sed 's/\(.*\)\(\..*\+.*\)  *\(.*\)$/\1 \3/g'

First, the basic command:

Code:
sed 's/<something>/\1 \3/g

We replace something (in fact every instance of something, because of the "g" at the end) by \1 \3. \1 and \3 are so-called "back-references". They work like variables: you search for something in the search part (the "<something>") and whatever you have found is put into the variable. The "first" and the "third" such found things will be put into the result, effectively deleting the second.

Now, lets have a look at the "something" which the input line is broken up into:

Code:
\(.*\)\(\..*\+.*\)  *\(.*\)$

Whatever is between "\(" and "\)" is put into such a backreference, hence we see three such pairs (marked bold) and a few characters in between:

Code:
\(.*\)
\(\..*\+.*\)
  *
\(.*\)
$

Let us first deal with the things outside the bracket pairs: * is a space, followed zero or more spaces. The asterisk means "zero or more of the character (in fact "regex", but in this case the regex is only a single character) before", hence "one or more of this character" is expressed by first such a character, then the same character with the asterisk:

Code:
x*       # zero or more x'es, hence even no x at all
xx*      # one or more x'es, hence at least one x

The $ means "end of line" and is a way of "anchoring" a regular expression. If you search for a group of characters they could appear anywhere in a line. If you want to specifically search for a word appearing at the beginning or the end of a line these anchors (there is ^ for "beginning of line" and $ for "end of line") are the means to express that.

To sum up so far, the search expression means:

Code:
\(??\)\(??\)<one or more spaces>\(??\)<end-of-line>

For the "??" parts:

Code:
\(.*\)\(\..*\+.*\)  *\(.*\)$

The dot (.) means "any character", therefore, in conjunction with the asterisk, which means "any number of what precedes me", "any number of any character" - the first bracket pair pretty much mathces everything in any length.

If this would be the whole regex it would match the complete line. But because it isn't the second brackets pair is in fact limiting it:

Code:
\(\..*\+.*\)

This matches a literal dot character (because the dot has a special meaning to sed if you want to match only a real literal dot you need to "escape" it - precede it with a backslash: "." = "any character "\." = "a literal dot character". Analogous for "\+" (escaped "+" character), hence: the meaning of the regexp inside the bracket pair is: a literal dot, followed by anything, followed by a literal "+", followed by anything.

You should now be able to decipher the rest and put together what it means in context. One thing you need to know, though: regexps are always "greedy" meaning that if there are several ways to match something always the longest possible match
is used. For example, here is some input and a regexp. The matched part is marked bold:

Code:
aBxyzBbla-foo-BsomethingBandsomemore
a.*B

Notice that "aB" would also have been a valid match for a.*B, but the longest possible is the one i marked. Therefore will the first regexp part i.e. skip over the first literal dot (after the filemode field: drwxrwxrwx.) and only go for the second one.

@drl: I think you could forego the "g" at the end, because you anchor the regexp at the end-of-line anyway.

I hope this helps.

bakunin
These 2 Users 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

How i can add via preg replace dot after numbers ?

So lets say i have file my_birthday.402.zip ho it can became my_birthday.4.0.2.zip Thank you :) (1 Reply)
Discussion started by: ZerO13
1 Replies

2. Shell Programming and Scripting

Adding (as in arithmetic) to numbers in columns in file, and writing new file with new numbers

Hi again. Sorry for all the questions — I've tried to do all this myself but I'm just not good enough yet, and the help I've received so far from bartus11 has been absolutely invaluable. Hopefully this will be the last bit of file manipulation I need to do. I have a file which is formatted as... (4 Replies)
Discussion started by: crunchgargoyle
4 Replies

3. Shell Programming and Scripting

How to ignore characters and print only numbers using awk?

Input: ak=70&cat15481=lot=6991901">Kaschau (1820-1840) ak=7078&cat15482=lot=70121">Principauté (1940-1993) ak=709&cat=lot15484=70183944">Arubas (4543-5043)Output: 70 15481 6991901 7078 15482 70121 709 15484 70183944 (11 Replies)
Discussion started by: sdf
11 Replies

4. UNIX for Dummies Questions & Answers

Replace 8th and 9th characters in file permanently

Good evening, I have a file and wish to replace the 8th and 9th characters on the first line only no matter what they are with 44 and the file permanantly changed. e.g. file example.txt before change: 123456789123456 hi how are you blah blah file example.txt after change: ... (4 Replies)
Discussion started by: GarciasMuffin
4 Replies

5. Shell Programming and Scripting

Truncate all characters and numbers in string - using perl

hi, I have an data from file where it has 20110904 234516 <<hdd-10#console|0c.57,passed,5,28,READ,0,20822392,8,5,4,0,40,0,-1,0,29909,25000,835,3.3,0,0,0,0,implied,0,0,2011/9/5-2:3:17,2011/9/5-2:3:47,X292_0F15,TAP ,NQ09,J40LTG\r\r\n I want to remove characters till #console| i.e want... (1 Reply)
Discussion started by: asak
1 Replies

6. UNIX for Dummies Questions & Answers

Matching numbers of characters in two lines

Dear all, I'm stuck on a certain problem regarding counting the number of characters in one line and then adjusting the number of characters of another line to this number. This was my original input data: @HWI-ST471_57:1:1:1231:2079/2... (4 Replies)
Discussion started by: DerSeb
4 Replies

7. Shell Programming and Scripting

Greping numbers with dot in it

Hi, I wanted to create a script what would take two numbers out of two files and add them together, but I got stuck with greping numbers what have a dot in it. So far I have grepped the two lines what include the numbers I need (from both files) to a third file and from that file I try to... (7 Replies)
Discussion started by: mario8eren
7 Replies

8. Shell Programming and Scripting

searching regular expressions with special characters like dot using grep

hi everybody I am a new user to this forum and its previous posts have been very useful. I'm searching in a file using grep for patterns like 12.13.444 55.44.443 i.e. of form <digit><digit>.<digit><digit>.<digit><digit><digit> Can anybody help me with this. Thanks in advance (4 Replies)
Discussion started by: jpriyank
4 Replies

9. Shell Programming and Scripting

how to ommit column

Good afternoon, just wanna ask how to ommit, for example column 2 $cat file 1 2 3 4 1 2 3 4 5 1 2 3 1 2 1 2 3 4 5 6 output: 1 3 4 1 3 4 5 1 3 1 1 3 4 5 6 (2 Replies)
Discussion started by: invinzin21
2 Replies

10. Shell Programming and Scripting

how to ommit space

Good afternoon, How to output like this. $cat file file1 file2 file3 $cat file file1 file2 file3 (1 Reply)
Discussion started by: kenshinhimura
1 Replies
Login or Register to Ask a Question