Substitute particular char in a file


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Substitute particular char in a file
# 1  
Old 03-03-2017
Substitute particular char in a file

I have a file like this.

Code:
1 aaa bcd  1 56 xyz 1
2 ccc  rrr   1 25  512 1
1 zaz eee   1 55  511 1

I want to change middle 1's ie after bcd,rrr,eee to 0 where as other 1's
should not change.
Can you please provide a solution .

Last edited by Corona688; 03-03-2017 at 12:47 PM..
# 2  
Old 03-03-2017
Code:
awk '$1 ~ /^(bcd|rrr|eee)$/ { $4=0 } 1' inputfile > outputfile

# 3  
Old 03-03-2017
substitution of particular char

Thanks.
But the file is big one having 5000 records. I want to change only 154 th
char where the file is having 306 chars.
# 4  
Old 03-03-2017
Quote:
Originally Posted by kshari8888
Thanks.
But the file is big one having 5000 records. I want to change only 154 th
char where the file is having 306 chars.
I'm lost. If you have a file with 5000 records, how can the file contain only 306 characters???

Are you saying that each record in your file has a fixed length of 306 characters (or bytes) and that you want to change the character in character position 154 from 1 to 0 if the "word" before that position is one of the three strings you specified in post #1?

Note that the sample input you provided does not have a total of 306 characters, does not have 306 character lines, does not have fixed length lines (records), and does not have the 1 that you want to change in a fixed position.

Are you just looking for a single character 1 following one of your three specified strings? Or, does it have to be a 1 followed by a <space> (so multi-digit numbers starting with a 1 as the 1st digit would not changed to have a leading 0)?

Do the varying numbers of <space> characters between fields in your lines have to be maintained? Or can strings of multiple adjacent <space>s be coalesced into a single <space>?

Is the 1 you want to change always the 4th field and the strings you are looking for always the 3rd field in a line (as in your sample input)?
# 5  
Old 03-04-2017
The file has 5000 records.
Each record has 306 chars.All records have fixed length.
At 154 th char if it is 1 it should be changed to 0. Otherwise no change.
This is the problem, Can you please provide a solution.
# 6  
Old 03-04-2017
Can you please provide a small representative sample input file (containing some records that should be changed and some that should not) and also provide the exact output file that you want to produce from that input?

Are the three strings that you're looking for in a record also in a fixed position within the record? If so, in what three character positions do they appear? (They aren't in a fixed position in the sample you provided in post #1; but neither was the 1 that you want to change to a 0.)

What, if anything, delimits records in your file? For example, is a record composed of 305 non-<newline> characters and a <newline> record terminator?
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 substitute a word in multiple file?

Team, I want to change below parameter in all the files in a directory, Check for HOSTNAME=`hostname` Change to HOSTNAME=localhost And I tried below but, its not working ☹ find /tmp -type f -exec sed 's/"HOSTNAME\=\`hostname\`"/"HOSTNAME\=localhost/g'" Help me if I am missing... (6 Replies)
Discussion started by: natraj005
6 Replies

2. Shell Programming and Scripting

Substitute special Characters into a file

Hi experts :) I need to replace special characters into a file , in the followiing way : " --> "" ' --> '' _--> \_ I tried with the sed command but I'm getting and error ksh: $: not found. ksh: $: not found. sed: Function s/\/\/ cannot be parsed. Any idea ? Thanks , KOLAS... (2 Replies)
Discussion started by: Kolas79
2 Replies

3. UNIX for Dummies Questions & Answers

Help with AWK - Compare a field in a file to lookup file and substitute if only a match

I have the below 2 files: 1) Third field from file1.txt should be compared to the first field of lookup.txt. 2) If match found then third field, file1.txt should be substituted with the second field from lookup.txt. 3)Else just print the line from file1.txt. File1.txt:... (4 Replies)
Discussion started by: venalla_shine
4 Replies

4. Programming

error: invalid conversion from ‘const char*’ to ‘char*’

Compiling xpp (The X Printing Panel) on SL6 (RHEL6 essentially): xpp.cxx: In constructor ‘printFiles::printFiles(int, char**, int&)’: xpp.cxx:200: error: invalid conversion from ‘const char*’ to ‘char*’ The same error with all c++ constructors - gcc 4.4.4. If anyone can throw any light on... (8 Replies)
Discussion started by: GSO
8 Replies

5. Shell Programming and Scripting

How to substitute in a large gz file?

I have a large .gz file where i wish to replace the occurrence of a particular word with another word.. Can anyone help?I prefer a short script.. Can sed work with gz files? (5 Replies)
Discussion started by: maximus8886
5 Replies

6. Shell Programming and Scripting

Using awk to substitute columns from one file into another

Hi, I am new to this forum and my script skills are very weak. I have this file (file 1) that contains 3 columns (tab delimited): kyle start stop john start stop joe start stop And I want to replace name (column 1) from another file. This other file has two columns, one is the... (4 Replies)
Discussion started by: kylle345
4 Replies

7. UNIX for Dummies Questions & Answers

substitute file name

correct file names are: *_0.txt *_1.txt incorrect file names are: *_12.txt *_0123.txt *_04321.txt all files that are incorrect need to replace the ending with *_1.txt therefore need to create a loop to find the wrong files in a dir ->that dont end in _1.txt or _0.txt and then... (3 Replies)
Discussion started by: sigh2010
3 Replies

8. Shell Programming and Scripting

substitute a line in file

i have an file ,i want to substitute line 003 M 33 22 22 00 WITH NEW 003 M 24 26 28 00 how can i do it (2 Replies)
Discussion started by: RahulJoshi
2 Replies

9. Shell Programming and Scripting

Substitute File name

Hi all I am in a small problem pl help me out. I am having a directory having ZIP files with name starting as : 01.xyz 02.pqr and so on I want to run the script- cat myfile | awk '{print $1, $2}' | while read var1 var2 do zcat $var2* | grep "^000$var1" >> my_output done Where the... (22 Replies)
Discussion started by: vanand420
22 Replies

10. Shell Programming and Scripting

how to substitute more than one word in a text file?

well i have this file here: <XML> <pregate xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <system_c>HPREGATE</system_c> <trans_c>HSPG</trans_c> <trans_dt>20060105161333</trans_dt> <user_id_m></user_id_m> <func_c>C</func_c> </pregate> </XML> i want to... (2 Replies)
Discussion started by: forevercalz
2 Replies
Login or Register to Ask a Question