user3306447
user3306447

Reputation: 13

Delete string between 2 special strings on every line in file

Using awk I need find characters between first "." and first space " " (if exists) and replace it with space " ". I have file (test.txt) with this structure:

asdf.test.com 04-05-2014  
qwer.test1.test.com 04-05-2014  
asdfgg 04-05-2014  
sadf  
asdff.com 04-05-2014 

I need this output:

asdf 04-05-2014  
qwer  04-05-2014  
asdfgg  04-05-2014  
sadf  
asdff  04-05-2014 

Upvotes: 1

Views: 79

Answers (4)

Jotne
Jotne

Reputation: 41460

This should do:

awk '{sub(/\..*/,x,$1)}8' test.txt
asdf04-05-2014
qwer04-05-2014
asdfgg 04-05-2014
sadf
asdff04-05-2014

Updated as suggested by BMW, since there are blank at end of line.


or this:

awk -F"[. ]" '{print $1,(NF>1?$NF:x)}' test.txt
asdf 04-05-2014
qwer 04-05-2014
asdfgg 04-05-2014
sadf
asdff 04-05-2014

Upvotes: -1

BMW
BMW

Reputation: 45333

Using sed, which can fix the issue that space in end of line.

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

Upvotes: 2

Zombo
Zombo

Reputation: 1

awk NF=NF FS='\\..+ ' test.txt

Result

asdf 04-05-2014
qwer 04-05-2014
asdfgg 04-05-2014
sadf
asdff 04-05-2014

Upvotes: 1

mjuarez
mjuarez

Reputation: 16844

Try sed:

cat test.txt | sed 's/\..*\ / /g'

Upvotes: 0

Related Questions