editing a character in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting editing a character in a file
# 1  
Old 04-23-2008
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
# 2  
Old 04-23-2008
If you know the line number, use that.

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

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

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

# 3  
Old 04-23-2008
MySQL

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
# 4  
Old 04-23-2008
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
Code:
sed -i "1 s/.*/2/" file

i love it when something works Smilie
# 5  
Old 04-23-2008
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'
Smilie
# 6  
Old 04-23-2008
Code:
 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.
# 7  
Old 04-23-2008
Quote:
Originally Posted by danmero
Code:
 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 Smilie

anyway, thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to remove newline character if it is the only character in the entire file.?

I have a file which comes every day and the file data look's as below. Vi abc.txt a|b|c|d\n a|g|h|j\n Some times we receive the file with only a new line character in the file like vi abc.txt \n (8 Replies)
Discussion started by: rak Kundra
8 Replies

2. Shell Programming and Scripting

Convert vi editing to text editing

Dear Guru's I'm using Putty and want to edit a file. I know we generally use vi editor to do it. As I'm not good in using vi editor, I want to convert the vi into something like text pad. Is there any option in Putty to do the same ? Thanks for your response. Srini (6 Replies)
Discussion started by: thummi9090
6 Replies

3. Shell Programming and Scripting

File character adjustment based on specific character

i have a reqirement to adjust the data in a file based on a perticular character the sample data is as below 483PDEAN CORRIGAN 52304037528955WAGES 50000 89BP ABCD MASTER352 5434604223735428 4200 58BP SOUTHERN WA848 ... (1 Reply)
Discussion started by: pema.yozer
1 Replies

4. Shell Programming and Scripting

editing line in text file adding number to value in file

I have a text file that has data like: Data "12345#22" Fred ID 12345 Age 45 Wilma Dino Data "123#22" Tarzan ID 123 Age 33 Jane I need to figure out a way of adding 1,000,000 to the specific lines (always same format) in the file, so it becomes: Data "1012345#22" Fred ID... (16 Replies)
Discussion started by: say170
16 Replies

5. Shell Programming and Scripting

Help with file editing while keeping file format intact

Hi, I am having a file which is fix length and comma seperated. And I want to replace values for one column. I am reading file line by line in variable $LINE and then replacing the string. Problem is after changing value and writing new file temp5.txt, formating of original file is getting... (8 Replies)
Discussion started by: Mruda
8 Replies

6. Shell Programming and Scripting

read the text file and print the content character by character..

hello all i request you to give the solution for the following problem.. I want read the text file.and print the contents character by character..like if the text file contains google means..i want to print g go goo goog googl google like this Using unix Shell scripting... without using... (1 Reply)
Discussion started by: samupnl
1 Replies

7. Shell Programming and Scripting

Breaking a file into three new files, character by character

I am new to shell scripting, and need a script to randomly distribute each character from a file into one of three new files. I also need each character to maintain it's position from the original file in the new file (such that if a character is written to File 1, Files 2 and 3 have spaces... (10 Replies)
Discussion started by: foxcastle
10 Replies

8. Shell Programming and Scripting

Deleting all characters from 350th character to 450th character from the log file

Hi All, I have a big log file i want to delete all characters (between 350th to 450th characters) starting at 350th character position to 450th character position. please advice or sample code. (6 Replies)
Discussion started by: rajeshorpu
6 Replies

9. Shell Programming and Scripting

read in a file character by character - replace any unknown ASCII characters with spa

Can someone help me to write a script / command to read in a file, character by character, replace any unknown ASCII characters with space. then write out the file to a new filename/ Thanks! (1 Reply)
Discussion started by: raghav525
1 Replies

10. AIX

check for a particular character inside a file and substitute with a given character?

i am a newbie to shell script,so i want a kshell script in which i need to check for a particular character inside a file through conditional looping(like if ,case,while)and if that character exists ,then substitute a given character to that character. consider a file test.txt,inside the file... (1 Reply)
Discussion started by: karthikprasathk
1 Replies
Login or Register to Ask a Question