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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to replace the first word of a line if it occurs again in the next line(shell)
# 1  
Old 09-24-2007
Bug 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, Smilie
regards,
Geeko
# 2  
Old 09-25-2007
Quote:
Originally Posted by geeko
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, Smilie
regards,
Geeko
Suggest that you copy and paste content from the word document here (unless it's too big) or attach it as a pure textfile. People might hesitate to open a word-file they don't know anything about.
# 3  
Old 06-16-2009
Code:
 
previous_word=
cat your_file |
while read word rest_of_line ; do
  print_word=$word
  if [ "$print_word" = "$previous_word" ]; then
    print_word="\t"
  fi
  print "$print_word $rest_of_line"
  previous_word=$word
done

# 4  
Old 06-17-2009
Code:
while(<DATA>){
	my @tmp = split(" ",$_,2);
	if($.==1){
		$key=$tmp[0];
		print;
	}
	else{
		if(/$key/i){
			$tmp[0]=sprintf ' ' x length($tmp[0]);
			print join " ",@tmp;
		}
		else{
			$key=$tmp[0];
			print;
		}
	}
}
__DATA__
Maharastra Mumbai worli
Maharastra Mumbai navy
maharastra Pune
Maharastra Nagpur
Karnataka Bangalore
Karnataka Mysore
Karnataka Mangalore
Punjab Amritsar
punjab Jalandar

# 5  
Old 06-18-2009
Try following sed script:
Code:
#!/bin/sed -f
	1h
	1!{
		x
		G
		s/\([[:alnum:]]\+\)\>.*\n\1\(.*\)/\t\2/i
		s/.*\n//
	}

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. Shell Programming and Scripting

Find word in a line and output in which line the word occurs / no. of times it occurred

I have a file: file.txt, which contains the following data in it. This is a file, my name is Karl, what is this process, karl is karl junior, file is a test file, file's name is file.txt My name is not Karl, my name is Karl Joey What is your name? Do you know your name and... (3 Replies)
Discussion started by: anuragpgtgerman
3 Replies

3. Shell Programming and Scripting

Read a File line by line and split into array word by word

Hi All, Hope you guys had a wonderful weekend I have a scenario where in which I have to read a file line by line and check for few words before redirecting to a file I have searched the forum but,either those answers dint work (perhaps because of my wrong under standing of how IFS... (6 Replies)
Discussion started by: Kingcobra
6 Replies

4. Shell Programming and Scripting

sed command to replace a word with new line and /

Hi, I have been trying to replace the key word "SQL> spool off " with "/ show errors" with out double quotes in all the files in a directory. above show erros should be displayed next line Could you please help me how to do that. I have tried something like this... (3 Replies)
Discussion started by: pointers
3 Replies

5. Shell Programming and Scripting

perl: replace multiple word on a line

Hi All, If I have a line as following: ( MA "vertical" ) How can I convert it to as below: ( BC "horizontal" ) Thanks, --Michael (6 Replies)
Discussion started by: mxn731
6 Replies

6. Shell Programming and Scripting

get the fifth line of a text file into a shell script and trim the line to extract a WORD

FOLKS , i have a text file that is generated automatically of an another korn shell script, i want to bring in the fifth line of the text file in to my korn shell script and look for a particular word in the line . Can you all share some thoughts on this one. thanks... Venu (3 Replies)
Discussion started by: venu
3 Replies

7. Shell Programming and Scripting

Looking for a single line to count how many times one character occurs in a word...

I've been looking on the internet, and haven't found anything simple enough to use in my code. All I want to do is count how many times "-" occurs in a string of characters (as a package name). It seems it should be very simple, and shouldn't require more than one line to accomplish. And this is... (2 Replies)
Discussion started by: Shingoshi
2 Replies

8. Shell Programming and Scripting

Replace only if the keyword is the first word in every line

How do I replace only if the keyword is at the begining of a line? Code: -- a = “This is a print statement” print a -- What if I want to replace print by #print only in the second line i.e only if the line starts with that keyword. Please help me out. I'm new to SED. -----Post... (5 Replies)
Discussion started by: alexzubin
5 Replies

9. Shell Programming and Scripting

How to replace a word at a parcitular line

Could someone tell me how to replace a word at a particular line by a single SED or AWK command? e.g. I have a file with the contents below: $ cat file1 111 AAA 333 CCC 222 BBB 444 CCC I want to replace the word "CCC" with a blank to get the desired output below: 111 AAA 333 CCC... (3 Replies)
Discussion started by: stevefox
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