Reading a file and replacing char by position


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading a file and replacing char by position
# 1  
Old 04-19-2011
Reading a file and replacing char by position

Hi
I'm looking for a way to read a text file that may contain 1000 records or more and each of these records has 460 characters. I need to read each record, and add a string of characters starting at position 256 for each record. Any suggestions using UNIX shell scripting.
# 2  
Old 04-19-2011
See if this works for you:
Code:
sed 's/.\{255\}/&YourString/' Inp_File

# 3  
Old 04-19-2011
Awesome! Thanks Shell_Life, one thing though as I inserted these 4 characters my data shifted to the right 4 spaces. Anyway to do this without shifting the data over??
# 4  
Old 04-19-2011
You asked to add/insert a string, this way the original data is incremented by the string and thus shifted.

If you do not want to have the data shifted, then you want to replace:
Code:
sed 's/\(.\{255\}\)..../\1ABCD/' Inp_File

This User Gave Thanks to Shell_Life For This Post:
# 5  
Old 04-19-2011
Awesome!!! Thank you very much Shell_Life, that worked like a champ. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Help with replacing a char

Hello All, I have a file as below . I want to convert the Y with numbers to H From 4, M11, P2521759, Y75,Y70,Y105,Y110,Y700,Y815,Y830,Y900,Y162,Y300, Y291,Y290,Y15,Y20, MR2716014,MR2617014, Yesterday,current 1,201012, 102032,1 11112,0 to 4, M11, P2521759,... (2 Replies)
Discussion started by: arunkumar_mca
2 Replies

2. 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

3. Shell Programming and Scripting

Insert carriage return on the 10th char position of each line

Hi experts, Need your help on how to insert carriage return after the 10th char position of each line in a file and then add two blank spaces after the carriage return. Example: >cat test.txt testingline dummystring samplesample teststringline Expected output should be.. ... (2 Replies)
Discussion started by: brichigo
2 Replies

4. UNIX for Dummies Questions & Answers

Replacing char in filename scripts fails

Hi I'm trying to remove what I "think" is a bad character. How I got the bad character is when I downloaded jpgs onto my PC and then renamed the files using windows explorer. In cygwin, the files look like $ dir -l total 7840 ----------+ 1 None 3647968 Jul 21 08:41 2012-07-21\ (1).JPG... (6 Replies)
Discussion started by: SailingDreams
6 Replies

5. Shell Programming and Scripting

reading Un Printable char in Filter

hi, i have a file with delimiter "þ" . how to read these delimiters filters like AWK and cut..:wall: Thanks, Soma (1 Reply)
Discussion started by: challamsomu
1 Replies

6. Shell Programming and Scripting

AWK: list files with 1rst col=N and char position 123=N

I need to list all files where 1rst column=ABK and char position 123 to 125=ZBK: For the first part I can I can do a awk '{$1="ABK";print}' file and for the second a cut -c123-125 file | grep ZBK but this would only work partially.. How can I do this with only one awk command ? Thanks in... (10 Replies)
Discussion started by: cabrao
10 Replies

7. Shell Programming and Scripting

Replacing one Char in a string of variable length

Hi all, I am trying to find the best way of making a change to 1 char in a string, the string can be between 1 and 14 characters. I am reading a line in from a file which contains 012341231231:2:102939283:NNN: Require :NBN: 012838238232:3:372932:NNN: Require :NNB: I need to change 1 N or a... (8 Replies)
Discussion started by: nkwilliams
8 Replies

8. Shell Programming and Scripting

Replacing a position in a file

Hi I'm trying to use awk in a file(test123.dat). My requirement is to to check for the 65th position, if the 65th position is a space then replace the 65th position by the number 9. This is the code that i used: awk '{substr($0,65,1) ~ / / }{sub(substr($0,65,1),"9")}{print}' test123.dat ... (7 Replies)
Discussion started by: angelarosh
7 Replies

9. Programming

replacing char with string

how we can replace char with a string example char *a="a.s" so finally what i ant to do raplace a with ant and s sree so in my array a i want to store the value as "ant.sree" thank u in advance (1 Reply)
Discussion started by: phani_sree
1 Replies

10. UNIX for Dummies Questions & Answers

replacing all 4 first upper char of every rec to lowercase ?

I have a file where some records have been updated the wrong way and need to fix it quickly since the amount can be alot. Every record where any of the first 4 characters are in upper case need to be changed to lowercase. Records can have '#' in position-1 for comments. These musn't be... (2 Replies)
Discussion started by: Browser_ice
2 Replies
Login or Register to Ask a Question