How to replace word with multiline text using shell scripting.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to replace word with multiline text using shell scripting.
# 1  
Old 09-04-2012
How to replace word with multiline text using shell scripting.

Hi all
I have data files which contain data as shown below:

Code:
Line 5:  FIDE
INST_DESC:  DIAM Co Ltd/Japan => MAID Co Ltd/Japan
INST_NME:  DIAM Co Ltd/Japan => MAID Co Ltd/Japan


Line 6:  FIDE
INST_DESC:  DIAM DL/Pimco US Bond Open Born in the USA => MAID DL/Pimco US Bond Open Born in the USA
INST_NME:  DIAM DL/Pimco US Bond Open Born in the USA => MAID DL/Pimco US Bond Open Born in the USA

Basically above data is comparison between to database dumps.

Now i want to add a line between Line 5: FIDE and the next line (say, something like "column name" and other stuff)
E.g:
Code:
Line 5:  FIDE
COLUMN NAME
INST_DESC:  DIAM Co Ltd/Japan => MAID Co Ltd/Japan
INST_NME:  DIAM Co Ltd/Japan => MAID Co Ltd/Japan

Now the thing is this Line 5 keeps changing in an uneven fashion.
so how should i do it?

thanks in advance
Ganesh

Moderator's Comments:
Mod Comment Please use code tags next time for your code and data.

Last edited by radoulov; 09-04-2012 at 07:16 AM..
# 2  
Old 09-04-2012
Please use code tags...

Try something like this..
Code:
awk '{ if ($0 ~ /Line 5:/) { print; print "Column name"} else { print }}' file

# 3  
Old 09-04-2012
awk

Hi,

Check this out,

Code:
awk '/^Line 5:/{print $0 RS "Column Name";next;}1' file

Cheers,
Ranga Smilie
# 4  
Old 09-04-2012
Can you please explain the code too
# 5  
Old 09-04-2012
awk

Quote:
Originally Posted by Ganesh_more
Can you please explain the code too
Code:
awk '/^Line 5:/{print $0 RS "Column Name";next;}1' file

/^Line 5:/ - Check if $0 starts with 'Line 5:' then goto the {print....}
RS - Record separator (by default \n).
1 - denotes 1{print;}. Here will print all the lines except 'Line 5:'

are you clear with this..?

Cheers,
Ranga Smilie
# 6  
Old 09-04-2012
Hi puma i tried your code and it is working fine. I want to know in your code where have written the text to be written in the file should be on the next line coz i don't see anywhere '\n' ???
# 7  
Old 09-04-2012
As rangarasan already said, RS is newline by default.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell Script @ Find a key word and If the key word matches then replace next 7 lines only

Hi All, I have a XML file which is looks like as below. <<please see the attachment >> <?xml version="1.0" encoding="UTF-8"?> <esites> <esite> <name>XXX.com</name> <storeId>10001</storeId> <module> ... (4 Replies)
Discussion started by: Rajeev_hbk
4 Replies

2. Shell Programming and Scripting

Shell scripting: frequency of specific word in a string and statistics

Hello friends, I need a BIG help from UNIX collective intelligence: I have a CSV file like this: VALUE,TIMESTAMP,TEXT 1,Sun May 05 16:13:05 +0000 2013,"RT @gracecheree: Praying God sends me a really great man one day. Gotta trust in his timing. 0,Sun May 05 16:13:05 +0000 2013,@sendi__... (19 Replies)
Discussion started by: kraterions
19 Replies

3. Shell Programming and Scripting

perl : replace multiline text between two marker points

Hi there I just wondered if someone could give me some perl advice I have a bunch of text files used for a wiki that have common headings such as ---++ Title blah ---++ Summary blah ---++ Details Here is the multiline block of text I wish to (6 Replies)
Discussion started by: rethink
6 Replies

4. Shell Programming and Scripting

How to put a word starting at particular position in a file using shell scripting

Hi all, I'm new to shell scripting and hence this query. I have 2 files. temp.txt and config.txt. The values in temp.txt are tab separated. ex: temp.txt AB CDE GHIJ OPQRS WXY ex:config.txt (1st line for 1st element of temp.txt and so on) start = '1' end='5' start = '6' end =... (26 Replies)
Discussion started by: subhrap.das
26 Replies

5. Homework & Coursework Questions

[Scripting]Find & replace using user input then replacing text after

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: (o) Checkout an auto part: should prompt the user for the name of the auto part and borrower's name: Name:... (2 Replies)
Discussion started by: SlapnutsGT
2 Replies

6. Shell Programming and Scripting

Sed scripting, match text within line and replace

New to sed... Have a file foo.txt (below). Need to replace text on 2 lines, but can only feed sed the first few characters of each line (all lines are unique). So, in my example, I have put '$' in place of what I need to figure out how to feed the whole line. What I have thus far: sed -e... (6 Replies)
Discussion started by: boolean2222
6 Replies

7. Shell Programming and Scripting

grep command to replace multiline text from httpd.conf file on Fedora

Hi, I am a newbie to shell scripting and to Linux environment as well. In my project I am trying to search for following text from the httpd.conf file <Directory '/somedir/someinnerdir'> AllowOverride All </Directory> and then remove this text and again rewrite the same text. The... (1 Reply)
Discussion started by: bhushan
1 Replies

8. Shell Programming and Scripting

How to merge multiline into line begining with specific word

Hi, The file format is like the following. timestamp=2008-02-28-23.50.29.550675;category=CONTEXT;audit event=CONNECT; event correlator=2; database=CURDOMS;userid=inst3;authid=INST3; origin node=0;coordinator node=0; application id=AC122081.FA97.054468155029;application... (2 Replies)
Discussion started by: missyou
2 Replies

9. Shell Programming and Scripting

Find and replace a part of the word in Shell

I have a csv file in which there are numbers like 078976/9XXX 098754/8XXX I want to replace the XXX with null. I want to know the command/code to do this. I know how to replace the whole word/number. But don't know how to replace a part of it. Thanks in advance, Mihir (3 Replies)
Discussion started by: mihirk
3 Replies

10. Shell Programming and Scripting

Can a shell script pull the first word (or nth word) off each line of a text file?

Greetings. I am struggling with a shell script to make my life simpler, with a number of practical ways in which it could be used. I want to take a standard text file, and pull the 'n'th word from each line such as the first word from a text file. I'm struggling to see how each line can be... (5 Replies)
Discussion started by: tricky
5 Replies
Login or Register to Ask a Question