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. |
so what have you tried? what do you prefer (perl/awk/sed/python....)?
|
With this InFile ...
Code:
Now is the time Code:
awk -F "" '{if ($1~/[0-9]/) $0="grapefruit "$0}1' $InFile >$OutFile Code:
Now is the time Code:
AW="pomegranate" # AW = Added Word. Code:
Now is the time |
With this InFile ...
Code:
Now is the time Code:
sed 's/\(^[0-9]\)/peach \1/' $InFile >$OutFile Code:
Now is the time |
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 ! ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ |
[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 |
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; With this InFile ... Code:
Now is the time Code:
awk '{/^[0-9]/?$0="banana "$0:0;}1' $InFile >$OutFile Code:
Now is the time |
All times are GMT -5. The time now is 07:21 PM. |