Deleting pattern without removing line

I am trying to delete a pattern without removing line. I searched a lot in this forum and using those I could come up with sed command but it seems that command does not work. Here's how my file looks like:

1 ./63990 7
1171 ./63990 2
2425 ./63990 9
2539 ./63990 1
3125 ./63990 1
10141 ./63990 1
10968 ./63990 1
11571 ./63990 2
11755 ./63990 1
12757 ./63990 1
13679 ./63990 1
13813 ./63990 4
14349 ./63990 1
14396 ./63990 13
16376 ./63990 1
17846 ./63990 3

I want my output to be like this:

1 63990 7
1171 63990 2
2425 63990 9
2539 63990 1
3125 63990 1
10141 63990 1
10968 63990 1
11571 63990 2
11755 63990 1
12757 63990 1
13679 63990 1
13813 63990 4
14349 63990 1
14396 63990 13
16376 63990 1
17846 63990 3

This is what I tried but to no avail:

sed "s/.\///g" file

In Linux with BASH

sed 's/\.\///g' file
1 Like
1 Like
$ nawk -F"[./]" '{print $1,$3}' infile
1 Like

Others have offered alternative solutions but no-one has told you why your solution did not work. The dot (.) is a special character in sed and other other languages that use regular expressions to mean match any character. So the dot needs to be escaped in this situation.

It is also worth noting that you can change the delimiters in your sed expression:

sed 's^\./^^g' file

Funnily enough I tried your solution and it worked. How was it not working?

Andrew

1 Like

That's great answer. :slight_smile:

But indeed the regex that I posted wasn't working at my end (I really don't know why as I am not an expert in analysing such regex codes) but the solutions given above did work out.

Thanks for pointing out the shortcoming of my expression. I really appreciate that.

awk -F"[./]" '$1=$1' file

:wink:

1 Like

I'm late to the party, but here's another way if you want the output formatted nicely:

awk '{sub(/\.\//, "")} {printf "%-10s%-10s%-05s\n", $1,$2,$3}' INPUTFILE
1           63990    7    
1171      63990     2    
2425      63990     9    
2539      63990     1    
3125      63990     1    
10141     63990     1    
10968     63990     1    
11571     63990     2    
11755     63990     1    
12757     63990     1    
13679     63990     1    
13813     63990     4    
14349     63990     1    
14396     63990     13   
16376     63990     1    
17846     63990     3
1 Like