Adding a character in each line depending upon number of such characters present in a line.
Hi !,
How to add a character in front of each line depending upon number of such character present in that line.For eg. Input.txt is london.uk 10.wrs.org baby.us target.zym.com . . . So...on Output.txt results in: .london.uk # "." is added at the beginning of the line because number of "." present in this line was 1 10.wrs.org # "." is not added at the beginning of the line because number of "." present in this line was 2 .baby.us target.zym.com . . . So...on Please Note: # lines are not the part of file output.txt, they are just for explanation. Can you plz suggest how to accomplish this using sed, awk,shell script or any other command?? Thanks in advance. |
Have a look at this:
Code:
awk -F"." 'NF == "2" { print "."$0 ;next }{ print}' infile Code:
$ cat infile |
Code:
awk '{if ($0 ~ /\..+\./){print} else {print "."$0}}' Input.txt |
Hi!
I am in a bit of a hurry right now, but maybe this will get you started: Code:
1 #!/bin/bash Best, HMW |
Oh dear, you guys (previous posters) are fast! And much better than I am at AWK!
HMW |
Thanks druuna,firerat & HMW.
Time to learn more about regular expressions, I guess. |
Actually, you don't really need regular expressions for this stuff. What you do need is to count the occurances of the dots in each line. That can be done (as you've already seen above) in a lot of different ways.
But yes, you are right. Regular expressions are awesome, but I'm not very good at 'em myself. As every other thing it takes time to get the hang of. Time that I don't really have at the moment. I just want to point out that the Awk one-liners posted by the other dudes (or dudettes, what do I know!?!) are probably more efficient than my attempt at a solution. But, Awk is another one of those things I haven't found time to properly learn, it looks like hieroglyphs to me, and I only use the '{ print }' function! Another thing on my to-do list... Best, HMW |
Quote:
Quote:
Mine is a bit sneaky; It uses a different field separator and checks how many fields there are (2 fields -> add leading dot). As already mentioned by HMW: Regular expressions are very powerful and can be used almost anywhere. If you want to know more about them then have a look here: Regular-Expressions.info Here are some awk related links that might come in handy: |
Code:
awk -F. 'NF == 2{$0 = "."$0}1' file Code:
awk -F. '$0= (NF == 2?".":"")$0' file |
assuming you were interested in fields..
Code:
awk '{for (i=1;i<=NF;i++){ |
decided that last one of mine was lame ( it is )
Code:
awk '{for (i=1;i<=NF;i++)if ($i !~ /\..+\./){$i = "."$i};print}' Input |
OK!
That is nice of druuna to mention tutorials for sed/awk. After going through them, l hope, I will shift from zero to expert level. |
Quote:
- Resources / Useful links |
Now!!, this is one of the best part of linuxquestions
Thankyou once again druuna & others. I WILL BE BACK (armed with sed,awk & others). |
All times are GMT -5. The time now is 08:32 PM. |