Insert character at specific location in a each line of the file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert character at specific location in a each line of the file
# 1  
Old 10-06-2016
Oracle Insert character at specific location in a each line of the file

Hi All,


I am trying to write a shell script where it should insert character 'I' in 180th position of each line(except first and last line) of the file. Below is the script

Code:
for file in /home/test/bharat/*.RET
do
 # Process file
 echo "File Name=" $file
 #l_fileName="${file##*/}"
 l_fileName=`basename ${file}`_P
 mv $file $l_fileName
 echo "New File Name= " $l_fileName
 #Remove special chracters from the file
 sed 's/[!@#\$%^&*()]//g' $l_fileName>$file

 mv -f $l_fileName /home/test/bharat/archive
 
#Loop throguh all the lines in the file and insert I in 180 position 
COUNT=0
while read -r line; do
    COUNT=$(( $COUNT + 1 ))
	echo "line " $line
    echo "Inside the file line count: " $COUNT
	sed 's/./I/180' $line
done < $file

done

Somehow sed 's/./I/180' $line is not working properly. Please help to achieve this. and also i need to do this starting line 2 and should exclude last line.

Please help.

Thanks
Bharat
Moderator's Comments:
Mod Comment Please use CODE tags, as required by forum rules, when displaying sample input, sample output, and code segments.

Last edited by Don Cragun; 10-06-2016 at 10:37 PM.. Reason: Add CODE and ICODE tags.
# 2  
Old 10-06-2016
Hi,

Can you please try similar to below one ?Here i have simulated for 10th position in a file , excluding first and last line.

Code:
cat file
1234567890ABCD
1234567890XYZ
1234567890PQR
1234567890LMN

Code:
sed -e '1{n};$!s/./&a/10' file

Gives output:
Quote:
1234567890ABCD
1234567890aXYZ
1234567890aPQR
1234567890LMN
Hope it helps.
This User Gave Thanks to greet_sed For This Post:
# 3  
Old 10-06-2016
Hi greet,

When i try like this. I get an error:
Code:
sed: Function 1{n};$!s/./&I/10 cannot be parsed.

Below is the code i tried.

Code:
#Loop throguh all the lines in the file and insert I in 180 position 
COUNT=0
while read -r line; do
    COUNT=$(( $COUNT + 1 ))
	echo "line " $line
    echo "Inside the file line count: " $COUNT
	sed -e '1{n};$!s/./&I/10' line
done < $file

If i directly try this command on one single file it seems working. But my requirement is, it should happen for all the files in the directory..my files looks similar like below.


Code:
XXXXXXXXXXXXXXXXXXXXXXXXXXXXX       293800206774        XXXXXXXXXXXXXXXXX      
10210496781000100293800206774   %     30095                    15962835
10210496781000100293800206774   $     34099                    17054579
10210496781000100293800206774   #     999                      00000000

Thanks
Bharat

Last edited by Don Cragun; 10-06-2016 at 10:38 PM.. Reason: Add CODE tags.
# 4  
Old 10-06-2016
Please use code tags for code snippet and quote tags for text input/output.

Example i gave is for file and it refers to line such as first , exclude last line.

Code:
sed -e '1{n};$!s/./&I/10' line

is wrong here.

Is below are contents of file or file names ?

Quote:
XXXXXXXXXXXXXXXXXXXXXXXXXXXXX 293800206774 XXXXXXXXXXXXXXXXX
10210496781000100293800206774 % 30095 15962835
10210496781000100293800206774 $ 34099 17054579
10210496781000100293800206774 # 999 00000000
Here is what i can simulate with multiple files:

Code:
ls 
123.txt  456.txt  f1  f2

Code:
for file in *.txt
do
echo "File Name=" $file
sed '1{n};$!s/./&a/10' $file
done

Gives output :
Quote:
File Name= 123.txt
1234567890ABCD
1234567890aXYZ
1234567890aPQR
1234567890LMN
File Name= 456.txt
1234567890ccc
1234567890abbb
1234567890addd
1234567890eee
Once you are sure that this is what you want, you can use in-place edit option
Code:
-i

to update in the file itself. Please note , you can not undo .

Last edited by greet_sed; 10-06-2016 at 07:20 PM.. Reason: update code tags / add -i option
This User Gave Thanks to greet_sed For This Post:
# 5  
Old 10-06-2016
Hi Greet,

Thanks for the quick reply.Sorry i am new to shell scripting.

Below is the content of the file
Code:
XXXXXXXXXXXXXXXXXXXXXXXXXXXXX 293800206774 XXXXXXXXXXXXXXXXX 
10210496781000100293800206774 % 30095 15962835
10210496781000100293800206774 $ 34099 17054579
10210496781000100293800206774 # 999 00000000

and i tried to use the same way as you suggested.
Code:
for file in /home/user/bharat/*.RET
do
echo "File Name=" $file
sed '1{n};$!s/./&a/10' $file
done

and its giving me the below error.
Code:
File Name= /home/user/bharat/CN01066C.RET
sed: Function 1{n};$!s/./&a/10 cannot be parsed.
File Name= /home/user/bharat/CN01067C.RET
sed: Function 1{n};$!s/./&a/10 cannot be parsed.

Thanks
Bharat

Last edited by Don Cragun; 10-06-2016 at 10:39 PM.. Reason: Change QUOTE tags to CODE tags for sample input and output.
# 6  
Old 10-06-2016
Hi Greet,

Again i tried with .txt files. But i get similar error.

Quote:
$ sh test.prog
Program Begin
File Name= abc.txt
sed: Function 1{n};$!s/./&a/10 cannot be parsed.
File Name= def.txt
sed: Function 1{n};$!s/./&a/10 cannot be parsed.
Program End
I am attaching both the file used and shell script. Please help. Is it something to do with my unix version or something?

Thanks
Bharat
# 7  
Old 10-06-2016
Hi,

It might be your version of sed / unix.

Tested in below versions and works fine:
Code:
sed --version
sed (GNU sed) 4.2.2
GNU sed version 4.1.5

can you please give a try as follows, if it helps?
Code:
sed '1n;$!s/./&a/10' file

This User Gave Thanks to greet_sed 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

How to insert new line after a specific character in scripts?

Hi, I'm trying to add a new line after finding a specific String. That is my string: volumes: - ${DIR_WORK}/loadbalancer/html:/var/www/html and I want to change that file to: volumes: - ${DIR_WORK}/loadbalancer/html:/var/www/html extra_hosts: -... (4 Replies)
Discussion started by: siamak
4 Replies

2. Shell Programming and Scripting

Count specific character of a file in each line and delete this character in a specific position

I will appreciate if you help me here in this script in Solaris Enviroment. Scenario: i have 2 files : 1) /tmp/TRANSACTIONS_DAILY_20180730.txt: 201807300000000004 201807300000000005 201807300000000006 201807300000000007 201807300000000008 2)... (10 Replies)
Discussion started by: teokon90
10 Replies

3. Shell Programming and Scripting

Removing last character of a specific line from a file

Hello guys, I would need to remove the last character ")" of a specific line. This can be from any line. Your help is appreciated. Below is the line. HOSTNAME=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)) Please help. (6 Replies)
Discussion started by: sang8g
6 Replies

4. Solaris

Insert a file at specific line

Hi, Anyone can help me in Solaris command on how to insert a file at specific line. I want file1.sql content to be inserted on file2.sh after "recover database using backup controlfile until cancel". # file1.sql /archivelogs/927_822338133.arc /archivelogs/671_822338107.arc... (3 Replies)
Discussion started by: fspalero
3 Replies

5. Shell Programming and Scripting

Insert text line to specific location CSV

In Perl. ***edited question below*** Hey all, I am teaching myself some simple CSV file manipulation and have become a little stuck. Say I have the following layout in the CSV file: age,name,locationIs it possible to INSERT data into the CSV into the correct age order. For example, if I had... (1 Reply)
Discussion started by: whyte_rhyno
1 Replies

6. Shell Programming and Scripting

Help with replace character based on specific location

Hi, I got long list of reference file (column one is refer to the header in input file; column 2 is info of start position in input file; column 3 is info of end position in input file;) shown as below: read_2 10 15 read_3 5 8 read_1 4 10 . . . Input file (huge file with total... (6 Replies)
Discussion started by: perl_beginner
6 Replies

7. Shell Programming and Scripting

Insert character in a specific position of a file

Hi, I need to add Pipe (|) at 5th and 18th position of all records a file. How can I do this? I tried to add it at 5th position using the below code. It didnt work. Please help!!! awk '{substr($0,5,1) ~ /|/}{print}' $input_file > $temp_file (1 Reply)
Discussion started by: gpaulose
1 Replies

8. Shell Programming and Scripting

Replace, insert n times a specific character

How can using Vim, replace one character with another repeating it 10 times? Ex.: Transforming this: 125A986 That: 125##########986 (12 Replies)
Discussion started by: IJNeves
12 Replies

9. Shell Programming and Scripting

Insert 2 lines in a file at a specific location

Hi, I need to insert two new lines in a file: The file: "..... ...... ULIMIT_MAX_FILES="ulimit -S -n `ulimit -H -n`" .... .... " I need to add the lines: LD_LIBRARY_PATH='$LD_LIBRARY_PATH:$APACHE_HOME/modules' DOWNLOADMODULE_CONF_PATHNAME='$APACHE_HOME/conf/DWLModule.cfg' right... (2 Replies)
Discussion started by: potro
2 Replies

10. Shell Programming and Scripting

Insert lines at specific location in file

Hi There I have this file that I would like to add entries to, however, there is a "}" as the last line that I need to keep. Basically i would like to know how I can write a script that will add new lines at the second to last line position (ie always add new line above the close bracket) ... (17 Replies)
Discussion started by: hcclnoodles
17 Replies
Login or Register to Ask a Question