SED - adding a new line after pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting SED - adding a new line after pattern
# 1  
Old 07-04-2008
SED - adding a new line after pattern

Hi,

In files, I have a field Date Of Birth (DOB). After that line I need to add Date of Joining (DOJ)

DOB:19-Apr-1981

needs to become

DOB:19-Apr-1981
DOJ:20-Jun-2005

What can be a sed/perl line that can do it for me. Please note that DOB/DOJ I have in variables

I am doing in a sh script

'line' represents file name which has names of many files which need to be enhanced.

var1=`echo $line | cut -d ":" -f1`
var2=`echo $line | cut -d ":" -f2`
perl -e 'while (<>) { s/(DOBSmilie/$1\/DOJ: $var2/g; print }' $var1.txt > $va
r1\_new.txt

var1 - filename
var2 - DOJ data

Please advise.
# 2  
Old 07-04-2008
Used two lines of input to show what happens:
Code:
PAT="DOJ:20-Jun-2005"
sed '/$/a\'${PAT}'' infile

DOB:19-Apr-1981
DOJ:20-Jun-2005
DOB:20-Apr-1981
DOJ:20-Jun-2005

That's what you wanted?
# 3  
Old 07-04-2008
Currently my files have

DOB:........ line

I need to add DOJ:..... after that line
# 4  
Old 07-04-2008
1 clarification.

DOB line is not the last line.

The file would have something like

Name:
DOB:
Address:
Contact:
Qualification:

etc

Alternatively if I can induce that in a specified line, that is equally great
# 5  
Old 07-04-2008
Better show a larger snippet of your input file to make circumstances easier to see/understand, please.

Code:
PAT="DOJ:20-Jun-2005"
sed '/^DOB.*$/a\'${PAT}'' infile

Will now only add that pattern as a separate line if, if the former line starts with DOB.
# 6  
Old 07-04-2008
Thanks a lot for your help.

My original file looks like

Name: Eager
DOB: 19-Apr-1981
Address: 2nd Cross, Church Street
Contact: 407 ...
Qualification: Bachelor Of Comp Science

Needs to become

Name: Eager
DOB: 19-Apr-1981
DOJ: 20-Jun-2005
Address: 2nd Cross, Church Street
Contact: 407 ...
Qualification: Bachelor Of Comp Science

I have a file like DOJAdd.txt which mentiones Eager.txt which needs to be altered

DOJAdd would look like

Eager: 20-Jun-2005
Seeker: 18-Sep-2007

So I am extracting 20-Jun-2005 for Eager file and adding that content
# 7  
Old 07-06-2008
Try this:

Code:
awk 'BEGIN{FS=OFS=": "}
NR==FNR{a[$1]=$2;next}
a[$2]{s=a[$2];print;getline;print;print "DOJ: " s;next}
1' DOJAdd.txt Eager.txt

If you get errors use nawk, gawk or /usr/xpg4/bin/awk on Solaris.

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Adding a new line after a specific line with sed

Hi All, My requirement is to add a specific line in a file after a certain line that contains 'setenv' the existing code is like setenv SEQFILES "/ConvWrk/inteng03/alltars/bnymais1" LIBDEF scope='JOB' type='PGM' dataset='SUNAR.PJ90000P.JOBLIB'... (5 Replies)
Discussion started by: gotamp
5 Replies

2. Shell Programming and Scripting

Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern

'Hi I'm using the following code to extract the lines(and redirect them to a txt file) after the pattern match. But the output is inclusive of the line with pattern match. Which option is to be used to exclude the line containing the pattern? sed -n '/Conn.*User/,$p' > consumers.txt (11 Replies)
Discussion started by: essem
11 Replies

3. Shell Programming and Scripting

sed adding a new line not working

sed '/patternstring/ a\ new line string' file1 The above code is not working even with the i option.... it shows sed grambled if '\' after new line string is not being used....after using no changes it is displaying..Pls help (5 Replies)
Discussion started by: bhavanabahety
5 Replies

4. Shell Programming and Scripting

sed adding a new line not working

The file which is used is /abc/apps/cobbbbbb/apps/abadv/binder/axyz.bnd /abc/apps/cobbbbbb/apps/abbrio/binder/na6115.bnd /abc/apps/cobbbbbb/apps/abbrio/binder/kc22.bnd /abc/apps/cobbbbbb/apps/abbrio/binder/tr4823.bnd /abc/apps/cobbbbbb/apps/abcmp/binder/cpc0105.bnd The commads which I ran... (3 Replies)
Discussion started by: bhavanabahety
3 Replies

5. UNIX for Dummies Questions & Answers

Sed: Adding new line after matching pattern

Hi I just wanted to add a new line after every matching pattern: The method doing this doesn't matter, however, I have been using sed and this is what I tried doing, knowing that I am a bit off: sed 'Wf a\'/n'/g' Basically, I want to add a new line after occurrence of Wf. After the line Wf... (5 Replies)
Discussion started by: MIA651
5 Replies

6. Shell Programming and Scripting

sed adding a blank line

I use the following as part of a script to correct for a faulty hostname file. # get the domain name read -r thehostname < /etc/hostname dom="$(echo $thehostname | cut -d'.' -f2)" numchar=${#dom} if then echo "It appears as though the hostname is not correctly set." echo "Hostname has... (5 Replies)
Discussion started by: bugeye
5 Replies

7. Shell Programming and Scripting

sed: Find start of pattern and extract text to end of line, including the pattern

This is my first post, please be nice. I have tried to google and read different tutorials. The task at hand is: Input file input.txt (example) abc123defhij-E-1234jslo 456ujs-W-abXjklp From this file the task is to grep the -E- and -W- strings that are unique and write a new file... (5 Replies)
Discussion started by: TestTomas
5 Replies

8. Shell Programming and Scripting

Need help in sed command (adding a blank line btw each block generated by pattern)

Hello friends, I have a C source code containing sql statements. I use the following sed command to print all the sql blocks in the source code.... sed -n "/exec sql/,/;/p" Sample.cpp The above sed command will print the sql blocks based on the pattern "exec sql" & ";"... (2 Replies)
Discussion started by: frozensmilz
2 Replies

9. UNIX for Dummies Questions & Answers

sed - adding new line

I want to use sed to look for spaces in text and when find one move the next word to the next line. I used: sed 's/ /\n/g' out > new However when there is more than one space between two words it adds more lines between them. And I just want the words to be one under another. How can I... (2 Replies)
Discussion started by: sovixi
2 Replies

10. Solaris

adding a new line using sed command

I need to find text "A" and replace it with A B. The issue is that I need a new line between A and B. \n does not do the work. Any help would be much appreciated. Thanks, (2 Replies)
Discussion started by: ivesia
2 Replies
Login or Register to Ask a Question