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 (.).
# 1  
Old 05-08-2017
Ommit the numbers or any characters only at 8th columns after the dot (.).

Ommit the numbers or any characters only at 7th or 8th columns after the dot (.) . Since the group column has 1 and 2 spaces.
Thanks


Code:
-rw-r--r--. 1 user1   domain users           619 2017-04-13 16:16:50.284598383 +0000  aa
drwxr-xr-x. 2 root    root             6 2017-05-08 12:40:33.182976407  +0000 aaa
-rw-r--r--. 1 root    root         13883 2017-03-31 17:07:35.821185258  +0000 aa.sh
-rw-r--r--. 1 root    root             0 2017-05-08 12:40:36.310976557  +0000 ab

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


Last edited by invinzin21; 05-08-2017 at 10:02 AM..
# 2  
Old 05-08-2017
Hi.

Using perl, and taking advantage of the unique context of each date-related chunk:
Code:
#!/usr/bin/env bash

# @(#) s1       Demonstrate string manipulation, based on context.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
em() { pe "$*" >&2 ; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C perl

FILE=${1-data1}
E=expected-output.txt

pl " Input data file $FILE:"
cat $FILE

pl " Expected output:"
cat $E

pl " Results:"
perl -wpe 's/(:\d\d)([.]\d+\s\s?[+]\d+)/$1/' $FILE |
tee f1

pl " Verify results if possible:"
C=$HOME/bin/pass-fail
[ -f $C ] && $C || ( pe; pe " Results cannot be verified." ) >&2

exit 0

producing:
Code:
$ ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.7 (jessie) 
bash GNU bash 4.3.30
perl 5.20.2

-----
 Input data file data1:
-rw-r--r--. 1 user1   domain users           619 2017-04-13 16:16:50.284598383 +0000  aa
drwxr-xr-x. 2 root    root             6 2017-05-08 12:40:33.182976407  +0000 aaa
-rw-r--r--. 1 root    root         13883 2017-03-31 17:07:35.821185258  +0000 aa.sh
-rw-r--r--. 1 root    root             0 2017-05-08 12:40:36.310976557  +0000 ab

-----
 Expected output:
-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

-----
 Results:
-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

-----
 Verify results if possible:

-----
 Comparison of 4 created lines with 4 lines of desired results:
f1 expected-output.txt differ: char 21, line 1
 Failed -- files f1 and expected-output.txt not identical -- detailed comparison follows.
 Succeeded by ignoring whitespace differences.

The decreased space in the expected output appears to be a typo, so no attempt was made to correct it.

Best wishes ... cheers, drl
This User Gave Thanks to drl For This Post:
# 3  
Old 05-08-2017
Thank you so much.. i appreciated it.. 1thanks for you... but somehow im hoping for awk or sed..i just need a 1 line command..
# 4  
Old 05-08-2017
Hi.
Code:
To get the best advice on transforming, extracting, manipulating data,
please supply the environment and context:

1. Os and shell, preferably with versions
2. Representative input
3. Output desired that corresponds to the input
4. Logic to obtain output from input
5. Attempts at a solution that you have tried

If you need to use or to avoid certain tools, you may need
to explain why, especially if your post count is low. Once the
problem is identified, responders often can choose the most
appropriate tool, not necessarily the one you might want.

These guidelines allow responders to create solutions without
ambiguity and to avoid creating sample data sets.

Note that:
Code:
perl -wpe 's/(:\d\d)([.]\d+\s\s?[+]\d+)/$1/' $FILE

is a single line. The rest is identifying, documenting, and reporting code.

Best wishes ... cheers, drl
This User Gave Thanks to drl For This Post:
# 5  
Old 05-08-2017
how about - could probably be improved:
Code:
sed 's/\(.*\)\(\..*\+.*\)  *\(.*\)$/\1 \3/g' myFile

This User Gave Thanks to vgersh99 For This Post:
# 6  
Old 05-08-2017
Hi Drl,
Can you please explain to me character per character this command please?

perl -wpe 's/(:\d\d)([.]\d+\s\s?[+]\d+)/$1/'

Thanks

---------- Post updated at 10:00 PM ---------- Previous update was at 09:53 PM ----------

Quote:
Originally Posted by vgersh99
how about - could probably be improved:
Code:
sed 's/\(.*\)\(\..*\+.*\)  *\(.*\)$/\1 \3/g' myFile


Sir,
Can you please explain this letter per letter word per word. So next time I will do it on my own?

ls --full-time -rt |sed 's/\(.*\)\(\..*\+.*\) *\(.*\)$/\1 \3/g'

THanks

---------- Post updated at 10:00 PM ---------- Previous update was at 10:00 PM ----------

Quote:
Originally Posted by vgersh99
how about - could probably be improved:
Code:
sed 's/\(.*\)\(\..*\+.*\)  *\(.*\)$/\1 \3/g' myFile


Sir,
Can you please explain this letter per letter word per word. So next time I will do it on my own?

ls --full-time -rt |sed 's/\(.*\)\(\..*\+.*\) *\(.*\)$/\1 \3/g'

THanks
# 7  
Old 05-08-2017
Hi.

Code:
\d   a numeric character
\s   a space (actually any whitespace)
[p]  a literal "p", in this case period, fullstop (usually "." is a meta-character matching anything), [+] matches a plus sign, 
      (usually + is one or more of the previous)
?    a quantifier, either 0 or 1 of the previous item
()   capturing parens, to variable $1

See perldoc perlre for more details ... cheers, drl
Code:
NAME
    perlre - Perl regular expressions

DESCRIPTION
    This page describes the syntax of regular expressions in Perl.

    If you haven't used regular expressions before, a quick-start introduction
    is available in perlrequick, and a longer tutorial introduction is
    available in perlretut.

...

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