Problem with replacing chars in file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Problem with replacing chars in file
# 1  
Old 09-28-2012
Problem with replacing chars in file

my file is :

hello1
hello2
hello3_B,dummy,N


Need to change to :

hello1
hello2
hello3_B,dummy_01,N


Can anyone help? Thank you.

Moderator's Comments:
Mod Comment Please use code tags next time for your code and data.

Last edited by vbe; 09-28-2012 at 05:38 AM..
# 2  
Old 09-28-2012
Problem with replacing chars in file

My file is :

hello1
hello2
hello3_B,AAA
dummy,N

How to replace the third line dummy in the file to the below

hello1
hello2
hello3_B,AAA
dummyA,N

Anyone can help.Thanks.
# 3  
Old 09-28-2012
is this only one change..?

use sed
Code:
sed 's/dummy/dummy_01/g'



need some more details if this you don't want..
This User Gave Thanks to pamu For This Post:
# 4  
Old 09-28-2012
Code:
sed '3s/dummy/&A/' yourfile > temp
mv temp yourfile

This User Gave Thanks to elixir_sinari For This Post:
# 5  
Old 09-28-2012
Thanks a lot...
what if the file also contain chars that is not fixed can also be :
file1:
hello1
hello2
hello3_B,AAAtest1,N

or
file2:
hello1
hello2
hello3_B,AAAtest2V,N

or

file3:
hello1
hello2
hello3_B,,N

and need to retain all the content except to change the test* or empty to replace as dummy

file1:
hello1
hello2
hello3_B,AAAdummy,N

file2:
hello1
hello2
hello3_B,AAAdummy,N

file3:
hello1
hello2
hello3_B,dummy,N

Last edited by Alyssa; 09-28-2012 at 03:47 AM..
# 6  
Old 09-28-2012
Thanks a lot...
what if the file also contain chars that is not fixed as below example :
file1:
hello1
hello2
hello3_B,AAAtest1,N

or
file2:
hello1
hello2
hello3_B,AAAtest2V,N

or

file3:
hello1
hello2
hello3_B,,N

and need to retain all the content except to change the test* or empty to replace as dummy

file1:
hello1
hello2
hello3_B,AAAdummy,N

file2:
hello1
hello2
hello3_B,AAAdummy,N

file3:
hello1
hello2
hello3_B,dummy,N

Moderator's Comments:
Mod Comment Please use code tags next time for your code and data.

Last edited by vbe; 09-28-2012 at 05:38 AM..
# 7  
Old 09-28-2012
try with awk..

Code:
awk -F, '{if($2 ~ /AAA/) {$2="AAAdummy"}else if($2 == "" && NF > 2) { $2 = "dummy"}}1' OFS=, file

with sed....

Code:
sed 's/AAA[a-zA-Z0-9\t]\+,/AAAdummy,/; s/,,/,dummy,/' file

This User Gave Thanks to pamu For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell script to split data with a delimiter having chars and special chars

Hi Team, I have a file a1.txt with data as follows. dfjakjf...asdfkasj</EnableQuotedIDs><SQL><SelectStatement modified='1' type='string'><! The delimiter string: <SelectStatement modified='1' type='string'><! dlm="<SelectStatement modified='1' type='string'><! The above command is... (7 Replies)
Discussion started by: kmanivan82
7 Replies

2. Shell Programming and Scripting

Problem wth spec chars in script

Hello I would like to make a script which will get the line from file list (ex. passkey) and put it into further processing. The main problem is that lines, in text file contains all specials characters, and whitespaces too, as these that was used as a extremmaly-safe passwords. I have written... (4 Replies)
Discussion started by: elxa1
4 Replies

3. UNIX for Dummies Questions & Answers

Replacing multiple special chars with single char

Hi I've a string . And i need to replace set of characters with a single character Means .. or . or ... and so on should be replaced with single % character Irrespective of number of dots in between the characters , those should be replaced with single % All the above strings should be... (3 Replies)
Discussion started by: smile689
3 Replies

4. Shell Programming and Scripting

Removing unknow chars from file names.

I'm trying to move a large folder to an external drive but some files have these weird chars that the external drive won't accept. Does anyone know any command of any bash script that will look through a given folder and remove any weird chars? (4 Replies)
Discussion started by: Joktaa
4 Replies

5. UNIX for Dummies Questions & Answers

Splitting a file based on first 8 chars

I have an input file of this format <Date><other data> For example, 20081213aaaaaaaaa 20081213bbbbbbbbb 20081220ccccccccc 20081220ddddddddd 20081220eeeeeeeee 20081227ffffffffffffff The first 8 chars are date in YYYYMMDD formT. I need to split this file into n files where n is the... (9 Replies)
Discussion started by: paruthiveeran
9 Replies

6. Shell Programming and Scripting

find 4 chars on 2nd line, 44 chars over

I know this should be simple, but I've been manning sed awk grep and find and am stupidly stumped :( I'm trying to use sed (or awk, find, etc) to find 4 characters on the second line of a file.txt 44-47 characters in. I can find lots of sed things for lines, but not characters. (4 Replies)
Discussion started by: unclecameron
4 Replies

7. UNIX for Advanced & Expert Users

Problem in FTP of file with chinese chars

Hi, I have a file in my PC with Chinese (PRC) characters. When I transfer that file into my AIX5.3 server through FTP, I get junk characters instead of Chinese chars. Last week I copied the content from my notepad and pasted in vi editor. The Chinese characters were correct, but when I try now,... (4 Replies)
Discussion started by: carthyc
4 Replies

8. UNIX for Dummies Questions & Answers

Using SED to get n chars after given value from file

Hello, my name is Marc, I'm a linux starter :) and I hope you specialists can help me solving this issue. I have a file containing a lot of data. Somewhere in this file, there's a string called "Faultdump", directly followed by 64 chars of HEX data. I need to get the HEX part. I accomplished... (12 Replies)
Discussion started by: Kally
12 Replies

9. Shell Programming and Scripting

How to convert C source from 8bit chars to 16bit chars?

I was using the following bash command inside the emacs compile command to search C++ source code: grep -inr --include='*.h' --include='*.cpp' '"' * | sed "/include/d" | sed "/_T/d" | sed '/^ *\/\//d' | sed '/extern/d' Emacs will then position me in the correct file and at the correct line... (0 Replies)
Discussion started by: siegfried
0 Replies

10. Shell Programming and Scripting

to append few chars at the end of a file

hi i want to open a file at runtime append few chars at the end of each line all these i want to have done automatically how to do it (2 Replies)
Discussion started by: trichyselva
2 Replies
Login or Register to Ask a Question