editing a character in a file

hello, nice forum you have here, glad i joined

i have a problem with inline editing via scripts and i was hoping one of you smarter gents could help me out. i'm just doing my baby steps so bare with me if you can.

so i have an input file which goes something like this:
6
5
6
3
i'd like to be able to change a number, like say the first 6 to 2. i played around with sed lie so
sed '1!d' file | sed "s/.*/2/"
but how can i put this to the same line i want to edit?
also i searched the forum for solutions, and saw things in perl, i don't want to go that far just for a solution (since i don't know the first thing about perl). basic bash or awk would be the best.

thanks

If you know the line number, use that.

sed '1/s/6/2/' file

If you just want the first line with a 6 then maybe awk is simpler.

awk '/^6$/ && done==0 { print 2; done=1 } { print }' file

Hi,
i didn't get your question, i would like to give a sugestion, it would be always nice you can show the expected output as the part of question.
so that things becomes more clear.i suppose that you like to see the changes made by sed in the s=input file.

    the syntax for that is 

sed 's/pattern1/pattern2/g' -i file
or
sed 's/pattern1/pattern2/g' -i.back file

the later one would create a back up of the old file

the command above replaces pattern1 to pattern2 all over the file

the 's' used in sed represent the operation substitute you can use 1s , 6s to substitute only that line

best Regards,
Rakesh UV

thanks everyone for the help, it's working now

my objective (through an example) was to get from (contents of "file"):
6
5
6
3

to:
2
5
6
3

and i was able to do it with

sed -i "1 s/.*/2/" file

i love it when something works :slight_smile:

what if you don't know the line of the text you wanna edit?

Like you 'search' for it first then editing or deleting it?

Example

You
Me
We
Them

then I wanted to change 'Me' to 'She'
:frowning:

 sed s/Me/She/1 data.file

Hi Imajean , You are welcome.
Please read this page for basic sed examples: Gentoo Linux Documentation -- Sed by example, Part 1
And next time start your own tread !!! Forum Rules.

I did start my own thread :confused:

anyway, thanks

It means to click the "New Thread" button on the forum's overview page, as opposed to "Post Reply" to somebody else's thread with an unrelated question.