insert text and add blank line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting insert text and add blank line
# 1  
Old 08-15-2007
Bug insert text and add blank line

I need your help,

I want to add a text every 2nd line and also a blank line after 3 line
(In the output 2nd line is "changetype: modify" and every 4th line is blank line)

Input file format
dn: abc
orclsourceobjectdn: abcd
dn: bcd
orclsourceobjectdn: bcda
dn: cba
orclsourceobjectdn: cbaa


Output format has to be

dn: abc
changetype: modify
orclsourceobjectdn: abcd

dn: bcd
changetype: modify
orclsourceobjectdn: bcda

dn: cba
changetype: modify
orclsourceobjectdn: cbaa



Thanks in advance
# 2  
Old 08-15-2007
nawk -f athindi.awk myFile

athindi.awk:
Code:
!FNR%2 { print "changetype: modify"}
1
!FNR%2 { print ""}

# 3  
Old 08-15-2007
It's not the fastest script in the world, but it works for this situation. Save as insert.ksh.

Code:
#!/bin/ksh

    while read LINE
    do
        if [ $(expr "$LINE" : "dn:") -ne 0 ]
        then
            LINE="$LINE\nchangetype: modify"

        elif [ $(expr "$LINE" : "orclsourceobjectdn:") -ne 0 ]
        then
            LINE="$LINE\n"
        fi
        echo "$LINE"
    done

# ksh insert.ksh < file
dn: abc
changetype: modify
orclsourceobjectdn: abcd

dn: bcd
changetype: modify
orclsourceobjectdn: bcda

dn: cba
changetype: modify
orclsourceobjectdn: cbaa



#
# 4  
Old 08-16-2007
Code:
awk '{ if ( match($0, "^orcl") ) { printf "%s\n%s\n\n", "changetype: modify", $0 } else { print } }' filename

# 5  
Old 08-16-2007
try this one

hi, pls try this one as below:


Code:
cat file | awk '{ 
if (NR%2==1) 
{ 
print 
print "change" 
} 
else 
{ 
printf("%s\n",$0)
print "" } }'

# 6  
Old 08-16-2007
summer_cherry

this is UUOC

you can give the filename to the awk directly

no need to pipe the output of cat to awk
# 7  
Old 08-16-2007
Quote:
Originally Posted by summer_cherry
hi, pls try this one as below:


Code:
cat file | awk '{ 
if (NR%2==1) 
{ 
print 
print "change" 
} 
else 
{ 
printf("%s\n",$0)
print "" } }'

summer_cherry,
way too verbose for my taste..... Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to identify exact text and then add a blank line above it using sed?

I need to identify the exact text of San Antonio Generator Running in the output my script which lands to a text file. Once SED finds the specific text, I need it to insert one line above the matched text. Here is what I have so far that isn't working all that well for me. Any help would be... (7 Replies)
Discussion started by: jbrass
7 Replies

2. Shell Programming and Scripting

sed - How to insert line before the first blank line following a token

Hello. I have a config file (/etc/my_config_file) which may content : # # port for HTTP (descriptions, SOAP, media transfer) traffic port=8200 # network interfaces to serve, comma delimited network_interface=eth0 # set this to the directory you want scanned. # * if have multiple... (6 Replies)
Discussion started by: jcdole
6 Replies

3. Shell Programming and Scripting

Find regex, place on individual lines and insert blank line before

Hello, I have a file that I want to be able to insert a new line before every instance of a regex. I can get it to do this for each line that contains the regex, but not for each instance. Contents of infile: Test this 1... Test this 2... Test this 3... Test this 4... Test this... (2 Replies)
Discussion started by: deneuve01
2 Replies

4. Shell Programming and Scripting

sort with condition and insert blank line

Hi, I have a file with no blank line anywhere Its a conf file, The lines between and starts with "#" should be sort out with the first three characters(characters are between (a-z)). and insert a blank space in between the uniq lines. For clear understanding ,pls find the below input file... (11 Replies)
Discussion started by: anil8103
11 Replies

5. Shell Programming and Scripting

insert blank line between lines

Hello, I am trying to write a script that will count the number of characters for each line in a file and all the lines that have less than 80 characters and that are ending with a period, I want it to insert a blank line after them immediately. But, for whatever reason the condition if ]] is... (3 Replies)
Discussion started by: Pouchie1
3 Replies

6. Shell Programming and Scripting

Insert blank line if grep not found

Hi all, I've googling around forum regarding my prob, the nearest would same as thread tittled Insert blank line if grep not found, but she/he did not mention the solution, so I would like to request your help I've this task, to search in file2 based on pattern in file1 and output it to... (4 Replies)
Discussion started by: masterpiece
4 Replies

7. Shell Programming and Scripting

Insert blank line in a file

I have a file with data as below : Heading 1 ------------- Heading 1 data1 Heading 1 data2 Heading 1 data3 Heading 1 data4 Heading 2 ------------- Heading 2 data1 Heading 2 data2 Heading 2 data3 Heading 2 data4 Heading 3 ------------- Heading 3 data1 Heading 3 data2 Heading 3... (2 Replies)
Discussion started by: yoursdivu
2 Replies

8. Shell Programming and Scripting

Insert\extract text with blank on unix

I need your lights. On Unix I have a file consisting of a single line of 2 700 000 characters ... Like that : 30004SCT TARTENPION 00003FONTEN 30008*E GOUDYEAR DODO 1 Street Bigggest 30004OHOHOHITSALONGLINE ...... (3 Replies)
Discussion started by: mangalor
3 Replies

9. Shell Programming and Scripting

compare three files and insert a blank line at each mismatch

i need to compare three files in unix a.txt b.txt c.txt 1 2 1 2 5 3 4 6 5 5 6 6 i need to insert a blank line in the file if i don't find a match and put the items at the same column if found a match The items in the files... (4 Replies)
Discussion started by: mad_man12
4 Replies

10. Shell Programming and Scripting

Insert blank line if grep not found

Hello everyone... please help if you can -- I'm stumped. Making this work will save me hours of manual labor: I need to search file2 for pattern in file1. If pattern found append file2 line to file3. If pattern not found append a blank line to file3. file1 contents example: 123 456 789... (6 Replies)
Discussion started by: michieka
6 Replies
Login or Register to Ask a Question