LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-23-2013, 03:31 AM   #1
ls_milkyway
LQ Newbie
 
Registered: Aug 2013
Distribution: BT5R2
Posts: 28

Rep: Reputation: Disabled
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.
 
Old 08-23-2013, 03:53 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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
 
2 members found this post helpful.
Old 08-23-2013, 03:58 AM   #3
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
Code:
awk '{if ($0 ~ /\..+\./){print} else {print "."$0}}' Input.txt
 
Old 08-23-2013, 04:07 AM   #4
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
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
 
Old 08-23-2013, 04:09 AM   #5
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

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

HMW
 
Old 08-23-2013, 04:43 AM   #6
ls_milkyway
LQ Newbie
 
Registered: Aug 2013
Distribution: BT5R2
Posts: 28

Original Poster
Rep: Reputation: Disabled
Thanks druuna,firerat & HMW.
Time to learn more about regular expressions, I guess.
 
Old 08-23-2013, 04:58 AM   #7
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
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
 
Old 08-23-2013, 05:11 AM   #8
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Quote:
Originally Posted by ls_milkyway View Post
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:
 
1 members found this post helpful.
Old 08-23-2013, 05:56 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Code:
awk -F. 'NF == 2{$0 = "."$0}1' file
Or:
Code:
awk -F. '$0= (NF == 2?".":"")$0' file
Just as some alternatives
 
Old 08-23-2013, 06:31 AM   #10
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
assuming you were interested in fields..

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

Last edited by Firerat; 08-23-2013 at 06:33 AM.
 
Old 08-23-2013, 12:32 PM   #11
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
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
 
Old 08-23-2013, 03:29 PM   #12
ls_milkyway
LQ Newbie
 
Registered: Aug 2013
Distribution: BT5R2
Posts: 28

Original Poster
Rep: Reputation: Disabled
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.
 
Old 08-24-2013, 02:19 AM   #13
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Quote:
Originally Posted by ls_milkyway View Post
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
 
Old 08-24-2013, 03:19 AM   #14
ls_milkyway
LQ Newbie
 
Registered: Aug 2013
Distribution: BT5R2
Posts: 28

Original Poster
Rep: Reputation: Disabled
Now!!, this is one of the best part of linuxquestions
Thankyou once again druuna & others.
I WILL BE BACK (armed with sed,awk & others).
 
  


Reply

Tags
awk, sed, shell script



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Insert line using sed or awk at line using line number as variable sunilsagar Programming 11 02-03-2012 10:48 AM
adding number and string to each line in a data file teresevo1 Programming 7 04-11-2010 12:03 AM
How to sort by line size (number of characters in a line) fast_rizwaan Linux - General 8 01-08-2010 05:53 PM
End-of-line Characters missing from last line of md5 file. Md5sum fails mehorter Linux - General 5 06-29-2009 08:56 PM
Removing new line characters on every line execpt first line bioinformatics_guy Linux - Newbie 4 10-21-2008 12:41 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration