Operating System - HP-UX
1753816 Members
8452 Online
108805 Solutions
New Discussion юеВ

Re: Awk expert's help needed (how to edit a text file?)

 
SOLVED
Go to solution
Hanif Kazemi
Occasional Advisor

Awk expert's help needed (how to edit a text file?)

Hi,

I have a text file and I'd like to use awk to edit it. I'd like it to go to the line 18 and from there add the phrase "/work/" to the beginning of each line.

AWK/GAWK experts, I request your help and appreciate your response.

Hanif Kazemi.
14 REPLIES 14
Peter Godron
Honored Contributor
Solution

Re: Awk expert's help needed (how to edit a text file?)

Hanif,
the end result is still the same ;-) :
If the datafile is called a.lis

grep "18,$ s/^/\/work\//" a.lis > b.lis
mv b.lis a.lis

Regards
harry d brown jr
Honored Contributor

Re: Awk expert's help needed (how to edit a text file?)

Y use awk when something as simple as "ex" will do?

ex file <18
s_^_\/work\/_
:wq
EOS


live free or die
harry d brown jr
Live Free or Die
harry d brown jr
Honored Contributor

Re: Awk expert's help needed (how to edit a text file?)

Now if you want to add /work/ to line 18 and every line after line 18, then modify my first post to:

ex file <18
s_^_\/work\/_g999999
:wq
EOS

I would imagine awk to be thousands of times slower than ex for such a simple task.

Additionally with "ex" you don't need a temporary file to write the changes to and you don't need to move the new file over on top of the original file!


live free or die
harry d brown jr
Live Free or Die
Peter Godron
Honored Contributor

Re: Awk expert's help needed (how to edit a text file?)

Hanif,
sorry about my earlier answer:
please replace grep with sed
Regards
Hanif Kazemi
Occasional Advisor

Re: Awk expert's help needed (how to edit a text file?)

couple points:

1: I want the phrase "work" to be added to lines that contains "htm" and are in lines 18 and after.

2: Harry, what language is that?

Thanks very much.
Patrick Wallek
Honored Contributor

Re: Awk expert's help needed (how to edit a text file?)

For Harry's answers, do a 'man ex'. It is not a language it is a shell command. The man page will tell all about it.
Rodney Hills
Honored Contributor

Re: Awk expert's help needed (how to edit a text file?)

Hanif,

As a courtesy, point assignment is the accepted way to say thank-you and indicate which response(s) was the best solution.

-- Rod Hills
There be dragons...
harry d brown jr
Honored Contributor

Re: Awk expert's help needed (how to edit a text file?)


OK. It's not a LANGUAGE, it is a line editor:

man ex

where does "htm" exist? (at the beginning of the line, at the end of the line, in the middle?)

live free or die
harry d brown jr





Live Free or Die
Hanif Kazemi
Occasional Advisor

Re: Awk expert's help needed (how to edit a text file?)

Sorry.

I had no idea about the point system. I am pretty new to the Forum.

To clearify my question:

1-I have a text file, let's say a.txt
2-In line 18 and after, I'd like to add a phrase(let's say " /work/ " to the beginning of the line.

Thanks everyone again.