Insert a line of text on nth line of a file

Hi All,

I am using UNix Sun OS sun4u sparc SUNW,SPARC-Enterprise

My intention is to insert a line of text after 13th line of every file inside a particular directory.

While trying to do it for a single file , i am using sed

sed '3 i this is the 4th line' filename
sed: command garbled: 3 i this is the 4th line

again i tried with

 sed -i '3i 4th line' filename
sed: illegal option -- i

any help would be appreciated.

Regards- Goutam

Hello gotamp,

Could you please try following and let me know if this helps you.
Let's say following is the Input_file.

cat Input_file
200,1245,E1,1,E1,,7611068,KWH,30, ,,,,,,,,
200,1245,E1,1,E1,,7611070,KWH,30, ,,,,,,,,
300,20140223,0.001,0.001,0.001,0.001,0.001
300,20140224,0.001,0.001,0.001,0.001,0.001
300,20140225,0.001,0.001,0.001,0.001,0.001
300,20140226,0.001,0.001,0.001,0.001,0.001
300,20140227,0.001,0.001,0.001,0.001,0.001
300,20140228,0.001,0.001,0.001,0.001,0.001
300,20140301,0.001,0.001,0.001,0.001,0.001
300,20140302,0.001,0.001,0.001,0.001,0.001
300,20140303,0.001,0.001,0.001,0.001,0.001
300,20140304,0.001,0.001,0.001,0.001,0.001
1300,20140305,0.001,0.001,0.001,0.001,0.001
300,20140305,0.001,0.001,0.001,0.001,0.001
300,20140305,0.001,0.001,0.001,0.001,0.001
200,1254,E1,1,E1,,4749337,KWH,30, ,,,,,,,,
300,20140306,0.001,0.001,0.001,0.001,0.001

Then following code may be helpful for you, which will insert a line after 13th line of Input_file and save the Input_file too with changes.

sed -n -i 'p;13a "CHUMMA"'  Input_file

Output will be as follows now.

cat Input_file
200,1245,E1,1,E1,,7611068,KWH,30, ,,,,,,,,
200,1245,E1,1,E1,,7611070,KWH,30, ,,,,,,,,
300,20140223,0.001,0.001,0.001,0.001,0.001
300,20140224,0.001,0.001,0.001,0.001,0.001
300,20140225,0.001,0.001,0.001,0.001,0.001
300,20140226,0.001,0.001,0.001,0.001,0.001
300,20140227,0.001,0.001,0.001,0.001,0.001
300,20140228,0.001,0.001,0.001,0.001,0.001
300,20140301,0.001,0.001,0.001,0.001,0.001
300,20140302,0.001,0.001,0.001,0.001,0.001
300,20140303,0.001,0.001,0.001,0.001,0.001
300,20140304,0.001,0.001,0.001,0.001,0.001
1300,20140305,0.001,0.001,0.001,0.001,0.001
"CHUMMA"
300,20140305,0.001,0.001,0.001,0.001,0.001
300,20140305,0.001,0.001,0.001,0.001,0.001
200,1254,E1,1,E1,,4749337,KWH,30, ,,,,,,,,
300,20140306,0.001,0.001,0.001,0.001,0.001

In case you don't want to save the changes in Input_file then following may be helpful to you.

sed -n 'p;13a "CHUMMA"'  Input_file

Hope this helps you(Also I tried this in GNU sed only).

Thanks,
R. Singh

1 Like

Many thanks Ravinder,

I tried this but the -i option is not working

<aspsun19:inteng03> sed -n -i 'p;3a "CHUMMA"' abc
sed: illegal option -- i

then again i tried without -i option as well just to check the change . still it does not work.

sed -n 'p;3a "CHUMMA"' abc
sed: command garbled: p;3a "CHUMMA"

may be there is a problem with the version. Sending you the whole uname output so that you can understand my machine a bit more .

 uname -a
SunOS aspsun19 5.10 Generic_150400-20 sun4u sparc SUNW,SPARC-Enterprise

many thanks - Goutam

Hello gotamp,

As you are using SunOS, you could try following and let me know if this helps you.

cat script.ksh
awk 'FNR==13{print $0 ORS "CHUMMA";next} 1' Input_file > temp_file
mv temp_file Input_file

On a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk , /usr/xpg6/bin/awk , or nawk .

NOTE: You could run above in a script like I have showed or up to you if you could use it as commands too(in case you wanted to do this for 1 or 2 files), don't forget to give proper permissions to script.ksh to run it properly. Also I haven't tested above script but it should work properly I believe.

Thanks,
R. Singh

You might also want to try the following:

#!/bin/ksh
dir='/path/of/directory/to/process'
IAm=${0##*/}
tmpf="$IAm.$$"

trap 'rm -f "$tmpf"' EXIT

cd "$dir"
for file in *
do	[ ! -f "$file" ] && continue
	/usr/xpg4/bin/sed '13a\
this is the 14th line
	' "$file" > "$tmpf" && cp "$tmpf" "$file"
done

Obviously, you'll need to change the path shown in red to a path naming the directory in which you want to process your files.

If sed fails for some reason, it won't destroy the file you were trying to update (which RavinderSingh13's suggestion might do if the awk fails).

If there are any hard links to any of the files being processed, this script will maintain those links. (The code RavinderSingh13 suggested will break the links, if there are any, creating two separate (unlinked) files.)

This would probably also work with /usr/bin/sed on Solaris systems, but I'm almost positive it will work with /usr/xpg4/bin/sed . Unfortunately, I don't have a Solaris system available for testing.

Hi.

One of the missing routines is insert_every :

#!/usr/bin/env bash

# @(#) s1       Demonstrate repeating string insertion, insert_every

PROGRAM="./insert_every"
# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
LC_ALL=C ; LANG=C ; export LC_ALL LANG
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C 

FILE=${1-data1}

pl " Input data file $FILE:"
cat $FILE

pl " Results:"
$PROGRAM --n=3 "Hello" < $FILE

pl " Text of perl code, $PROGRAM:"
cat $PROGRAM

exit 0

producing:

$  ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.3 (jessie) 
bash GNU bash 4.3.30

-----
 Input data file data1:
1
2
3
4
5
6
7
8
9
10

-----
 Results:
1
2
3
Hello
4
5
6
Hello
7
8
9
Hello
10

-----
 Text of perl code, ./insert_every:
#!/usr/bin/env perl

# @(#) insert_every     Write first-parameter-string after --n=x lines.
# $Id: insert_every,v 1.2 2015/03/18 12:38:41 drl Exp drl $

# The Missing Textutils, Ondrej Bojar, obo@cuni.cz
# http://www.cuni.cz/~obo/textutils
#
# 'insert_every' processes stdin to stdout, writing ARG1 after every n
# lines

use strict;
use warnings;
use Getopt::Long;

sub usage {
  print STDERR "insert_every text_to_insert <stdin >stdout
Options:
  --n=X   ... insert after n lines
";
  exit 1;
}

my $n     = 10;
my $usage = 0;
GetOptions(
  "help" => \$usage,
  "n=i"  => \$n,
);
usage() if $usage;

my $extra = shift;

my $nr = 0;
while (<>) {
  $nr++;
  print;
  print $extra. "\n" if $nr % $n == 0;
}

To see all of the dozens of missing textutil perl codes, visit the web page of Ondrej Bojar as noted in the script comments (verified as of today, Wed Feb 24 09:05:48 CST 2016).

Best wishes ... cheers, drl