Visit Our UNIX and Linux User Community


replace only 1st word of a line if it comes in the subsequent lines at same postion.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting replace only 1st word of a line if it comes in the subsequent lines at same postion.
# 1  
Old 09-25-2007
replace only 1st word of a line if it comes in the subsequent lines at same postion.

I have a file like this..

Maharastra Mumbai worli
Maharastra Mumbai navy
maharatra Pune
Maharastra Nagpur
Karnataka Bangalore
Karnataka Mysore
Karnataka Mangalore
Punjab Amritsar
punjab Jalandar


my expected outcome should be like this

Maharastra Mumbai worli
---------- Mumbai Navy
---------- pune
---------- Nagpur
Karnataka Bangalore
--------- mysore
--------- Mangalore
Punjab Amritsar
--------- Jalandar

i am not able to find any similar posts,please reply

thanks in advance,
regards,
Geeko
# 2  
Old 09-25-2007
i am also struggling with the same one, can some one have look into it. so that it'll be useful ( need solution for bash shell)

thanks,
# 3  
Old 09-25-2007
Two users with the same requirement! Is it homework ?
# 4  
Old 09-25-2007
Quote:
Originally Posted by vino
Two users with the same requirement! Is it homework ?
most likely
# 5  
Old 09-25-2007
Refer to this one!!!!

Hi,
Hope this one can help you.

iuput(a):
Code:
Maharastra Mumbai worli
Maharastra Mumbai navy
maharatra Pune
Maharastra Nagpur
Karnataka Bangalore
Karnataka Mysore
Karnataka Mangalore
Punjab Amritsar
punjab Jalandar

output:
Code:
Maharastra Mumbai worli
-------- Mumbai navy
maharatra Pune
Maharastra Nagpur
Karnataka Bangalore
-------- Mysore
-------- Mangalore
Punjab Amritsar
-------- Jalandar

code:
Code:
cat a | nawk '
{
        if (cur=="")
        {
                cur=toupper($1)
                print $0
        }
        else
        {
                if (toupper($1)==cur)
                print "--------"substr($0,index($0," "),length($0)-length($1))
                else
                {
                        print $0
                        cur=toupper($1)
                }
        }
}'

# 6  
Old 09-25-2007
thanks a lot for your code summer cherry, the command nawk didn't work, but with awk command, i met my requirement.
# 7  
Old 07-03-2008
How can I replace a string from a multiple lines. I have two words in my text file and want to replace some text from a particular position in second word.

example :

I have a line :
grep 445170141207

and I want :
grep 44495 *list*
that is from 6th position I want to insert a string *list* in my second word.

Can any one help me in this regard.

Thanks in Advance

Previous Thread | Next Thread
Test Your Knowledge in Computers #415
Difficulty: Easy
HTML, CSS, and JavaScript are the three core technologies of the World Wide Web.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Get an output of lines in pattern 1st line then 10th line then 11th line then 20th line and so on.

Input file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 (6 Replies)
Discussion started by: Sagar Singh
6 Replies

2. UNIX for Beginners Questions & Answers

Replace only First Word in Line

I have query to replace the first word in line using below code but its replace the middle word too sed -i 's/load /# LOAD/' /tmp/check.sql Query 1 : UPDATE accheadcon_data_last_upload SET last_upload_date = '2017-07-23' Replace to UPDATE accheadcon_data_last_up# LOAD SET... (1 Reply)
Discussion started by: kaushik02018
1 Replies

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

4. UNIX for Advanced & Expert Users

Need command for grepping pattern lines with subsequent lines

Hi, I have a requirement like, I have a list of pattens in a file say pattern.txt, PHC111 PHC113 and in another file called master.lst i have entries like, PHC111 a b PHC112 a PHC113 b c PHC114 d e (5 Replies)
Discussion started by: rbalaj16
5 Replies

5. Shell Programming and Scripting

Grep the word from pattern line and update in subsequent lines till next pattern line reached

Hi, I have got the below requirement. please suggest. I have a file like, Processing Item is: /data/ing/cfg2/abc.txt /data/ing/cfg3/bgc.txt Processing Item is: /data/cmd/for2/ght.txt /data/kernal/config.klgt.txt I want to process the above file to get the output file like, ... (5 Replies)
Discussion started by: rbalaj16
5 Replies

6. Shell Programming and Scripting

awk - use fields from subsequent lines

I've run into a problem getting exactly what I want out of awk - some folks may recognize this as an output from Amazon's ec2-describe-instances: Given the following: INSTANCE i-4960f321 BLOCKDEVICE Line2Var2 TAG instance i-4960f321 Name web1 TAG instance i-4960f321... (2 Replies)
Discussion started by: colinjohnson
2 Replies

7. Shell Programming and Scripting

Grep and fetch subsequent lines also

Hi, I need to grep a pattern and fetch subsequent lines till end of the data-set. E.g., i have a file like: AA 1111 23 34 BB 45 56 78 CC 22 44 AA 2222 78 34 56 BB 22 56 67 68 23 CC 56 78 DD 33 55 77 AA 3333 46 BB 58 79 In above file i have 3-data sets where each set starts with... (6 Replies)
Discussion started by: prvnrk
6 Replies

8. Shell Programming and Scripting

search for keyword in subsequent lines and delete the second line

I have my data something like this I need to search for the keyword yyyy in the susequent lines and if it is present, delete the second line with keyword. In other words, if a keywords is found in two subsequent lines delete the second line. input data: aaaa bbbbb cccc dddd xxxx... (4 Replies)
Discussion started by: rdhanek
4 Replies

9. Shell Programming and Scripting

How to search for keywords in subsequent lines

Hi all, I am looking for a coomand to search for the keywords in susequenct lines. Keyword1 in a line and Keyword2 in the very next line. Once i found the combination ineed to print the lines with patterns and the line above and one below. I am giving an example here: Keywords are :ERROR and... (12 Replies)
Discussion started by: rdhanek
12 Replies

10. Shell Programming and Scripting

Need to replace the first word of a line if it occurs again in the next line(shell)

Hi folks, have a look into the attachment, i am not familiar with unix, can you please help me in this regard. thanks in advance, :) regards, Geeko (4 Replies)
Discussion started by: geeko
4 Replies

Featured Tech Videos