LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Adding a character in each line depending upon number of such characters present in a line. (https://www.linuxquestions.org/questions/linux-newbie-8/adding-a-character-in-each-line-depending-upon-number-of-such-characters-present-in-a-line-4175474372/)

ls_milkyway 08-23-2013 03:31 AM

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.

druuna 08-23-2013 03:53 AM

Have a look at this:
Code:

awk -F"." 'NF == "2" { print "."$0 ;next }{ print}' infile
Example run:
Code:

$ cat infile
london.uk
10.wrs.org
baby.us
target.zym.com

$ awk -F"." 'NF == "2" { print "."$0 ;next }{ print}' infile
.london.uk
10.wrs.org
.baby.us
target.zym.com


Firerat 08-23-2013 03:58 AM

Code:

awk '{if ($0 ~ /\..+\./){print} else {print "."$0}}' Input.txt

HMW 08-23-2013 04:07 AM

Hi!

I am in a bit of a hurry right now, but maybe this will get you started:

Code:

  1 #!/bin/bash
  2
  3 DOTCOUNT=2
  4 COUNT=0
  5
  6 while read line; do
  7  COUNT=$(echo "$line" | grep -o '\.' | wc -m)
  8  echo "$COUNT" # Instead of echo, do your thing with the dots!
  9  done < testfile.txt

This basically counts the dots (plus the newline character) on every line, if they equal 2, then add a dot at the beginning.

Best,
HMW

HMW 08-23-2013 04:09 AM

Oh dear, you guys (previous posters) are fast! And much better than I am at AWK!

HMW

ls_milkyway 08-23-2013 04:43 AM

Thanks druuna,firerat & HMW.
Time to learn more about regular expressions, I guess.

HMW 08-23-2013 04:58 AM

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

druuna 08-23-2013 05:11 AM

Quote:

Originally Posted by ls_milkyway (Post 5014364)
Thanks druuna,firerat & HMW.

You're welcome ;)
Quote:

Time to learn more about regular expressions, I guess.
Only Firerat's solution uses a regular expression.

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:

grail 08-23-2013 05:56 AM

Code:

awk -F. 'NF == 2{$0 = "."$0}1' file
Or:
Code:

awk -F. '$0= (NF == 2?".":"")$0' file
Just as some alternatives

Firerat 08-23-2013 06:31 AM

assuming you were interested in fields..

Code:

awk '{for (i=1;i<=NF;i++){
if ($i ~ /\..+\./){
    printf $i" "
    } else {
    printf "."$i" "
    }
  };
  printf"\n"
}' Infile


Firerat 08-23-2013 12:32 PM

decided that last one of mine was lame ( it is )

Code:

awk '{for (i=1;i<=NF;i++)if ($i !~ /\..+\./){$i = "."$i};print}' Input
shorter and doesn't have the dumb trailing space I had earlier

ls_milkyway 08-23-2013 03:29 PM

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.

druuna 08-24-2013 02:19 AM

Quote:

Originally Posted by ls_milkyway (Post 5014683)
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.

If you are looking for more bash, sed, awk and perl (and other) resources, have a look here:
- Resources / Useful links

ls_milkyway 08-24-2013 03:19 AM

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.