1

I am in the learning phase of sed and awk commands, trying some complicated logic but couldn't get solution for the below.

File contents:

This is apple,apple.com  443,apple2.com  80,apple3.com      232,
We talk on 1 banana,banana.com   80,banannna.com 23,
take 5 grape,grape5.com 23,

When I try with

$ cat sample.txt | sed -e 's/[[:space:]][^,]*,/,/g'
,apple.com,apple2.com,apple3.com,
,banana.com,banannna.com,
,grape5.com,

is ok but I want to skip this sed for the first comma in each line, so expected output is

This is apple,apple.com,apple2.com,apple3.com,
We talk on 1 banana,banana.com,banannna.com,
take 5 grape,grape5.com,

Any help is appreciated.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 3
    Hi Shravan. I noticed you've tagged a bunch of recent questions with awk, sed, and perl, even though you only mention awk and sed in the questions. Are you also trying to learn perl? If not, can you please only use the tags that are directly relevant to your question? – ThisSuitIsBlackNot Dec 07 '16 at 18:17
  • Note [UUoC — Useless Use of `cat`](https://stackoverflow.com/questions/11710552/). – Jonathan Leffler Dec 08 '16 at 04:46

2 Answers2

2

If you are using GNU sed, you can do something like

sed -e 's/[[:space:]][^,]*,/,/2g' file

where the 2g specifies something like start the substitution from the 2nd occurrence and g for doing it subsequently to the rest of the occurrences.

The output for the above command.

sed -e 's/[[:space:]][^,]*,/,/2g' file
This is apple,apple.com,apple2.com,apple3.com,
We talk on 1 banana,banana.com,banannna.com,
take 5 grape,grape5.com,

An excerpt from the man page of GNU sed

g Apply the replacement to all matches to the regexp, not just the first.

number Only replace the numberth match of the regexp.

Community
  • 1
  • 1
Inian
  • 80,270
  • 14
  • 142
  • 161
0
 awk '{gsub(/[ ]+/," ")gsub(/com [0-9]+/,"com")}1' file

    This is apple,apple.com,apple2.com,apple3.com,
    We talk on 1 banana,banana.com,banannna.com,
    take 5 grape,grape5.com,

The first gsub removes extra space and the next one takes away unwanted numbers between com and comma.

Claes Wikner
  • 1,457
  • 1
  • 9
  • 8
  • 5
    While this code snippet may solve the question, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, this reduces the readability of both the code and the explanations! – kayess Dec 08 '16 at 08:37