Sponsored Content
Top Forums Shell Programming and Scripting Using sed to insert text between lines Post 302976400 by tsu3000 on Wednesday 29th of June 2016 05:12:04 AM
Old 06-29-2016
Using sed to insert text between lines

Hello, I am trying to insert a section of text between lines in another text file.

The new lines to be inserted are:
Code:
abcd[0].efgh[N].zzzz=blah
abcd[0].efgh[N].xxxx=blah

Where N = 0 to 2

Original File:
Code:
abcd[0].efgh[0].wwxx=aaaaa
abcd[0].efgh[0].yyzz=bbbbb
abcd[0].efgh[1].wwxx=aaaaa
abcd[0].efgh[1].yyzz=bbbbb
abcd[0].efgh[2].wwxx=aaaaa
abcd[0].efgh[2].yyzz=bbbbb

Change to:
Code:
abcd[0].efgh[0].wwxx=aaaaa
abcd[0].efgh[0].yyzz=bbbbb
abcd[0].efgh[0].zzzz=blah
abcd[0].efgh[0].xxxx=blah
abcd[0].efgh[1].wwxx=aaaaa
abcd[0].efgh[1].yyzz=bbbbb
abcd[0].efgh[1].zzzz=blah
abcd[0].efgh[1].xxxx=blah
abcd[0].efgh[2].wwxx=aaaaa
abcd[0].efgh[2].yyzz=bbbbb
abcd[0].efgh[2].zzzz=blah
abcd[0].efgh[2].xxxx=blah


This is what I have done so far (and it works with Linux GNU sed), but I am looking for a more elegant way to do it. Perhaps using backreferencing?

Code:
sed "{
/efgh\[0\].yyzz=bbbbb/a abcd[0].efgh[0].wwxx=aaaaa\nabcd[0].efgh[0].xxxx=blah
/efgh\[1\].yyzz=bbbbb/a abcd[0].efgh[1].wwxx=aaaaa\nabcd[0].efgh[1].xxxx=blah
/efgh\[2\].yyzz=bbbbb/a abcd[0].efgh[2].wwxx=aaaaa\nabcd[0].efgh[2].xxxx=blah
}" dataset.txt

Any ideas?

Thanks in advance.
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Insert Text With Sed

Hello. Trying to insert text at line 1 and after last line of file. I have searched posts but nothing seems to work. I keep getting extra characters error or nothing gets inserted into the file. #!/bin/sh touch textfile.txt sed 'i\ Add this line before every line with WORD' textfile.txt ... (5 Replies)
Discussion started by: steveramsey
5 Replies

2. Shell Programming and Scripting

Need to insert new text and change existing text in a file using SED

Hi all, I need to insert new text and change existing text in a file. For that I used the below line in the command line and got the expected output. sed '$a\ hi... ' shell > shell1 But I face problem when using the same in script. It is throwing the error as, sed: command garbled:... (4 Replies)
Discussion started by: iamgeethuj
4 Replies

3. UNIX for Dummies Questions & Answers

Insert Text on lines having the string word

I need help on how I can accomplish my task. I hope someone can help me since I've researching and trying to accomplish this for hours now. Basically, I need to comment-out (or insert a # sign in the beginning of the line) a line when the line has the specific word I am searching. Example I have... (3 Replies)
Discussion started by: Orbix
3 Replies

4. Shell Programming and Scripting

sed - insert two lines

I have done this sed command to insert one line after a specific string is found: sed '/patternstring/ a\ new line string' file1 But how do I insert two lines? This is not possible: sed '/patternstring/ a\ new line string \a new line string 2' file1 (2 Replies)
Discussion started by: locoroco
2 Replies

5. Shell Programming and Scripting

Insert text using sed

sed 's/$/TEST/g' will insert TEST at the end of each line. i want to insert TEST at column 64 (7 Replies)
Discussion started by: lawsongeek
7 Replies

6. Shell Programming and Scripting

sed insert text 2 lines above pattern

Hi I am trying to insert a block of text 2 lines above a pattern match using sed eg #Start of file entry { } #End of file entry new bit of text has to be put in just above the } eg #Start of file entry { New bit of text } #End of file entry (7 Replies)
Discussion started by: eeisken
7 Replies

7. Shell Programming and Scripting

How do I insert text with sed ?

Hi I was wondering if anyone new of a solution to this problem? I need to copy a time stamp that is on a line of .text in a text file into multiple positions on the same line. I need to insert the time stamp on the same line between every occurance of the text ".pdf_.html" right after the... (9 Replies)
Discussion started by: Paul Walker
9 Replies

8. Shell Programming and Scripting

Insert text before first 'n' lines

I want to put a particular text, say, the hash '#' before each of the first n lines of a file. How can I do that? (4 Replies)
Discussion started by: hbar
4 Replies

9. Shell Programming and Scripting

Sed; insert text two lines above match

Hi! Considering below text, how would I use sed to insert text right below the v0005-line, using the SEPARATOR-line as a pattern to search for, so two lines above the separator? I can do it right above the separator, but not 2 lines... # v0004 - Some text # v0005 - More text #... (5 Replies)
Discussion started by: indo1144
5 Replies

10. Shell Programming and Scripting

Insert Text after one, two, three lines & so on..

I want to insert "Text" in each file as a place where I mentioned below "Insert Text Here". These files are something like news of newspaper. Generally, newspaper headlines contain one or two lines. I don't know how it can be identified whether Text is inserted after first line or second line. ... (10 Replies)
Discussion started by: imranrasheedamu
10 Replies
XROOTCONSOLE(1) 					    BSD General Commands Manual 					   XROOTCONSOLE(1)

NAME
xrootconsole -- display a file (or stdin) to a transparent window SYNOPSIS
xrootconsole [options] [file] DESCRIPTION
xrootconsole is a small utility which displays its input in a transparent text box on X's root window. It will read from any file listed on the command line or, by default, from stdin. It is most useful when it reads from a FIFO; this allows you to redirect multiple commands to the FIFO and monitor their output. The options are as follows: -fg color This option specifies the color to use for displaying text. The default is ``black.'' -bg color This option specifies the color to AND with the root window, for shaded transparency. The default is ``clear.'' -bw number This option specifies the width of the border around xrootconsole in pixels. The default is 0. -bd color This option specifies the color to use for the border of the window. The default is ``black.'' -fn font This option specifies the font to use for displaying text. The default is ``fixed.'' -c number This option produces output that is number columns wide. Text is written vertically down each column in the order in which text is received from the input file. The default is 1. --topdown Forces xrootconsole to display new lines from its input at the top of the screen, scrolling previous lines down. Default behav- ior is to insert new lines at the bottom and scroll the previous lines up. --wrap Rather than truncating long lines, this option specifies that xrootconsole should wrap excess text to the following line or lines, as necessary. --ansi-color Enable color support by parsing ANSI escape sequences. -geometry geometry This option specifies the preferred size and position of the window; see X(1). The default is ``80x10+0+0.'' -h, --help Displays a detailed usage message and exits. -v, --version Displays the program version and exits. file Optionally specifies a file from which to read input; by default, xrootconsole reads from the standard input. DIAGNOSTICS
Exit status is 0 on success, and 1 if the command fails for any reason. AUTHORS
Eric Youngblut is the original author of xrootconsole. Bob Galloway <bob@de-fac.to> is the current maintainer. Alexandre Duret-Lutz elimi- nated flicker by storing the background pixmap and drawing off-screen. This manual page was written by Zak Johnson <zakj@i.am>. BSD
May 19, 2000 BSD
All times are GMT -4. The time now is 09:19 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy