Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I would like to go through a file and remove certain sequences in between delimiters.

For example

 drw---- 00000000 11111111        0 ./a/
 drw---- 00000000 11111111        0 ./b/
 d------ 00000000 11111111        0 ./c/
 d------ 00000000 11111111        0 ./d/k/
 d------ 00000000 11111111        0 ./e/l/r/
 d------ 00000000 11111111        0 ./f/m/s/x/
 ------- 00000000 11111111       89 ./g/n/t/y/C.xml
 dr----- 00000000 11111111        0 ./h/o/u/z/
 dr-r--- 00000000 11111111        0 ./i/p/v/A/D/
 d--r--- 00000000 11111111        0 ./j/q/w/B/

Would become

 drw---- ./a/
 drw---- ./b/
 d------ ./c/
 d------ ./d/k/
 d------ ./e/l/r/
 d------ ./f/m/s/x/
 ------- ./g/n/t/y/C.xml
 dr----- ./h/o/u/z/
 dr-r--- ./i/p/v/A/D/
 d--r--- ./j/q/w/B/

Where the starting delimiter is the 2nd space in the file, and the ending delimiter is ./

I'm really new to cygwin and all of it's clever tools, so I have no idea what to do. I'm pretty sure I could use sed and regular expressions somehow, but I simply don't know enough to come up with the solution on my own.

share|improve this question
    
delimiter <-- that is how you spell it. You got it right elsewhere but not in the subject. The word limit is in there. –  barlop Apr 6 '12 at 19:28

3 Answers 3

up vote 2 down vote accepted

Simplest way to do it is using awk.

$ awk '{print $1, $5}' myfile.txt

awk reads the file line by line, sets some special variables and runs the command for each line. $1 and $5 here contain first and fifth string when a line is tokenized by using space as delimeter.

share|improve this answer
    
Damn this is a nice solution, thanks so much. I'll have to read up on GAWK, seems like it's very useful. –  Millianz Apr 5 '12 at 19:54
2  
unless any filename has spaces. Then you might want to say awk '{$2=$3=$4=""; print}' –  glenn jackman Apr 5 '12 at 21:33

Here is the regex you want. Either open the file in vim and run it, or do sed the_expression oldname > newname.

:%s/[0-9][0-9]*//g

Explanation:
The % symbol specifies that the following command should be run on the whole file.
s means search/for this expression/and replace it with this one/
In your case you want to delete all the numbers so we instruct vim's regex engine to search for every occurrence of one or more number and replace it with nothing.

share|improve this answer
    
This is a very good solution as well, gawk is a little simpler though if you're not that familiar with regex –  Millianz Apr 6 '12 at 16:45
    
that one is just removing numbers not actually removing that which is between the "delimiters". In the example he gives it'd work as its numbers between the delimiters. What he meant is another matter. –  barlop Apr 6 '12 at 19:27
    
@barlop I know, this was a quick and dirty solution for the data at hand. –  Yitzchak Apr 9 '12 at 22:02

"Where the starting delimiter is the 2nd space in the file, and the ending delimiter is ./"

Here's an ugly one just for you

C:\sdf>type p.p
 drw---- 00000000 11111111        0 ./a/
 drw---- 00000000 11111111        0 ./b/
 d------ 00000000 11111111        0 ./c/
 d------ 00000000 11111111        0 ./d/k/
 d------ 00000000 11111111        0 ./e/l/r/
 d------ 00000000 11111111        0 ./f/m/s/x/
 ------- 00000000 11111111       89 ./g/n/t/y/C.xml
 dr----- 00000000 11111111        0 ./h/o/u/z/
 dr-r--- 00000000 11111111        0 ./i/p/v/A/D/
 d--r--- 00000000 11111111        0 ./j/q/w/B/
C:\sdf>sed -r "s/(\s+\S+\s*)([^.]*\.\/)/\1.\//" p.p
 drw---- ./a/
 drw---- ./b/
 d------ ./c/
 d------ ./d/k/
 d------ ./e/l/r/
 d------ ./f/m/s/x/
 ------- ./g/n/t/y/C.xml
 dr----- ./h/o/u/z/
 dr-r--- ./i/p/v/A/D/
 d--r--- ./j/q/w/B/
C:\sdf>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.