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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert Text after one, two, three lines & so on..
# 1  
Old 06-19-2016
Linux 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.

Could anyone please help me..

file1.txt
Code:
... .... .... .. ... ... ..
... ...... ........ .. ...... .... ...
Insert Text Here
.......  ... .......... ..... .... .... . ...... ... ... .... .... .. ... ...  ... .. ..... ... .... ....... ...... ........ ...... ...... .........  ...... ... ...... .. .... ..



file2.txt
Code:
....... .. ... .. ........ ... .. ... ...... .. .... .... ..........
Insert Text Here
.......  ... .............. .. ..... ... ..... ...... .. ......... ..... .....  ........ ..... ..... .. .... .. .... .... ..... .. ...... .. ..... ....  .... ..... ..... .. ......

file3.txt
Code:
...... .. ... ....... .. ..... .. ..... .. .... .... .. .......
....... ... ....... ..... .... .. ...... ..... .... ..... ....... .. .....
Insert Text Here
.......  ... ............ .. ... ....... .. .... .... .... .... .. ....... ..  ..... ..... .. .. ... .. .. ... ..... .. ..... ... .... .. ...... ..  ..... .... .... ... .. .. ....
   .. ... .. .....  .. .... ...... .. .... ..... ..... .... ..... .. .... ..... .. ... .....  .. ... ... .. .... .... .. ... .. ... ... .. ..... ..... .. .......  ........ .. ....


Moderator's Comments:
Mod Comment Please use code tages for your code and data next time, thanks


Moderator's Comments:
Mod Comment Please comply with forum rules - here: (9) English only! Text samples moderated.

Last edited by RudiC; 06-19-2016 at 06:54 AM.. Reason: Moderated non-English text.
# 2  
Old 06-19-2016
I'm afraid you accredit people in here with too much creativity. Unless you give some more details on how the files are structured (e.g. a common feature that tells header from body), we are as doomed as you are.
# 3  
Old 06-19-2016
I have a collection of files like this

File1.txt
Colorado woman pries
open mountain lion's jaws to rescue son
Summoned by the sound of screams, a Colorado woman raced to her frontyard to find a terrifying sight -- a mountain lion was hunched over her 5-year-old son, biting him.

File2.txt
Hillary's lead over Trump has narrowed after Orlando shooting
Donald Trump chipped away at Hillary Clinton’s lead in the presidential race this week, according to a Reuters/Ipsos poll released on Friday, as the candidates clashed over how to respond to the worst mass shooting in modern U.S. history.


and I need to insert tag to all files something like this

File1.txt
<heading>
Colorado woman pries
open mountain lion's jaws to rescue son
</heading>
<text>
Summoned by the sound of screams, a Colorado woman raced to her frontyard to find a terrifying sight -- a mountain lion was hunched over her 5-year-old son, biting him.
</text>

File2.txt
<heading>
Hillary's lead over Trump has narrowed after Orlando shooting
</heading>
<text>
Donald Trump chipped away at Hillary Clinton’s lead in the presidential race this week, according to a Reuters/Ipsos poll released on Friday, as the candidates clashed over how to respond to the worst mass shooting in modern U.S. history.
</text>
# 4  
Old 06-19-2016
Here is an attempt. Save the following to a file "test.sh" and make it executable.
Code:
#!/bin/sh
awk '
NR==1 {
  print "<heading>"
}
NR==2 {
  if (/\./ || length>=80) {
    # this looks like text
    print "</heading"
    print "<text>"
  } else {
    # this looks lilke a 2nd header line
    print
    print "</heading"
    print "<text>"
    next
  }
}
{ print }
END {
  if (NR>1) print "</text>"
}
' "$@"

Now you can run it with
Code:
./test.sh File1.txt
./test.sh File2.txt

See the criteria for the start of the text: if there is a . character or the length of the line is >= 80.
This User Gave Thanks to MadeInGermany For This Post:
# 5  
Old 06-19-2016
Thank you Sir!
Your code is working!
Is there any similar method to execute the same code for thousands of files??
# 6  
Old 06-20-2016
Add the missing > highlighted in red.
Quote:
Originally Posted by MadeInGermany
[...]
Code:
#!/bin/sh
awk '
NR==1 {
  print "<heading>"
}
NR==2 {
  if (/\./ || length>=80) {
    # this looks like text
    print "</heading>"
    print "<text>"
  } else {
    # this looks lilke a 2nd header line
    print
    print "</heading>"
    print "<text>"
    next
  }
}
{ print }
END {
  if (NR>1) print "</text>"
}
' "$@"

Now you can run it with
Code:
./test.sh File1.txt
./test.sh File2.txt

[...]
Quote:
Originally Posted by imranrasheedamu
Thank you Sir!
Your code is working!
Is there any similar method to execute the same code for thousands of files??
It all depends how much your shell can handle.

Please, try the following.

Save as txt2xmlnews.pl
Run as perl txt2xmlnews.pl file*.txt
or
perl txt2xmlnews.pl file1.txt file2.txt file3.txt ...
or
perl txt2xmlnews.pl file{1..100}.txt
Code:
#!/usr/bin/perl
use strict;
use warnings;

for my $filename (@ARGV) {
    open my $in, '<', $filename or die;
    open my $out, '>', "$filename.xml" or die;
    local $/;
    my ($head, $body) = split (/\n(?=[A-Z])/, <$in>, 2);
    print $out "<heading>\n$head\n</heading>\n";
    print $out "<text>\n$body</text>\n";
}

That creates a copy with the suffix .xml

Last edited by Aia; 06-20-2016 at 01:23 AM..
This User Gave Thanks to Aia For This Post:
# 7  
Old 06-20-2016
after executing the code in perl using perl test.pl file*.txt, I get this error

readline() on closed filehandle $in at test.pl line 9.
Use of uninitialized value $head in concatenation (.) or string at test.pl line 10.
Use of uninitialized value $body in concatenation (.) or string at test.pl line 11.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

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: abcd.efgh.zzzz=blah abcd.efgh.xxxx=blah Where N = 0 to 2 Original File: abcd.efgh.wwxx=aaaaa abcd.efgh.yyzz=bbbbb abcd.efgh.wwxx=aaaaa abcd.efgh.yyzz=bbbbb... (3 Replies)
Discussion started by: tsu3000
3 Replies

2. 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

3. 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

4. Shell Programming and Scripting

Comparing 2 text files & downloading a file if the last lines are different

Hello I'm having a little difficulty in writing a shell script for a few simple tasks. First I have two files "file1.txt" and "file2.txt" and I want to read and compare the last line of each file. The files look like this. File1.txt File2.txt After comparing the two lines I would... (2 Replies)
Discussion started by: RustikGaming
2 Replies

5. 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

6. Shell Programming and Scripting

Insert a variable to a text file after fixed number of lines

Hi, I am new to unix. I need to insert a variable which contains some lines of text into a text file after fixed number of lines.. Please help me on this.. Thanks in Advance, Amrutha (3 Replies)
Discussion started by: amr89
3 Replies

7. Shell Programming and Scripting

how to Insert values in multiple lines(records) within a pipe delimited text file in specific cols

this is Korn shell unix. The scenario is I have a pipe delimited text file which needs to be customized. say for example,I have a pipe delimited text file with 15 columns(| delimited) and 200 rows. currently the 11th and 12th column has null values for all the records(there are other null columns... (4 Replies)
Discussion started by: vasan2815
4 Replies

8. 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

9. Shell Programming and Scripting

how to insert text between lines of an existing file using perl

Hi , I need some inputs on how to open a file (file.txt) and parse the text example aaa of the file and bbb of the file and add the text zzzz once i parse (aaa and bbb) and followed by the remaining of the text as it is in the file using perl programming. Thanks in advance (3 Replies)
Discussion started by: madhul2002
3 Replies

10. 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
Login or Register to Ask a Question