wrwe
November 1, 2008, 8:19pm
1
Hello, i'm new with the scripting on unix and i need such script or maby something to that way:
I need to change the the first word in every new line to a given word. Using just /bin/sh ( not using sed, awk, perl and ect).
I would be very grateful.
Look like a homework, please read the The UNIX and Linux Forums - Forum Rules .
You can use this basic example:
while read i eol
do
echo new_word "$eol" >> new.file
done < old.file
mv new.file new.file
wrwe
November 4, 2008, 11:54am
3
#/bin/sh
while read firstword restofline
do
echo $firstword
firstword="word"
done < datafile.txt
#
This part just changes first words, how to glue the first word ant the rest of the line ?
danmero
November 4, 2008, 12:00pm
4
Base on your example
#/bin/sh
while read firstword restofline
do
echo "word" "$restofline" >> new_file
done < datafile.txt
#
Finlay replace datafile.txt by new_file
mv new_file datafile.txt
wrwe
November 9, 2008, 2:36pm
6
I'm sorry, but i'm curious about writting it usig set and awk, can enyone give me a hint how to begin?
neword=new_word
sed -i "s/^\([a-z]*\)/$neword/g" file
wrwe
November 9, 2008, 4:22pm
8
thanks
sed -i "s/^\([a-zA-Z0-9]*\)/word/g" file
But where it says that replace just the first word in the every line. I now /g - global for each line, ^ - symbolizes the begining. [a-zA-Z0-9] - that to replace. *- everything exept space. What should i change to change let's say the second word in each line. If you add number let say 2 before g and delete ^ it will change everything exepct the first word in every line. Is ^ that does the job?
IMHO awk is a better tool for this job:
awk '{$1="NewFirstWord"}{print}' file
or:
awk '{$2="NewSecondWord"}{print}' file
use awk
awk ' { print "yourword" $2 $3.....} ' filename > outputfile
however this will work if the file has all lines with same no of columns