Insert a line of text on nth line of a file

Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Old 02-24-2016
Wrench 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
Code:
sed '3 i this is the 4th line' filename
sed: command garbled: 3 i this is the 4th line

again i tried with
Code:
 sed -i '3i 4th line' filename
sed: illegal option -- i

any help would be appreciated.

Regards- Goutam
# 2  
Old 02-24-2016
Hello gotamp,

Could you please try following and let me know if this helps you.
Let's say following is the Input_file.
Code:
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.
Code:
sed -n -i 'p;13a "CHUMMA"'  Input_file

Output will be as follows now.
Code:
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.
Code:
sed -n 'p;13a "CHUMMA"'  Input_file

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

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
gotamp (02-24-2016)
# 3  
Old 02-24-2016
Many thanks Ravinder,

I tried this but the -i option is not working
Code:
<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.
Code:
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 .
Code:
 uname -a
SunOS aspsun19 5.10 Generic_150400-20 sun4u sparc SUNW,SPARC-Enterprise

many thanks - Goutam
# 4  
Old 02-24-2016
Hello gotamp,

As you are using SunOS, you could try following and let me know if this helps you.
Code:
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 awkto /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
# 5  
Old 02-24-2016
You might also want to try the following:
Code:
#!/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.
# 6  
Old 02-24-2016
Hi.

One of the missing routines is insert_every:
Code:
#!/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:
Code:
$  ./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
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Insert the line number from text file to filename output martinsmith UNIX for Beginners Questions & Answers 2 10-11-2018 06:20 AM
Printing string from last field of the nth line of file to start (or end) of each line (awk I think) samonl Shell Programming and Scripting 9 02-06-2018 06:19 PM
Insert new line of text into Odbc.ini file dellanicholson Shell Programming and Scripting 0 04-13-2016 03:30 PM
How to read a text file line by line and insert into a database table? JolietJake Shell Programming and Scripting 2 10-09-2014 12:23 PM
Insert a line in a text file lost.identity UNIX for Dummies Questions & Answers 2 02-18-2014 12:15 PM
How to start reading from the nth line till the last line of a file. machomaddy Shell Programming and Scripting 5 01-23-2012 02:16 PM
Insert a line in a sorted text file(s) raptor25 UNIX for Dummies Questions & Answers 7 07-15-2011 07:34 PM
Using sed to insert text file at first line psve Shell Programming and Scripting 18 03-29-2011 04:44 AM
Sed insert text at first line of empty file DC Slick Shell Programming and Scripting 4 02-18-2011 03:17 AM
How to insert string at particular 4th line in text file puneet.goel Shell Programming and Scripting 8 08-05-2009 10:33 AM
How to insert some constant text at beginig of each line within a text file. Muhammad Afzal Shell Programming and Scripting 4 10-31-2008 12:01 AM
Insert a line in a text file 4scriptmoni Shell Programming and Scripting 10 08-04-2008 11:17 AM
how to insert a extra line in a text file bhaviknp Shell Programming and Scripting 2 03-07-2008 08:38 AM
Can a shell script pull the first word (or nth word) off each line of a text file? tricky Shell Programming and Scripting 5 08-17-2006 07:29 AM
Insert text file at a certain line. insania Shell Programming and Scripting 4 08-01-2006 03:46 AM