If we just stick with standard sed addressing constructs and do not depend on non-standard options, you could try something more like:
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).
This User Gave Thanks to Don Cragun For This Post:
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)
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)
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)
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)
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)
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)
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)
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)
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)