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
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.
#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.
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.
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.
If you want to save a backup / copy of the file before modifying it, use
sed -i.bak '1n;$!s/./&a/10' $file
After execution of above command with -i.bak option, you have 2 files now.
1) before modification with .bak extension.
2) Modified file by sed command .
If we just stick with standard sed addressing constructs and do not depend on non-standard options, you could try something more like:
#!/bin/ksh
IAm=${0##*/}
tmpf="$IAm.$$"
trap 'rm -f "$tmpf"' EXIT
cd /home/user/bharat
for file in *.RET
do printf 'File Name=%s\n' "$file"
sed -e 1n -e '$n' -e 's/./I&/180' "$file" > "$tmpf" && cp "$tmpf" "$file"
done
Note that your description talks about INSERTING a character in the 180th position which I interpret to mean "insert a character before the 180th character on the existing line" while none of your sample input files have 180 characters in any line and all of the samples that have shown examples using character position 10 instead of 180 have been "appending a character after the 10th character" instead of "inserting a character before the 10th character. The above code attempts to "insert a character before the 180th character on lines 2 through n-1, inclusive, for a file that contains n lines.
The code samples you have shown unconditionally move the output produced by sed to the input file whether or not sed succeeded or failed. The above code only modifies the input file if the sed command completes successfully. Furthermore, it copies the updated file back to the original file (which preserves file permissions and hard links) while the moves used by your script will break any hard links to your input files and may destroy the contents of your input file if sed fails for any reason.
Hopefully, this will work better for you.
Note that it is always a good idea to tell us what operating system and shell you're using so the volunteers here trying to help you can avoid suggestions that won't work in your environment.
The previous script was written and tested using a Korn shell, but will work with any shell that is based on Bourne shell syntax and performs parameter expansions required by the POSIX standards (such as ash , bash , dash , ksh , and zsh ).
I tested this and works perfectly fine. I have one more requirement with the same operation. If 180th position already have character "I". i do not want to touch that file. I am not getting how to handle that with sed.
---------- Post updated at 12:05 PM ---------- Previous update was at 11:37 AM ----------
It works. Just fine. Thanks a lot.
for file in *.RET
do
echo "File Name in PROCESS Stage=" $file
#Insert character 'I' at 180th position if this chracter not exist
#sed -e 1n -e '$n' -e 's/./I&/180' "$file" > "$tmpf" && cp "$tmpf" "$file"
sed -e 1n -e '$n' -e '/^.\{179\}I/ !s/./I&/180' "$file" > "$tmpf" && cp "$tmpf" "$file"
done
My requirement is slightly changed and please help on the below.
Earlier i was trying insert a character(I) at 180th position of all the lines in the file except 1st and last line. Now i have little change in this requirement, I just need to replace whatever character in 180 the position to chracter "I".
I was using below code, But i am trying different options buts its not working.