Sponsored Content
Top Forums UNIX for Beginners Questions & Answers Insert a line of text on nth line of a file Post 302967507 by drl on Wednesday 24th of February 2016 10:07:52 AM
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
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Insert text file at a certain line.

I need to insert a file called temp_impact (which has about 15 lines in it) to a file called 11.23cfg starting at line 33. I searched the forums and found the sed '34i\ test' 11.23cfg > newfile That will enter word test at the appropriate line, but i need the entire file dumped there. Any... (4 Replies)
Discussion started by: insania
4 Replies

2. Shell Programming and Scripting

how to insert a extra line in a text file

how to insert a extra line in a text file using a sh command iam trying to think of a way to add a extra line but without deleting the whole text do anyone have any ideas (2 Replies)
Discussion started by: bhaviknp
2 Replies

3. Shell Programming and Scripting

Insert a line in a text file

So I need to write lines into line X of file X. I can get the file by doing: cfgnumber=$(cat -n -comm.cfg| grep -i "servicegroup_name 24x7-comunicacions") echo $cfgnumber it outputs the Line where it finds now I need to start writing something right bellow that line. thanks (10 Replies)
Discussion started by: 4scriptmoni
10 Replies

4. Shell Programming and Scripting

How to insert some constant text at beginig of each line within a text file.

Dear Folks :), I am new to UNIX scripting and I do not know how can I insert some text in the first column of a UNIX text file at command promtp. I can do this in vi editor by using this command :g/^/s//BBB_ e,g I have a file named as Test.dat and it containins below text: michal... (4 Replies)
Discussion started by: Muhammad Afzal
4 Replies

5. Shell Programming and Scripting

Using sed to insert text file at first line

sed '1r file.txt' <source.txt >desti.txt This example will insert 'file.txt' between line 1 and 2 of source.txt. sed '0r file.txt' <source.txt >desti.txt gives an error message. Does anyone know how 'sed' can insert 'file.txt' before the first line of source.txt? (18 Replies)
Discussion started by: psve
18 Replies

6. UNIX for Dummies Questions & Answers

Insert a line in a sorted text file(s)

Hello, I am trying to add a line (usually just a word) to some text files in a directory that are already sorted. I just don't want to run the sort command again because it can take a long time when the text or log files are really huge. I have a bashscript that will take in the 1st argument... (7 Replies)
Discussion started by: raptor25
7 Replies

7. Shell Programming and Scripting

How to start reading from the nth line till the last line of a file.

Hi, For my reuirement, I have to read a file from the 2nd line till the last line<EOF>. Say, I have a file as test.txt, which as a header record in the first line followed by records in rest of the lines. for i in `cat test.txt` { echo $i } While doing the above loop, I have read... (5 Replies)
Discussion started by: machomaddy
5 Replies

8. UNIX for Dummies Questions & Answers

Insert a line in a text file

I want to insert a line with text after the 9th line of a text file. How would I do this using sed or awk? (2 Replies)
Discussion started by: lost.identity
2 Replies

9. Shell Programming and Scripting

How to read a text file line by line and insert into a database table?

I have a test file that I want to read and insert only certain lines into the the table based on a filter. 1. Rread the log file 12 Hours back Getdate() -12 Hours 2. Extract the following information on for lines that say "DUMP is complete" A. Date B. Database Name C.... (2 Replies)
Discussion started by: JolietJake
2 Replies

10. Shell Programming and Scripting

Printing string from last field of the nth line of file to start (or end) of each line (awk I think)

My file (the output of an experiment) starts off looking like this, _____________________________________________________________ Subjects incorporated to date: 001 Data file started on machine PKSHS260-05CP ********************************************************************** Subject 1,... (9 Replies)
Discussion started by: samonl
9 Replies
cvcd(1M)						  System Administration Commands						  cvcd(1M)

NAME
cvcd - virtual console daemon SYNOPSIS
/platform/platform_name/cvcd DESCRIPTION
The virtual console daemon, cvcd, is a server process that supports the network console provided on some platforms. The cvcd daemon accepts network console connections from a remote host (only one host at any given time). Console input is read from this connection and forwarded to cvc(7D) by way of cvcredir(7D). Similarly, console output is read from cvcredir(7D) and forwarded across the network console connection. If cvcd dies, console traffic is automatically rerouted through an internal hardware interface. The cvcd daemon normally starts at system boot time. Each domain supports only one cvcd process at a time. Caution: On Sun Enterprise 10000 domains, cvcd uses a configuration file (/etc/ssphostname) to determine the name of the host from which network console connections are allowed. If the remote console host is renamed, you must edit the configuration file to reflect that change. OPERANDS
The following operands are supported: platform_name The official Sun platform name used in packaging and code. For example, for Sun Fire 15K servers, the platform_name would be SUNW,Sun-Fire-15000. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Architecture |Sun Enterprise 10000 | | |servers, Sun Fire High-End | | |Systems | +-----------------------------+-----------------------------+ |Availability |SUNWcvc.u | +-----------------------------+-----------------------------+ SEE ALSO
svcs(1), svcadm(1M), services(4), attributes(5), smf(5), cvc(7D), cvcredir(7D) Sun Enterprise 10000 SSP Reference Manual System Management Services (SMS) Reference Manual NOTES
The cvcd service is managed by the service management facility, smf(5), under the service identifier: svc:/system/cvc Administrative actions on this service, such as enabling, disabling, or requesting restart, can be performed using svcadm(1M). The ser- vice's status can be queried using the svcs(1) command. SunOS 5.10 11 Aug 2004 cvcd(1M)
All times are GMT -4. The time now is 02:28 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy