Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.
Adding a new string in a file
Name: vijay.goriya Date: March 6, 2008 at 03:51:50 Pacific OS: Unix CPU/Ram: 512 Product: dell
Comment:
HI,
My file contains the data like john smitch Tracy -tcopse I have to add a new string "PETER"at the 4th posion so the file shud contain the data like: john smitch Tracy PETER -tcopse please lemme know how it can be done...
Name: nails Date: March 6, 2008 at 06:57:00 Pacific
Reply:
One of the other guys will give you an awk or perl solution, but this one uses the shell:
#!/bin/ksh
newvar="PETER" while read f1 f2 f3 fr do echo "f1 $f2 $f3 $newvar $fr" done < file.txt
0
Response Number 2
Name: Rajam Date: March 7, 2008 at 02:48:23 Pacific
Reply:
This statement helps you to add a new string in a file. cat filename | awk '{print $1,$2,$3,"PETER",$4}' >Output
Thanks Rajam
0
Response Number 3
Name: nails Date: March 7, 2008 at 09:59:06 Pacific
Reply:
Rajam:
Your awk script works only for lines with 4 fields. If more the 4 fields exist, then the fields need to be shifted. (My original shell solution doesn't have this issue). This script works for any number of fields:
#!/bin/ksh
awk ' BEGIN { pos=4; newvar="PETER" } { for(i=1;i<=NF;i++) { if (i == pos) { savevar=$pos $pos=newvar }
if (i > pos) { buffer=$i $i=savevar savevar=buffer }
if (i == NF) savestr=$0" "savevar } print savestr } ' filename
0
Response Number 4
Name: vijay.goriya Date: March 12, 2008 at 01:20:16 Pacific
Reply:
Thnx alot Nails...this is what i was looking for...
Summary: hello, may i know how can i find and change string in multiple file...i had refer some of the post here .. but found that all is about find and change string in a file only..let say i want to change a...
Summary: I want to insert a string in a template txt file. Let my explain. Let suppose U have a template. Name it "pp.txt" with this structure: [send] . .<--- I want insert a string here. .<--- or here. ...
Summary: The short story is I want to find a string in a file and change it. Here are the details. I'm looking for a way to change every occurence of $source to "JavTemp" in a file called JavTemp2.java. $so...