LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-07-2014, 11:57 AM   #1
lwv962
Member
 
Registered: Feb 2011
Location: Wales
Distribution: MX Linux 18
Posts: 36

Rep: Reputation: 0
command to add a word at the beginning of each line that starts with a digit


Hi,
I'm searching as it says in the subject for a "command to add a word at the beginning of each line that starts with a digit
eg:
1:23 This is the text..................................
Art1:23 This is how would like it to appear, the word would vary but I can fix that.
To do it by pasting would take ages.
Thanks in anticipation
lwv962
PS.I'm using bash.

Last edited by lwv962; 12-07-2014 at 12:04 PM.
 
Old 12-07-2014, 12:13 PM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,789

Rep: Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304
so what have you tried? what do you prefer (perl/awk/sed/python....)?
 
Old 12-07-2014, 12:20 PM   #3
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
With this InFile ...
Code:
Now is the time
1:13 for all good men
to come to
2:02 the aid of their party.
That's all, folks!
... this awk ...
Code:
awk -F "" '{if ($1~/[0-9]/) $0="grapefruit "$0}1' $InFile >$OutFile
... produced this OutFile ...
Code:
Now is the time
grapefruit 1:13 for all good men
to come to
grapefruit 2:02 the aid of their party.
That's all, folks!
Maybe you want to make the Added Word a parameter. This awk ...
Code:
AW="pomegranate"  # AW = Added Word.
awk -v AW=$AW -F "" '{if ($1~/[0-9]/) $0=AW" "$0}1' $InFile >$OutFile
... produced this OutFile ...
Code:
Now is the time
pomegranate 1:13 for all good men
to come to
pomegranate 2:02 the aid of their party.
That's all, folks!
Daniel B. Martin

Last edited by danielbmartin; 12-07-2014 at 12:29 PM. Reason: Cosmetic improvement, no change to code.
 
1 members found this post helpful.
Old 12-07-2014, 12:27 PM   #4
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
With this InFile ...
Code:
Now is the time
1:13 for all 26 good men
to come 4:40 to
2:02 the aid of their party.
That's all, folks!
... this sed ...
Code:
sed 's/\(^[0-9]\)/peach \1/' $InFile >$OutFile
... produced this OutFile ...
Code:
Now is the time
peach 1:13 for all 26 good men
to come 4:40 to
peach 2:02 the aid of their party.
That's all, folks!
Daniel B. Martin
 
1 members found this post helpful.
Old 12-07-2014, 01:51 PM   #5
lwv962
Member
 
Registered: Feb 2011
Location: Wales
Distribution: MX Linux 18
Posts: 36

Original Poster
Rep: Reputation: 0
Thanks for your reply Pan64.I've tried vim but it seems limited to substitution,I've used it several times.
The problem is entering 'a word ' ONLY on lines that start with a digit/s eg.

1:12 Here is text
no text here
nothing here either

1:13 text again with digits
non digit line

I've tried :1,6 s/^/Gen/ in vim and got:

Gen1:12 Here is text
Genno text here
Gennothing here either
Gen
Gen1:13 text again with digits
Gennon digit line

But I only want them with lines 1:12 and 1:13.

What would you suggest ?

BTW I've got hundreds of lines !
~
~
~
~
~
~
~
~
~
~
~
~
~
 
Old 12-07-2014, 02:22 PM   #6
lwv962
Member
 
Registered: Feb 2011
Location: Wales
Distribution: MX Linux 18
Posts: 36

Original Poster
Rep: Reputation: 0
[SOLVED] command to add a word at the beginning of each line that starts with a digit

danielbmartin, many many thanks for your excellent and most
helpful response,it solved my problem 100%.
I have tried the awk but I'll get to the sed tomorrow.

lwv962
 
Old 12-08-2014, 07:26 AM   #7
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
The awk solution may be boiled down into fewer keystrokes if that sort of thing appeals to you. The ternary operator is difficult to read until you get used to it. The general form is:
Code:
if-this-is-true?then-do-this:else-do-this;
Note the ? : ; punctuation.

With this InFile ...
Code:
Now is   the     time
1:13 for all 26   good     men
to     come 4:40 to
2:02 the aid of       their party.
That's all, folks!
... this awk ...
Code:
awk '{/^[0-9]/?$0="banana "$0:0;}1' $InFile >$OutFile
... produced this OutFile ...
Code:
Now is   the     time
banana 1:13 for all 26   good     men
to     come 4:40 to
banana 2:02 the aid of       their party.
That's all, folks!
Daniel B. Martin
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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] long command line is wrapped at the beginning of the same line lwang3rock Linux - Newbie 10 12-28-2012 12:27 PM
Search word and delete only the word and the line using Sed command kbmukesh Linux - Newbie 4 06-28-2011 06:35 AM
how to add string on the beginning and end of the line packets Programming 2 08-03-2010 09:55 PM
How could i add to the beginning of line word? DoME69 Programming 6 09-17-2008 02:55 AM
add word to each line of the file? ziggie216 Linux - General 4 04-25-2005 04:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:44 AM.

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