The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
replace only 1st word of a line if it comes in the subsequent lines at same postion. geeko Shell Programming and Scripting 9 06-03-2009 07:25 AM
how can i replace / with new line in shell script or sed ? mail2sant Shell Programming and Scripting 3 04-25-2008 12:59 AM
How to replace a word at a parcitular line stevefox Shell Programming and Scripting 3 04-04-2007 02:27 AM
Can a shell script pull the first word (or nth word) off each line of a text file? tricky Shell Programming and Scripting 5 08-17-2006 06:29 AM
find a shell and replace the line jvellon UNIX for Advanced & Expert Users 1 06-13-2006 08:44 PM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-24-2007
geeko geeko is offline
Registered User
  
 

Join Date: Sep 2007
Posts: 15
Smile 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
Attached Files
File Type: doc New Microsoft Word Document.doc (25.5 KB, 86 views)
  #2 (permalink)  
Old 09-25-2007
borgeh borgeh is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 12
Quote:
Originally Posted by geeko View Post
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
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 (permalink)  
Old 06-16-2009
quirkasaurus's Avatar
quirkasaurus quirkasaurus is offline
Registered User
  
 

Join Date: Jan 2009
Location: canton, michigan
Posts: 373
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 (permalink)  
Old 06-17-2009
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,079
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 (permalink)  
Old 06-18-2009
tkleczek tkleczek is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 34
Try following sed script:
Code:
#!/bin/sed -f
	1h
	1!{
		x
		G
		s/\([[:alnum:]]\+\)\>.*\n\1\(.*\)/\t\2/i
		s/.*\n//
	}
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:46 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0