The UNIX and Linux Forums  

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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Need to serach if a new line character exists on the last line in a file sunilbm78 UNIX for Dummies Questions & Answers 10 02-29-2008 11:15 AM
batch delete using find, files with # character tphyahoo UNIX for Dummies Questions & Answers 0 05-30-2006 09:05 AM
Find files which contain a null character Bab00shka UNIX for Dummies Questions & Answers 5 12-16-2005 07:41 AM
delete a line based on first character of the line borncrazy UNIX for Dummies Questions & Answers 2 12-06-2005 12:27 PM
end of the line character zomboo Shell Programming and Scripting 5 12-30-2004 02:30 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 01-24-2008
Registered User
 

Join Date: Jan 2008
Posts: 23
need to find last character in each line in all files

Hi,

I need to read each line in all files .If last character in each line is not ;(semicolon) then i need to append next line also.Here each line may consists of multiple semicolons(.but i need to read only last character.I need to achieve this using shell script.And i need to remove tht last semicolon also.I need to achieve this in all files in a directory and i need to redirect modified files to the same directory.
How to achieve this?

For example my file consists of
asdfgh;adffg;afdfg;
adfsffvfgggg;
aedfdsfs
fsff;

Here i need output as
asdfgh;adffg;afdfg
adfsffvfgggg
aedfdsfs fsff

How to achieve this?
Reply With Quote
Forum Sponsor
  #2  
Old 01-24-2008
Yogesh Sawant's Avatar
Part Time Moderator and Full Time Dad
 

Join Date: Sep 2006
Location: Rossem, Tazenda
Posts: 798
if you have GNU sed:
Code:
sed -i 's/;$//' file*
or using Perl:
Code:
perl -pi -e 's/;$//' file*
Reply With Quote
  #3  
Old 01-24-2008
Registered User
 

Join Date: Jan 2008
Posts: 23
Hi,

If there is no semicolon its not appending next line with present line.And also its not working if my file contains pipe line characters like '|'.

For example if file contains

user|find|ghjk|gg|;
sufgh|fghj|hgj
Dhj|hjk|jkl;
its not removing semicolon
and its not appending 3red line to 2nd line
Reply With Quote
  #4  
Old 01-24-2008
Registered User
 

Join Date: Jun 2006
Posts: 74
try this

sed -e :a -e '/;$/!N;s/\n//; ta' -e 's/;$//' file
Reply With Quote
  #5  
Old 01-24-2008
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milano, Italia/Варна, България
Posts: 1,933
Code:
awk 'ORS=/;$/?RS:FS{sub(/;$/,"")}1' filename
Use nawk or /usr/xpg4/bin/awk on Solaris.
Reply With Quote
  #6  
Old 01-24-2008
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milano, Italia/Варна, България
Posts: 1,933
As far as the entire task is concerned:

Code:
awk 'FNR == 1 {
  system("[ -f \""fnt"\" ]&&mv "fnt" "fn)
  fn = FILENAME
  fnt = FILENAME"__"++c
}
ORS = /;$/ ? RS : FS {
  sub(/;$/,"")
  print > fnt
} END {
  system("[ -f \""fnt"\" ]&&mv "fnt" "fn)
}' filenames*
Use nawk or /usr/xpg4/bin/awk on Solaris.
Reply With Quote
  #7  
Old 01-24-2008
Registered User
 

Join Date: Jan 2008
Posts: 23
Hi quintet

using this sed -e :a -e '/;$/!N;s/\n//; ta' -e 's/;$//' file its not redirecting to my directory again
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
perl, sed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 06:38 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0