Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

change file inserting a character from a line 2

Status
Not open for further replies.

Yarka

Technical User
Jan 14, 2007
192
ES
hi,
i need to change a file. My file is as:
kkkkkk
...
aaaaaaaaa
bbbbbbbbb
aaa
sssss
ggg

and my new file must be as:
kkkkkk
.....
P aaaaaaaaa
P bbbbbbbbb
P aaa
P sssss
P ggg

i.e, from a line I need to insert a caracter at the beginning. I know this is very easy, but I am not very get used ... but I want to learn :)

txs.
 
What are the rules ?
Id est, how the awk program is supposed to know from which line it should insert which character ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I want to do this change in that file, but i don't know if awk
is appropiate for this. I don´t know how I can do this.

txs
 
How you can do WHAT ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
then, how can i add characters from a fixed line?
For example, if i've this file:
aa
bb
dd
ee
ff

i need to have from line third a 'x'. For this reason i need to have this output:
aa
bb
x dd
x ee
x ff

Forgive me, if awk forum doesn't appropiate for post this doubt.

Thanks.
 
awk '{x=(NR>=3?"x ":"")$0;print x}' /path/to/input

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi

Hmm... Now your problem seems to be solvable with almost anything :
Code:
awk 'NR>=3{printf"x" OFS}1' /input/file > /output/file

[gray]# or[/gray]

sed '3,$s/^/x /' /input/file > /output/file

[gray]# or[/gray]

perl -pe 's/^/x / if $.>=3' /input/file > /output/file
But I do not understand the provenience of "x". If it is a hardcoded value, then Ok, otherwise you really have to tell us more.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top