LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   command to add a word at the beginning of each line that starts with a digit (https://www.linuxquestions.org/questions/programming-9/command-to-add-a-word-at-the-beginning-of-each-line-that-starts-with-a-digit-4175527603/)

lwv962 12-07-2014 11:57 AM

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.

pan64 12-07-2014 12:13 PM

so what have you tried? what do you prefer (perl/awk/sed/python....)?

danielbmartin 12-07-2014 12:20 PM

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

danielbmartin 12-07-2014 12:27 PM

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

lwv962 12-07-2014 01:51 PM

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 !
~
~
~
~
~
~
~
~
~
~
~
~
~

lwv962 12-07-2014 02:22 PM

[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

danielbmartin 12-08-2014 07:26 AM

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


All times are GMT -5. The time now is 07:21 PM.